]>
Commit | Line | Data |
---|---|---|
14957cd0 A |
1 | /* |
2 | * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved. | |
3 | * Copyright (C) Research In Motion Limited 2011. All rights reserved. | |
4 | * | |
5 | * This library is free software; you can redistribute it and/or | |
6 | * modify it under the terms of the GNU Library General Public | |
7 | * License as published by the Free Software Foundation; either | |
8 | * version 2 of the License, or (at your option) any later version. | |
9 | * | |
10 | * This library is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * Library General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU Library General Public License | |
16 | * along with this library; see the file COPYING.LIB. If not, write to | |
17 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | |
18 | * Boston, MA 02110-1301, USA. | |
19 | * | |
20 | */ | |
21 | ||
22 | #ifndef StringOperators_h | |
23 | #define StringOperators_h | |
24 | ||
25 | namespace WTF { | |
26 | ||
27 | template<typename StringType1, typename StringType2> | |
28 | class StringAppend { | |
29 | public: | |
30 | StringAppend(StringType1 string1, StringType2 string2) | |
31 | : m_string1(string1) | |
32 | , m_string2(string2) | |
33 | { | |
34 | } | |
35 | ||
36 | operator String() const | |
37 | { | |
38 | RefPtr<StringImpl> resultImpl = tryMakeString(m_string1, m_string2); | |
39 | if (!resultImpl) | |
40 | CRASH(); | |
41 | return resultImpl.release(); | |
42 | } | |
43 | ||
44 | operator AtomicString() const | |
45 | { | |
46 | return operator String(); | |
47 | } | |
48 | ||
49 | void writeTo(UChar* destination) | |
50 | { | |
51 | StringTypeAdapter<StringType1> adapter1(m_string1); | |
52 | StringTypeAdapter<StringType2> adapter2(m_string2); | |
53 | adapter1.writeTo(destination); | |
54 | adapter2.writeTo(destination + adapter1.length()); | |
55 | } | |
56 | ||
57 | unsigned length() | |
58 | { | |
59 | StringTypeAdapter<StringType1> adapter1(m_string1); | |
60 | StringTypeAdapter<StringType2> adapter2(m_string2); | |
61 | return adapter1.length() + adapter2.length(); | |
62 | } | |
63 | ||
64 | private: | |
65 | StringType1 m_string1; | |
66 | StringType2 m_string2; | |
67 | }; | |
68 | ||
69 | template<typename StringType1, typename StringType2> | |
70 | class StringTypeAdapter<StringAppend<StringType1, StringType2> > { | |
71 | public: | |
72 | StringTypeAdapter<StringAppend<StringType1, StringType2> >(StringAppend<StringType1, StringType2>& buffer) | |
73 | : m_buffer(buffer) | |
74 | { | |
75 | } | |
76 | ||
77 | unsigned length() { return m_buffer.length(); } | |
78 | void writeTo(UChar* destination) { m_buffer.writeTo(destination); } | |
79 | ||
80 | private: | |
81 | StringAppend<StringType1, StringType2>& m_buffer; | |
82 | }; | |
83 | ||
84 | inline StringAppend<const char*, String> operator+(const char* string1, const String& string2) | |
85 | { | |
86 | return StringAppend<const char*, String>(string1, string2); | |
87 | } | |
88 | ||
89 | inline StringAppend<const char*, AtomicString> operator+(const char* string1, const AtomicString& string2) | |
90 | { | |
91 | return StringAppend<const char*, AtomicString>(string1, string2); | |
92 | } | |
93 | ||
94 | template<typename T> | |
95 | StringAppend<String, T> operator+(const String& string1, T string2) | |
96 | { | |
97 | return StringAppend<String, T>(string1, string2); | |
98 | } | |
99 | ||
100 | template<typename U, typename V, typename W> | |
101 | StringAppend<U, StringAppend<V, W> > operator+(U string1, const StringAppend<V, W>& string2) | |
102 | { | |
103 | return StringAppend<U, StringAppend<V, W> >(string1, string2); | |
104 | } | |
105 | ||
106 | } // namespace WTF | |
107 | ||
108 | #endif // StringOperators_h |