Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: third_party/WebKit/Source/wtf/text/StringOperators.h

Issue 2315853002: Massively simplify WTF's StringConcatenate (Closed)
Patch Set: missing inlines. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 #include "wtf/Allocator.h"
26 #include "wtf/text/StringConcatenate.h"
27
28 namespace WTF {
29
30 template<typename StringType1, typename StringType2>
31 class StringAppend final {
32 STACK_ALLOCATED();
33 public:
34 StringAppend(StringType1 string1, StringType2 string2);
35
36 operator String() const;
37
38 operator AtomicString() const;
39
40 bool is8Bit();
41
42 void writeTo(LChar* destination);
43
44 void writeTo(UChar* destination);
45
46 unsigned length();
47
48 private:
49 StringType1 m_string1;
50 StringType2 m_string2;
51 };
52
53 template<typename StringType1, typename StringType2>
54 StringAppend<StringType1, StringType2>::StringAppend(StringType1 string1, String Type2 string2)
55 : m_string1(string1)
56 , m_string2(string2)
57 {
58 }
59
60 template<typename StringType1, typename StringType2>
61 StringAppend<StringType1, StringType2>::operator String() const
62 {
63 return String(makeString(m_string1, m_string2));
64 }
65
66 template<typename StringType1, typename StringType2>
67 StringAppend<StringType1, StringType2>::operator AtomicString() const
68 {
69 return AtomicString(makeString(m_string1, m_string2));
70 }
71
72 template<typename StringType1, typename StringType2>
73 bool StringAppend<StringType1, StringType2>::is8Bit()
74 {
75 StringTypeAdapter<StringType1> adapter1(m_string1);
76 StringTypeAdapter<StringType2> adapter2(m_string2);
77 return adapter1.is8Bit() && adapter2.is8Bit();
78 }
79
80 template<typename StringType1, typename StringType2>
81 void StringAppend<StringType1, StringType2>::writeTo(LChar* destination)
82 {
83 ASSERT(is8Bit());
84 StringTypeAdapter<StringType1> adapter1(m_string1);
85 StringTypeAdapter<StringType2> adapter2(m_string2);
86 adapter1.writeTo(destination);
87 adapter2.writeTo(destination + adapter1.length());
88 }
89
90 template<typename StringType1, typename StringType2>
91 void StringAppend<StringType1, StringType2>::writeTo(UChar* destination)
92 {
93 StringTypeAdapter<StringType1> adapter1(m_string1);
94 StringTypeAdapter<StringType2> adapter2(m_string2);
95 adapter1.writeTo(destination);
96 adapter2.writeTo(destination + adapter1.length());
97 }
98
99 template<typename StringType1, typename StringType2>
100 unsigned StringAppend<StringType1, StringType2>::length()
101 {
102 StringTypeAdapter<StringType1> adapter1(m_string1);
103 StringTypeAdapter<StringType2> adapter2(m_string2);
104 return adapter1.length() + adapter2.length();
105 }
106
107 template<typename StringType1, typename StringType2>
108 class StringTypeAdapter<StringAppend<StringType1, StringType2>> {
109 STACK_ALLOCATED();
110 public:
111 StringTypeAdapter<StringAppend<StringType1, StringType2>>(StringAppend<Strin gType1, StringType2>& buffer)
112 : m_buffer(buffer)
113 {
114 }
115
116 unsigned length() { return m_buffer.length(); }
117
118 bool is8Bit() { return m_buffer.is8Bit(); }
119
120 void writeTo(LChar* destination) { m_buffer.writeTo(destination); }
121 void writeTo(UChar* destination) { m_buffer.writeTo(destination); }
122
123 private:
124 StringAppend<StringType1, StringType2>& m_buffer;
125 };
126
127 inline StringAppend<const char*, String> operator+(const char* string1, const St ring& string2)
128 {
129 return StringAppend<const char*, String>(string1, string2);
130 }
131
132 inline StringAppend<const char*, AtomicString> operator+(const char* string1, co nst AtomicString& string2)
133 {
134 return StringAppend<const char*, AtomicString>(string1, string2);
135 }
136
137 template<typename U, typename V>
138 inline StringAppend<const char*, StringAppend<U, V>> operator+(const char* strin g1, const StringAppend<U, V>& string2)
139 {
140 return StringAppend<const char*, StringAppend<U, V>>(string1, string2);
141 }
142
143 inline StringAppend<const UChar*, String> operator+(const UChar* string1, const String& string2)
144 {
145 return StringAppend<const UChar*, String>(string1, string2);
146 }
147
148 inline StringAppend<const UChar*, AtomicString> operator+(const UChar* string1, const AtomicString& string2)
149 {
150 return StringAppend<const UChar*, AtomicString>(string1, string2);
151 }
152
153 template<typename U, typename V>
154 inline StringAppend<const UChar*, StringAppend<U, V>> operator+(const UChar* str ing1, const StringAppend<U, V>& string2)
155 {
156 return StringAppend<const UChar*, StringAppend<U, V>>(string1, string2);
157 }
158
159 template<typename T>
160 StringAppend<String, T> operator+(const String& string1, T string2)
161 {
162 return StringAppend<String, T>(string1, string2);
163 }
164
165 template<typename U, typename V, typename W>
166 StringAppend<StringAppend<U, V>, W> operator+(const StringAppend<U, V>& string1, W string2)
167 {
168 return StringAppend<StringAppend<U, V>, W>(string1, string2);
169 }
170
171 } // namespace WTF
172
173 #endif // StringOperators_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698