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

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

Issue 2245773002: Simplify and clean up StringTypeAdaptor and StringAppend. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: const ref it all. Created 4 years, 4 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
« no previous file with comments | « no previous file | third_party/WebKit/Source/wtf/text/StringConcatenate.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2010 Apple Inc. All rights reserved. 2 * Copyright (C) 2010 Apple Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
43 43
44 template<typename StringType> 44 template<typename StringType>
45 class StringTypeAdapter { 45 class StringTypeAdapter {
46 DISALLOW_NEW(); 46 DISALLOW_NEW();
47 }; 47 };
48 48
49 template<> 49 template<>
50 class StringTypeAdapter<char> { 50 class StringTypeAdapter<char> {
51 DISALLOW_NEW(); 51 DISALLOW_NEW();
52 public: 52 public:
53 StringTypeAdapter<char>(char buffer) 53 explicit StringTypeAdapter<char>(char buffer)
54 : m_buffer(buffer) 54 : m_buffer(buffer) {}
55 {
56 }
57 55
58 unsigned length() { return 1; } 56 unsigned length() const { return 1; }
57 bool is8Bit() const { return true; }
59 58
60 bool is8Bit() { return true; } 59 void writeTo(LChar* destination) const
61
62 void writeTo(LChar* destination)
63 { 60 {
64 *destination = m_buffer; 61 *destination = m_buffer;
65 } 62 }
66 63
67 void writeTo(UChar* destination) { *destination = m_buffer; } 64 void writeTo(UChar* destination) const { *destination = m_buffer; }
68 65
69 private: 66 private:
70 unsigned char m_buffer; 67 unsigned char m_buffer;
71 }; 68 };
72 69
73 template<> 70 template<>
74 class StringTypeAdapter<LChar> { 71 class StringTypeAdapter<LChar> {
75 DISALLOW_NEW(); 72 DISALLOW_NEW();
76 public: 73 public:
77 StringTypeAdapter<LChar>(LChar buffer) 74 explicit StringTypeAdapter<LChar>(LChar buffer)
78 : m_buffer(buffer) 75 : m_buffer(buffer) {}
79 {
80 }
81 76
82 unsigned length() { return 1; } 77 unsigned length() const { return 1; }
78 bool is8Bit() const { return true; }
83 79
84 bool is8Bit() { return true; } 80 void writeTo(LChar* destination) const
85
86 void writeTo(LChar* destination)
87 { 81 {
88 *destination = m_buffer; 82 *destination = m_buffer;
89 } 83 }
90 84
91 void writeTo(UChar* destination) { *destination = m_buffer; } 85 void writeTo(UChar* destination) const { *destination = m_buffer; }
92 86
93 private: 87 private:
94 LChar m_buffer; 88 LChar m_buffer;
95 }; 89 };
96 90
97 template<> 91 template<>
98 class StringTypeAdapter<UChar> { 92 class StringTypeAdapter<UChar> {
99 DISALLOW_NEW(); 93 DISALLOW_NEW();
100 public: 94 public:
101 StringTypeAdapter<UChar>(UChar buffer) 95 StringTypeAdapter<UChar>(UChar buffer)
102 : m_buffer(buffer) 96 : m_buffer(buffer) {}
103 {
104 }
105 97
106 unsigned length() { return 1; } 98 unsigned length() const { return 1; }
99 bool is8Bit() const { return m_buffer <= 0xff; }
107 100
108 bool is8Bit() { return m_buffer <= 0xff; } 101 void writeTo(LChar* destination) const
109
110 void writeTo(LChar* destination)
111 { 102 {
112 ASSERT(is8Bit()); 103 ASSERT(is8Bit());
113 *destination = static_cast<LChar>(m_buffer); 104 *destination = static_cast<LChar>(m_buffer);
114 } 105 }
115 106
116 void writeTo(UChar* destination) { *destination = m_buffer; } 107 void writeTo(UChar* destination) const { *destination = m_buffer; }
117 108
118 private: 109 private:
119 UChar m_buffer; 110 UChar m_buffer;
120 }; 111 };
121 112
122 template<> 113 template<>
123 class WTF_EXPORT StringTypeAdapter<char*> { 114 class WTF_EXPORT StringTypeAdapter<char*> {
124 DISALLOW_NEW(); 115 DISALLOW_NEW();
125 public: 116 public:
126 StringTypeAdapter<char*>(char* buffer) 117 explicit StringTypeAdapter<char*>(char* buffer)
127 : m_buffer(buffer) 118 : m_buffer(buffer)
128 , m_length(strlen(buffer)) 119 , m_length(strlen(buffer)) {}
129 {
130 }
131 120
132 unsigned length() { return m_length; } 121 unsigned length() const { return m_length; }
122 bool is8Bit() const { return true; }
133 123
134 bool is8Bit() { return true; } 124 void writeTo(LChar* destination) const;
135 125 void writeTo(UChar* destination) const;
136 void writeTo(LChar* destination);
137
138 void writeTo(UChar* destination);
139 126
140 private: 127 private:
141 const char* m_buffer; 128 const char* m_buffer;
142 unsigned m_length; 129 unsigned m_length;
143 }; 130 };
144 131
145 template<> 132 template<>
146 class WTF_EXPORT StringTypeAdapter<LChar*> { 133 class WTF_EXPORT StringTypeAdapter<LChar*> {
147 DISALLOW_NEW(); 134 DISALLOW_NEW();
148 public: 135 public:
149 StringTypeAdapter<LChar*>(LChar* buffer); 136 explicit StringTypeAdapter<LChar*>(LChar* buffer);
150 137
151 unsigned length() { return m_length; } 138 unsigned length() const { return m_length; }
139 bool is8Bit() const { return true; }
152 140
153 bool is8Bit() { return true; } 141 void writeTo(LChar* destination) const;
154 142 void writeTo(UChar* destination) const;
155 void writeTo(LChar* destination);
156
157 void writeTo(UChar* destination);
158 143
159 private: 144 private:
160 const LChar* m_buffer; 145 const LChar* m_buffer;
161 unsigned m_length; 146 unsigned m_length;
162 }; 147 };
163 148
164 template<> 149 template<>
165 class WTF_EXPORT StringTypeAdapter<const UChar*> { 150 class WTF_EXPORT StringTypeAdapter<const UChar*> {
166 DISALLOW_NEW(); 151 DISALLOW_NEW();
167 public: 152 public:
168 StringTypeAdapter(const UChar* buffer); 153 explicit StringTypeAdapter(const UChar* buffer);
169 154
170 unsigned length() { return m_length; } 155 unsigned length() const { return m_length; }
156 bool is8Bit() const { return false; }
171 157
172 bool is8Bit() { return false; } 158 NO_RETURN_DUE_TO_CRASH void writeTo(LChar*) const
173
174 NO_RETURN_DUE_TO_CRASH void writeTo(LChar*)
175 { 159 {
176 RELEASE_ASSERT(false); 160 RELEASE_ASSERT(false);
177 } 161 }
178 162
179 void writeTo(UChar* destination); 163 void writeTo(UChar* destination) const;
180 164
181 private: 165 private:
182 const UChar* m_buffer; 166 const UChar* m_buffer;
183 unsigned m_length; 167 unsigned m_length;
184 }; 168 };
185 169
186 template<> 170 template<>
187 class WTF_EXPORT StringTypeAdapter<const char*> { 171 class WTF_EXPORT StringTypeAdapter<const char*> {
188 DISALLOW_NEW(); 172 DISALLOW_NEW();
189 public: 173 public:
190 StringTypeAdapter<const char*>(const char* buffer); 174 explicit StringTypeAdapter<const char*>(const char* buffer);
191 175
192 unsigned length() { return m_length; } 176 unsigned length() const { return m_length; }
177 bool is8Bit() const { return true; }
193 178
194 bool is8Bit() { return true; } 179 void writeTo(LChar* destination) const;
195 180 void writeTo(UChar* destination) const;
196 void writeTo(LChar* destination);
197
198 void writeTo(UChar* destination);
199 181
200 private: 182 private:
201 const char* m_buffer; 183 const char* m_buffer;
202 unsigned m_length; 184 unsigned m_length;
203 }; 185 };
204 186
205 template<> 187 template<>
206 class WTF_EXPORT StringTypeAdapter<const LChar*> { 188 class WTF_EXPORT StringTypeAdapter<const LChar*> {
207 DISALLOW_NEW(); 189 DISALLOW_NEW();
208 public: 190 public:
209 StringTypeAdapter<const LChar*>(const LChar* buffer); 191 explicit StringTypeAdapter<const LChar*>(const LChar* buffer);
210 192
211 unsigned length() { return m_length; } 193 unsigned length() const { return m_length; }
194 bool is8Bit() const { return true; }
212 195
213 bool is8Bit() { return true; } 196 void writeTo(LChar* destination) const;
214 197 void writeTo(UChar* destination) const;
215 void writeTo(LChar* destination);
216
217 void writeTo(UChar* destination);
218 198
219 private: 199 private:
220 const LChar* m_buffer; 200 const LChar* m_buffer;
221 unsigned m_length; 201 unsigned m_length;
222 }; 202 };
223 203
224 template<> 204 template<>
225 class WTF_EXPORT StringTypeAdapter<Vector<char>> {
226 DISALLOW_NEW();
227 public:
228 StringTypeAdapter<Vector<char>>(const Vector<char>& buffer)
229 : m_buffer(buffer)
230 {
231 }
232
233 size_t length() { return m_buffer.size(); }
234
235 bool is8Bit() { return true; }
236
237 void writeTo(LChar* destination);
238
239 void writeTo(UChar* destination);
240
241 private:
242 const Vector<char>& m_buffer;
243 };
244
245 template<>
246 class StringTypeAdapter<Vector<LChar>> {
247 DISALLOW_NEW();
248 public:
249 StringTypeAdapter<Vector<LChar>>(const Vector<LChar>& buffer)
250 : m_buffer(buffer)
251 {
252 }
253
254 size_t length() { return m_buffer.size(); }
255
256 bool is8Bit() { return true; }
257
258 void writeTo(LChar* destination);
259
260 void writeTo(UChar* destination);
261
262 private:
263 const Vector<LChar>& m_buffer;
264 };
265
266 template<>
267 class WTF_EXPORT StringTypeAdapter<String> { 205 class WTF_EXPORT StringTypeAdapter<String> {
268 DISALLOW_NEW(); 206 DISALLOW_NEW();
269 public: 207 public:
270 StringTypeAdapter<String>(const String& string) 208 explicit StringTypeAdapter<String>(const String& string)
271 : m_buffer(string) 209 : m_buffer(string) {}
272 {
273 }
274 210
275 unsigned length() { return m_buffer.length(); } 211 unsigned length() const { return m_buffer.length(); }
212 bool is8Bit() const { return m_buffer.isNull() || m_buffer.is8Bit(); }
276 213
277 bool is8Bit() { return m_buffer.isNull() || m_buffer.is8Bit(); } 214 void writeTo(LChar* destination) const;
278 215 void writeTo(UChar* destination) const;
279 void writeTo(LChar* destination);
280
281 void writeTo(UChar* destination);
282 216
283 private: 217 private:
284 const String& m_buffer; 218 const String& m_buffer;
285 }; 219 };
286 220
287 template<> 221 template<>
288 class StringTypeAdapter<AtomicString> { 222 class StringTypeAdapter<AtomicString> {
289 DISALLOW_NEW(); 223 DISALLOW_NEW();
290 public: 224 public:
291 StringTypeAdapter<AtomicString>(const AtomicString& string) 225 explicit StringTypeAdapter<AtomicString>(const AtomicString& string)
292 : m_adapter(string.getString()) 226 : m_adapter(string.getString()) {}
293 {
294 }
295 227
296 unsigned length() { return m_adapter.length(); } 228 unsigned length() const { return m_adapter.length(); }
229 bool is8Bit() const { return m_adapter.is8Bit(); }
297 230
298 bool is8Bit() { return m_adapter.is8Bit(); } 231 void writeTo(LChar* destination) const { m_adapter.writeTo(destination); }
299 232 void writeTo(UChar* destination) const { m_adapter.writeTo(destination); }
300 void writeTo(LChar* destination) { m_adapter.writeTo(destination); }
301 void writeTo(UChar* destination) { m_adapter.writeTo(destination); }
302 233
303 private: 234 private:
304 StringTypeAdapter<String> m_adapter; 235 StringTypeAdapter<String> m_adapter;
305 }; 236 };
306 237
307 inline void sumWithOverflow(unsigned& total, unsigned addend, bool& overflow) 238 inline void sumWithOverflow(unsigned& total, unsigned addend, bool& overflow)
308 { 239 {
309 unsigned oldTotal = total; 240 unsigned oldTotal = total;
310 total = oldTotal + addend; 241 total = oldTotal + addend;
311 if (total < oldTotal) 242 if (total < oldTotal)
312 overflow = true; 243 overflow = true;
313 } 244 }
314 245
315 template<typename StringType1, typename StringType2> 246 template<typename StringType1, typename StringType2>
316 PassRefPtr<StringImpl> makeString(StringType1 string1, StringType2 string2) 247 PassRefPtr<StringImpl> makeString(const StringTypeAdapter<StringType1>& adapter1 , const StringTypeAdapter<StringType2>& adapter2)
317 { 248 {
318 StringTypeAdapter<StringType1> adapter1(string1);
319 StringTypeAdapter<StringType2> adapter2(string2);
320
321 bool overflow = false; 249 bool overflow = false;
322 unsigned length = adapter1.length(); 250 unsigned length = adapter1.length();
323 sumWithOverflow(length, adapter2.length(), overflow); 251 sumWithOverflow(length, adapter2.length(), overflow);
324 if (overflow) 252 if (overflow)
325 return nullptr; 253 return nullptr;
326 254
327 if (adapter1.is8Bit() && adapter2.is8Bit()) { 255 if (adapter1.is8Bit() && adapter2.is8Bit()) {
328 LChar* buffer; 256 LChar* buffer;
329 RefPtr<StringImpl> resultImpl = StringImpl::createUninitialized(length, buffer); 257 RefPtr<StringImpl> resultImpl = StringImpl::createUninitialized(length, buffer);
330 if (!resultImpl) 258 if (!resultImpl)
(...skipping 17 matching lines...) Expand all
348 result += adapter1.length(); 276 result += adapter1.length();
349 adapter2.writeTo(result); 277 adapter2.writeTo(result);
350 278
351 return resultImpl.release(); 279 return resultImpl.release();
352 } 280 }
353 281
354 } // namespace WTF 282 } // namespace WTF
355 283
356 #include "wtf/text/StringOperators.h" 284 #include "wtf/text/StringOperators.h"
357 #endif 285 #endif
OLDNEW
« no previous file with comments | « no previous file | third_party/WebKit/Source/wtf/text/StringConcatenate.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698