OLD | NEW |
| (Empty) |
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 RELEASE_ASSERT(resultImpl); | |
40 return resultImpl.release(); | |
41 } | |
42 | |
43 operator AtomicString() const | |
44 { | |
45 return operator String(); | |
46 } | |
47 | |
48 bool is8Bit() | |
49 { | |
50 StringTypeAdapter<StringType1> adapter1(m_string1); | |
51 StringTypeAdapter<StringType2> adapter2(m_string2); | |
52 return adapter1.is8Bit() && adapter2.is8Bit(); | |
53 } | |
54 | |
55 void writeTo(LChar* destination) | |
56 { | |
57 ASSERT(is8Bit()); | |
58 StringTypeAdapter<StringType1> adapter1(m_string1); | |
59 StringTypeAdapter<StringType2> adapter2(m_string2); | |
60 adapter1.writeTo(destination); | |
61 adapter2.writeTo(destination + adapter1.length()); | |
62 } | |
63 | |
64 void writeTo(UChar* destination) | |
65 { | |
66 StringTypeAdapter<StringType1> adapter1(m_string1); | |
67 StringTypeAdapter<StringType2> adapter2(m_string2); | |
68 adapter1.writeTo(destination); | |
69 adapter2.writeTo(destination + adapter1.length()); | |
70 } | |
71 | |
72 unsigned length() | |
73 { | |
74 StringTypeAdapter<StringType1> adapter1(m_string1); | |
75 StringTypeAdapter<StringType2> adapter2(m_string2); | |
76 return adapter1.length() + adapter2.length(); | |
77 } | |
78 | |
79 private: | |
80 StringType1 m_string1; | |
81 StringType2 m_string2; | |
82 }; | |
83 | |
84 template<typename StringType1, typename StringType2> | |
85 class StringTypeAdapter<StringAppend<StringType1, StringType2> > { | |
86 public: | |
87 StringTypeAdapter<StringAppend<StringType1, StringType2> >(StringAppend<Stri
ngType1, StringType2>& buffer) | |
88 : m_buffer(buffer) | |
89 { | |
90 } | |
91 | |
92 unsigned length() { return m_buffer.length(); } | |
93 | |
94 bool is8Bit() { return m_buffer.is8Bit(); } | |
95 | |
96 void writeTo(LChar* destination) { m_buffer.writeTo(destination); } | |
97 void writeTo(UChar* destination) { m_buffer.writeTo(destination); } | |
98 | |
99 private: | |
100 StringAppend<StringType1, StringType2>& m_buffer; | |
101 }; | |
102 | |
103 inline StringAppend<const char*, String> operator+(const char* string1, const St
ring& string2) | |
104 { | |
105 return StringAppend<const char*, String>(string1, string2); | |
106 } | |
107 | |
108 inline StringAppend<const char*, AtomicString> operator+(const char* string1, co
nst AtomicString& string2) | |
109 { | |
110 return StringAppend<const char*, AtomicString>(string1, string2); | |
111 } | |
112 | |
113 template<typename U, typename V> | |
114 inline StringAppend<const char*, StringAppend<U, V> > operator+(const char* stri
ng1, const StringAppend<U, V>& string2) | |
115 { | |
116 return StringAppend<const char*, StringAppend<U, V> >(string1, string2); | |
117 } | |
118 | |
119 inline StringAppend<const UChar*, String> operator+(const UChar* string1, const
String& string2) | |
120 { | |
121 return StringAppend<const UChar*, String>(string1, string2); | |
122 } | |
123 | |
124 inline StringAppend<const UChar*, AtomicString> operator+(const UChar* string1,
const AtomicString& string2) | |
125 { | |
126 return StringAppend<const UChar*, AtomicString>(string1, string2); | |
127 } | |
128 | |
129 template<typename U, typename V> | |
130 inline StringAppend<const UChar*, StringAppend<U, V> > operator+(const UChar* st
ring1, const StringAppend<U, V>& string2) | |
131 { | |
132 return StringAppend<const UChar*, StringAppend<U, V> >(string1, string2); | |
133 } | |
134 | |
135 inline StringAppend<ASCIILiteral, String> operator+(const ASCIILiteral& string1,
const String& string2) | |
136 { | |
137 return StringAppend<ASCIILiteral, String>(string1, string2); | |
138 } | |
139 | |
140 inline StringAppend<ASCIILiteral, AtomicString> operator+(const ASCIILiteral& st
ring1, const AtomicString& string2) | |
141 { | |
142 return StringAppend<ASCIILiteral, AtomicString>(string1, string2); | |
143 } | |
144 | |
145 template<typename U, typename V> | |
146 inline StringAppend<ASCIILiteral, StringAppend<U, V> > operator+(const ASCIILite
ral& string1, const StringAppend<U, V>& string2) | |
147 { | |
148 return StringAppend<ASCIILiteral, StringAppend<U, V> >(string1, string2); | |
149 } | |
150 | |
151 template<typename T> | |
152 StringAppend<String, T> operator+(const String& string1, T string2) | |
153 { | |
154 return StringAppend<String, T>(string1, string2); | |
155 } | |
156 | |
157 template<typename U, typename V, typename W> | |
158 StringAppend<StringAppend<U, V>, W> operator+(const StringAppend<U, V>& string1,
W string2) | |
159 { | |
160 return StringAppend<StringAppend<U, V>, W>(string1, string2); | |
161 } | |
162 | |
163 } // namespace WTF | |
164 | |
165 #endif // StringOperators_h | |
OLD | NEW |