OLD | NEW |
---|---|
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 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
108 *destination = static_cast<LChar>(m_buffer); | 108 *destination = static_cast<LChar>(m_buffer); |
109 } | 109 } |
110 | 110 |
111 void writeTo(UChar* destination) { *destination = m_buffer; } | 111 void writeTo(UChar* destination) { *destination = m_buffer; } |
112 | 112 |
113 private: | 113 private: |
114 UChar m_buffer; | 114 UChar m_buffer; |
115 }; | 115 }; |
116 | 116 |
117 template<> | 117 template<> |
118 class StringTypeAdapter<char*> { | 118 class WTF_EXPORT StringTypeAdapter<char*> { |
119 public: | 119 public: |
120 StringTypeAdapter<char*>(char* buffer) | 120 StringTypeAdapter<char*>(char* buffer) |
121 : m_buffer(buffer) | 121 : m_buffer(buffer) |
122 , m_length(strlen(buffer)) | 122 , m_length(strlen(buffer)) |
123 { | 123 { |
124 } | 124 } |
125 | 125 |
126 unsigned length() { return m_length; } | 126 unsigned length() { return m_length; } |
127 | 127 |
128 bool is8Bit() { return true; } | 128 bool is8Bit() { return true; } |
129 | 129 |
130 void writeTo(LChar* destination) | 130 void writeTo(LChar* destination); |
131 { | |
132 for (unsigned i = 0; i < m_length; ++i) | |
133 destination[i] = static_cast<LChar>(m_buffer[i]); | |
134 } | |
135 | 131 |
136 void writeTo(UChar* destination) | 132 void writeTo(UChar* destination); |
137 { | |
138 for (unsigned i = 0; i < m_length; ++i) { | |
139 unsigned char c = m_buffer[i]; | |
140 destination[i] = c; | |
141 } | |
142 } | |
143 | 133 |
144 private: | 134 private: |
145 const char* m_buffer; | 135 const char* m_buffer; |
146 unsigned m_length; | 136 unsigned m_length; |
147 }; | 137 }; |
148 | 138 |
149 template<> | 139 template<> |
150 class StringTypeAdapter<LChar*> { | 140 class WTF_EXPORT StringTypeAdapter<LChar*> { |
151 public: | 141 public: |
152 StringTypeAdapter<LChar*>(LChar* buffer) | 142 StringTypeAdapter<LChar*>(LChar* buffer); |
153 : m_buffer(buffer) | |
154 , m_length(strlen(reinterpret_cast<char*>(buffer))) | |
155 { | |
156 } | |
157 | 143 |
158 unsigned length() { return m_length; } | 144 unsigned length() { return m_length; } |
159 | 145 |
160 bool is8Bit() { return true; } | 146 bool is8Bit() { return true; } |
161 | 147 |
162 void writeTo(LChar* destination) | 148 void writeTo(LChar* destination); |
163 { | |
164 memcpy(destination, m_buffer, m_length * sizeof(LChar)); | |
165 } | |
166 | 149 |
167 void writeTo(UChar* destination) | 150 void writeTo(UChar* destination); |
168 { | |
169 StringImpl::copyChars(destination, m_buffer, m_length); | |
170 } | |
171 | 151 |
172 private: | 152 private: |
173 const LChar* m_buffer; | 153 const LChar* m_buffer; |
174 unsigned m_length; | 154 unsigned m_length; |
175 }; | 155 }; |
176 | 156 |
177 template<> | 157 template<> |
178 class StringTypeAdapter<const UChar*> { | 158 class WTF_EXPORT StringTypeAdapter<const UChar*> { |
179 public: | 159 public: |
180 StringTypeAdapter<const UChar*>(const UChar* buffer) | 160 StringTypeAdapter(const UChar* buffer); |
181 : m_buffer(buffer) | |
182 { | |
183 size_t len = 0; | |
184 while (m_buffer[len] != UChar(0)) | |
185 ++len; | |
186 | |
187 RELEASE_ASSERT(len <= std::numeric_limits<unsigned>::max()); | |
188 | |
189 m_length = len; | |
190 } | |
191 | 161 |
192 unsigned length() { return m_length; } | 162 unsigned length() { return m_length; } |
193 | 163 |
194 bool is8Bit() { return false; } | 164 bool is8Bit() { return false; } |
195 | 165 |
196 NO_RETURN_DUE_TO_CRASH void writeTo(LChar*) | 166 NO_RETURN_DUE_TO_CRASH void writeTo(LChar*) |
197 { | 167 { |
198 RELEASE_ASSERT(false); | 168 RELEASE_ASSERT(false); |
199 } | 169 } |
200 | 170 |
201 void writeTo(UChar* destination) | 171 void writeTo(UChar* destination); |
202 { | |
203 memcpy(destination, m_buffer, m_length * sizeof(UChar)); | |
204 } | |
205 | 172 |
206 private: | 173 private: |
207 const UChar* m_buffer; | 174 const UChar* m_buffer; |
208 unsigned m_length; | 175 unsigned m_length; |
209 }; | 176 }; |
210 | 177 |
211 template<> | 178 template<> |
212 class StringTypeAdapter<const char*> { | 179 class WTF_EXPORT StringTypeAdapter<const char*> { |
213 public: | 180 public: |
214 StringTypeAdapter<const char*>(const char* buffer) | 181 StringTypeAdapter<const char*>(const char* buffer); |
215 : m_buffer(buffer) | |
216 , m_length(strlen(buffer)) | |
217 { | |
218 } | |
219 | 182 |
220 unsigned length() { return m_length; } | 183 unsigned length() { return m_length; } |
221 | 184 |
222 bool is8Bit() { return true; } | 185 bool is8Bit() { return true; } |
223 | 186 |
224 void writeTo(LChar* destination) | 187 void writeTo(LChar* destination); |
225 { | |
226 memcpy(destination, m_buffer, static_cast<size_t>(m_length) * sizeof(LCh ar)); | |
227 } | |
228 | 188 |
229 void writeTo(UChar* destination) | 189 void writeTo(UChar* destination); |
230 { | |
231 for (unsigned i = 0; i < m_length; ++i) { | |
232 unsigned char c = m_buffer[i]; | |
233 destination[i] = c; | |
234 } | |
235 } | |
236 | 190 |
237 private: | 191 private: |
238 const char* m_buffer; | 192 const char* m_buffer; |
239 unsigned m_length; | 193 unsigned m_length; |
240 }; | 194 }; |
241 | 195 |
242 template<> | 196 template<> |
243 class StringTypeAdapter<const LChar*> { | 197 class WTF_EXPORT StringTypeAdapter<const LChar*> { |
244 public: | 198 public: |
245 StringTypeAdapter<const LChar*>(const LChar* buffer) | 199 StringTypeAdapter<const LChar*>(const LChar* buffer); |
246 : m_buffer(buffer) | |
247 , m_length(strlen(reinterpret_cast<const char*>(buffer))) | |
248 { | |
249 } | |
250 | 200 |
251 unsigned length() { return m_length; } | 201 unsigned length() { return m_length; } |
252 | 202 |
253 bool is8Bit() { return true; } | 203 bool is8Bit() { return true; } |
254 | 204 |
255 void writeTo(LChar* destination) | 205 void writeTo(LChar* destination); |
256 { | |
257 memcpy(destination, m_buffer, static_cast<size_t>(m_length) * sizeof(LCh ar)); | |
258 } | |
259 | 206 |
260 void writeTo(UChar* destination) | 207 void writeTo(UChar* destination); |
261 { | |
262 StringImpl::copyChars(destination, m_buffer, m_length); | |
263 } | |
264 | 208 |
265 private: | 209 private: |
266 const LChar* m_buffer; | 210 const LChar* m_buffer; |
267 unsigned m_length; | 211 unsigned m_length; |
268 }; | 212 }; |
269 | 213 |
270 template<> | 214 template<> |
271 class StringTypeAdapter<Vector<char> > { | 215 class WTF_EXPORT StringTypeAdapter<Vector<char> > { |
272 public: | 216 public: |
273 StringTypeAdapter<Vector<char> >(const Vector<char>& buffer) | 217 StringTypeAdapter<Vector<char> >(const Vector<char>& buffer) |
274 : m_buffer(buffer) | 218 : m_buffer(buffer) |
275 { | 219 { |
276 } | 220 } |
277 | 221 |
278 size_t length() { return m_buffer.size(); } | 222 size_t length() { return m_buffer.size(); } |
279 | 223 |
280 bool is8Bit() { return true; } | 224 bool is8Bit() { return true; } |
281 | 225 |
282 void writeTo(LChar* destination) | 226 void writeTo(LChar* destination); |
283 { | |
284 for (size_t i = 0; i < m_buffer.size(); ++i) | |
285 destination[i] = static_cast<unsigned char>(m_buffer[i]); | |
286 } | |
287 | 227 |
288 void writeTo(UChar* destination) | 228 void writeTo(UChar* destination); |
289 { | |
290 for (size_t i = 0; i < m_buffer.size(); ++i) | |
291 destination[i] = static_cast<unsigned char>(m_buffer[i]); | |
292 } | |
293 | 229 |
294 private: | 230 private: |
295 const Vector<char>& m_buffer; | 231 const Vector<char>& m_buffer; |
296 }; | 232 }; |
297 | 233 |
298 template<> | 234 template<> |
299 class StringTypeAdapter<Vector<LChar> > { | 235 class StringTypeAdapter<Vector<LChar> > { |
300 public: | 236 public: |
301 StringTypeAdapter<Vector<LChar> >(const Vector<LChar>& buffer) | 237 StringTypeAdapter<Vector<LChar> >(const Vector<LChar>& buffer) |
302 : m_buffer(buffer) | 238 : m_buffer(buffer) |
303 { | 239 { |
304 } | 240 } |
305 | 241 |
306 size_t length() { return m_buffer.size(); } | 242 size_t length() { return m_buffer.size(); } |
307 | 243 |
308 bool is8Bit() { return true; } | 244 bool is8Bit() { return true; } |
309 | 245 |
310 void writeTo(LChar* destination) | 246 void writeTo(LChar* destination); |
311 { | |
312 for (size_t i = 0; i < m_buffer.size(); ++i) | |
313 destination[i] = m_buffer[i]; | |
314 } | |
315 | 247 |
316 void writeTo(UChar* destination) | 248 void writeTo(UChar* destination); |
317 { | |
318 for (size_t i = 0; i < m_buffer.size(); ++i) | |
319 destination[i] = m_buffer[i]; | |
320 } | |
321 | 249 |
322 private: | 250 private: |
323 const Vector<LChar>& m_buffer; | 251 const Vector<LChar>& m_buffer; |
324 }; | 252 }; |
325 | 253 |
326 template<> | 254 template<> |
327 class StringTypeAdapter<String> { | 255 class WTF_EXPORT StringTypeAdapter<String> { |
328 public: | 256 public: |
329 StringTypeAdapter<String>(const String& string) | 257 StringTypeAdapter<String>(const String& string) |
330 : m_buffer(string) | 258 : m_buffer(string) |
331 { | 259 { |
332 } | 260 } |
333 | 261 |
334 unsigned length() { return m_buffer.length(); } | 262 unsigned length() { return m_buffer.length(); } |
335 | 263 |
336 bool is8Bit() { return m_buffer.isNull() || m_buffer.is8Bit(); } | 264 bool is8Bit() { return m_buffer.isNull() || m_buffer.is8Bit(); } |
337 | 265 |
338 void writeTo(LChar* destination) | 266 void writeTo(LChar* destination); |
339 { | |
340 unsigned length = m_buffer.length(); | |
341 | 267 |
342 ASSERT(is8Bit()); | 268 void writeTo(UChar* destination); |
343 const LChar* data = m_buffer.characters8(); | |
344 for (unsigned i = 0; i < length; ++i) | |
345 destination[i] = data[i]; | |
346 | |
347 WTF_STRINGTYPEADAPTER_COPIED_WTF_STRING(); | |
Nico
2016/10/01 16:47:29
FWIW, this made parts of StringOperatorsTest.cpp f
| |
348 } | |
349 | |
350 void writeTo(UChar* destination) | |
351 { | |
352 unsigned length = m_buffer.length(); | |
353 | |
354 if (is8Bit()) { | |
355 const LChar* data = m_buffer.characters8(); | |
356 for (unsigned i = 0; i < length; ++i) | |
357 destination[i] = data[i]; | |
358 } else { | |
359 const UChar* data = m_buffer.characters16(); | |
360 for (unsigned i = 0; i < length; ++i) | |
361 destination[i] = data[i]; | |
362 } | |
363 | |
364 WTF_STRINGTYPEADAPTER_COPIED_WTF_STRING(); | |
365 } | |
366 | 269 |
367 private: | 270 private: |
368 const String& m_buffer; | 271 const String& m_buffer; |
369 }; | 272 }; |
370 | 273 |
371 template<> | 274 template<> |
372 class StringTypeAdapter<AtomicString> { | 275 class StringTypeAdapter<AtomicString> { |
373 public: | 276 public: |
374 StringTypeAdapter<AtomicString>(const AtomicString& string) | 277 StringTypeAdapter<AtomicString>(const AtomicString& string) |
375 : m_adapter(string.string()) | 278 : m_adapter(string.string()) |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
431 result += adapter1.length(); | 334 result += adapter1.length(); |
432 adapter2.writeTo(result); | 335 adapter2.writeTo(result); |
433 | 336 |
434 return resultImpl.release(); | 337 return resultImpl.release(); |
435 } | 338 } |
436 | 339 |
437 } // namespace WTF | 340 } // namespace WTF |
438 | 341 |
439 #include "wtf/text/StringOperators.h" | 342 #include "wtf/text/StringOperators.h" |
440 #endif | 343 #endif |
OLD | NEW |