• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "StringConcatenate.h"
26 
27 namespace WTF {
28 
29 template<typename StringType1, typename StringType2>
30 class StringAppend {
31 public:
32     StringAppend(StringType1 string1, StringType2 string2);
33 
34     operator String() const;
35 
36     operator AtomicString() const;
37 
38     bool is8Bit();
39 
40     void writeTo(LChar* destination);
41 
42     void writeTo(UChar* destination);
43 
44     unsigned length();
45 
46 private:
47     StringType1 m_string1;
48     StringType2 m_string2;
49 };
50 
51 template<typename StringType1, typename StringType2>
StringAppend(StringType1 string1,StringType2 string2)52 StringAppend<StringType1, StringType2>::StringAppend(StringType1 string1, StringType2 string2)
53     : m_string1(string1)
54     , m_string2(string2)
55 {
56 }
57 
58 template<typename StringType1, typename StringType2>
String()59 StringAppend<StringType1, StringType2>::operator String() const
60 {
61     return String(makeString(m_string1, m_string2));
62 }
63 
64 template<typename StringType1, typename StringType2>
AtomicString()65 StringAppend<StringType1, StringType2>::operator AtomicString() const
66 {
67     return AtomicString(makeString(m_string1, m_string2));
68 }
69 
70 template<typename StringType1, typename StringType2>
is8Bit()71 bool StringAppend<StringType1, StringType2>::is8Bit()
72 {
73     StringTypeAdapter<StringType1> adapter1(m_string1);
74     StringTypeAdapter<StringType2> adapter2(m_string2);
75     return adapter1.is8Bit() && adapter2.is8Bit();
76 }
77 
78 template<typename StringType1, typename StringType2>
writeTo(LChar * destination)79 void StringAppend<StringType1, StringType2>::writeTo(LChar* destination)
80 {
81     ASSERT(is8Bit());
82     StringTypeAdapter<StringType1> adapter1(m_string1);
83     StringTypeAdapter<StringType2> adapter2(m_string2);
84     adapter1.writeTo(destination);
85     adapter2.writeTo(destination + adapter1.length());
86 }
87 
88 template<typename StringType1, typename StringType2>
writeTo(UChar * destination)89 void StringAppend<StringType1, StringType2>::writeTo(UChar* destination)
90 {
91     StringTypeAdapter<StringType1> adapter1(m_string1);
92     StringTypeAdapter<StringType2> adapter2(m_string2);
93     adapter1.writeTo(destination);
94     adapter2.writeTo(destination + adapter1.length());
95 }
96 
97 template<typename StringType1, typename StringType2>
length()98 unsigned StringAppend<StringType1, StringType2>::length()
99 {
100     StringTypeAdapter<StringType1> adapter1(m_string1);
101     StringTypeAdapter<StringType2> adapter2(m_string2);
102     return adapter1.length() + adapter2.length();
103 }
104 
105 template<typename StringType1, typename StringType2>
106 class StringTypeAdapter<StringAppend<StringType1, StringType2> > {
107 public:
108     StringTypeAdapter<StringAppend<StringType1, StringType2> >(StringAppend<StringType1, StringType2>& buffer)
m_buffer(buffer)109         : m_buffer(buffer)
110     {
111     }
112 
length()113     unsigned length() { return m_buffer.length(); }
114 
is8Bit()115     bool is8Bit() { return m_buffer.is8Bit(); }
116 
writeTo(LChar * destination)117     void writeTo(LChar* destination) { m_buffer.writeTo(destination); }
writeTo(UChar * destination)118     void writeTo(UChar* destination) { m_buffer.writeTo(destination); }
119 
120 private:
121     StringAppend<StringType1, StringType2>& m_buffer;
122 };
123 
124 inline StringAppend<const char*, String> operator+(const char* string1, const String& string2)
125 {
126     return StringAppend<const char*, String>(string1, string2);
127 }
128 
129 inline StringAppend<const char*, AtomicString> operator+(const char* string1, const AtomicString& string2)
130 {
131     return StringAppend<const char*, AtomicString>(string1, string2);
132 }
133 
134 template<typename U, typename V>
135 inline StringAppend<const char*, StringAppend<U, V> > operator+(const char* string1, const StringAppend<U, V>& string2)
136 {
137     return StringAppend<const char*, StringAppend<U, V> >(string1, string2);
138 }
139 
140 inline StringAppend<const UChar*, String> operator+(const UChar* string1, const String& string2)
141 {
142     return StringAppend<const UChar*, String>(string1, string2);
143 }
144 
145 inline StringAppend<const UChar*, AtomicString> operator+(const UChar* string1, const AtomicString& string2)
146 {
147     return StringAppend<const UChar*, AtomicString>(string1, string2);
148 }
149 
150 template<typename U, typename V>
151 inline StringAppend<const UChar*, StringAppend<U, V> > operator+(const UChar* string1, const StringAppend<U, V>& string2)
152 {
153     return StringAppend<const UChar*, StringAppend<U, V> >(string1, string2);
154 }
155 
156 template<typename T>
157 StringAppend<String, T> operator+(const String& string1, T string2)
158 {
159     return StringAppend<String, T>(string1, string2);
160 }
161 
162 template<typename U, typename V, typename W>
163 StringAppend<StringAppend<U, V>, W> operator+(const StringAppend<U, V>& string1, W string2)
164 {
165     return StringAppend<StringAppend<U, V>, W>(string1, string2);
166 }
167 
168 } // namespace WTF
169 
170 #endif // StringOperators_h
171