| 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 23 matching lines...) Expand all Loading... |
| 34 #endif | 34 #endif |
| 35 | 35 |
| 36 // This macro is helpful for testing how many intermediate Strings are created w
hile evaluating an | 36 // This macro is helpful for testing how many intermediate Strings are created w
hile evaluating an |
| 37 // expression containing operator+. | 37 // expression containing operator+. |
| 38 #ifndef WTF_STRINGTYPEADAPTER_COPIED_WTF_STRING | 38 #ifndef WTF_STRINGTYPEADAPTER_COPIED_WTF_STRING |
| 39 #define WTF_STRINGTYPEADAPTER_COPIED_WTF_STRING() ((void)0) | 39 #define WTF_STRINGTYPEADAPTER_COPIED_WTF_STRING() ((void)0) |
| 40 #endif | 40 #endif |
| 41 | 41 |
| 42 namespace WTF { | 42 namespace WTF { |
| 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 |
| 53 StringTypeAdapter<char>(char buffer) | 53 public: |
| 54 : m_buffer(buffer) | 54 StringTypeAdapter<char>(char buffer) : m_buffer(buffer) {} |
| 55 { | 55 |
| 56 } | 56 unsigned length() { return 1; } |
| 57 | 57 |
| 58 unsigned length() { return 1; } | 58 bool is8Bit() { return true; } |
| 59 | 59 |
| 60 bool is8Bit() { return true; } | 60 void writeTo(LChar* destination) { *destination = m_buffer; } |
| 61 | 61 |
| 62 void writeTo(LChar* destination) | 62 void writeTo(UChar* destination) { *destination = m_buffer; } |
| 63 { | 63 |
| 64 *destination = m_buffer; | 64 private: |
| 65 } | 65 unsigned char m_buffer; |
| 66 | 66 }; |
| 67 void writeTo(UChar* destination) { *destination = m_buffer; } | 67 |
| 68 | 68 template <> |
| 69 private: | |
| 70 unsigned char m_buffer; | |
| 71 }; | |
| 72 | |
| 73 template<> | |
| 74 class StringTypeAdapter<LChar> { | 69 class StringTypeAdapter<LChar> { |
| 75 DISALLOW_NEW(); | 70 DISALLOW_NEW(); |
| 76 public: | 71 |
| 77 StringTypeAdapter<LChar>(LChar buffer) | 72 public: |
| 78 : m_buffer(buffer) | 73 StringTypeAdapter<LChar>(LChar buffer) : m_buffer(buffer) {} |
| 79 { | 74 |
| 80 } | 75 unsigned length() { return 1; } |
| 81 | 76 |
| 82 unsigned length() { return 1; } | 77 bool is8Bit() { return true; } |
| 83 | 78 |
| 84 bool is8Bit() { return true; } | 79 void writeTo(LChar* destination) { *destination = m_buffer; } |
| 85 | 80 |
| 86 void writeTo(LChar* destination) | 81 void writeTo(UChar* destination) { *destination = m_buffer; } |
| 87 { | 82 |
| 88 *destination = m_buffer; | 83 private: |
| 89 } | 84 LChar m_buffer; |
| 90 | 85 }; |
| 91 void writeTo(UChar* destination) { *destination = m_buffer; } | 86 |
| 92 | 87 template <> |
| 93 private: | |
| 94 LChar m_buffer; | |
| 95 }; | |
| 96 | |
| 97 template<> | |
| 98 class StringTypeAdapter<UChar> { | 88 class StringTypeAdapter<UChar> { |
| 99 DISALLOW_NEW(); | 89 DISALLOW_NEW(); |
| 100 public: | 90 |
| 101 StringTypeAdapter<UChar>(UChar buffer) | 91 public: |
| 102 : m_buffer(buffer) | 92 StringTypeAdapter<UChar>(UChar buffer) : m_buffer(buffer) {} |
| 103 { | 93 |
| 104 } | 94 unsigned length() { return 1; } |
| 105 | 95 |
| 106 unsigned length() { return 1; } | 96 bool is8Bit() { return m_buffer <= 0xff; } |
| 107 | 97 |
| 108 bool is8Bit() { return m_buffer <= 0xff; } | 98 void writeTo(LChar* destination) { |
| 109 | 99 ASSERT(is8Bit()); |
| 110 void writeTo(LChar* destination) | 100 *destination = static_cast<LChar>(m_buffer); |
| 111 { | 101 } |
| 112 ASSERT(is8Bit()); | 102 |
| 113 *destination = static_cast<LChar>(m_buffer); | 103 void writeTo(UChar* destination) { *destination = m_buffer; } |
| 114 } | 104 |
| 115 | 105 private: |
| 116 void writeTo(UChar* destination) { *destination = m_buffer; } | 106 UChar m_buffer; |
| 117 | 107 }; |
| 118 private: | 108 |
| 119 UChar m_buffer; | 109 template <> |
| 120 }; | |
| 121 | |
| 122 template<> | |
| 123 class WTF_EXPORT StringTypeAdapter<char*> { | 110 class WTF_EXPORT StringTypeAdapter<char*> { |
| 124 DISALLOW_NEW(); | 111 DISALLOW_NEW(); |
| 125 public: | 112 |
| 126 StringTypeAdapter<char*>(char* buffer) | 113 public: |
| 127 : m_buffer(buffer) | 114 StringTypeAdapter<char*>(char* buffer) |
| 128 , m_length(strlen(buffer)) | 115 : m_buffer(buffer), m_length(strlen(buffer)) {} |
| 129 { | 116 |
| 130 } | 117 unsigned length() { return m_length; } |
| 131 | 118 |
| 132 unsigned length() { return m_length; } | 119 bool is8Bit() { return true; } |
| 133 | 120 |
| 134 bool is8Bit() { return true; } | 121 void writeTo(LChar* destination); |
| 135 | 122 |
| 136 void writeTo(LChar* destination); | 123 void writeTo(UChar* destination); |
| 137 | 124 |
| 138 void writeTo(UChar* destination); | 125 private: |
| 139 | 126 const char* m_buffer; |
| 140 private: | 127 unsigned m_length; |
| 141 const char* m_buffer; | 128 }; |
| 142 unsigned m_length; | 129 |
| 143 }; | 130 template <> |
| 144 | |
| 145 template<> | |
| 146 class WTF_EXPORT StringTypeAdapter<LChar*> { | 131 class WTF_EXPORT StringTypeAdapter<LChar*> { |
| 147 DISALLOW_NEW(); | 132 DISALLOW_NEW(); |
| 148 public: | 133 |
| 149 StringTypeAdapter<LChar*>(LChar* buffer); | 134 public: |
| 150 | 135 StringTypeAdapter<LChar*>(LChar* buffer); |
| 151 unsigned length() { return m_length; } | 136 |
| 152 | 137 unsigned length() { return m_length; } |
| 153 bool is8Bit() { return true; } | 138 |
| 154 | 139 bool is8Bit() { return true; } |
| 155 void writeTo(LChar* destination); | 140 |
| 156 | 141 void writeTo(LChar* destination); |
| 157 void writeTo(UChar* destination); | 142 |
| 158 | 143 void writeTo(UChar* destination); |
| 159 private: | 144 |
| 160 const LChar* m_buffer; | 145 private: |
| 161 unsigned m_length; | 146 const LChar* m_buffer; |
| 162 }; | 147 unsigned m_length; |
| 163 | 148 }; |
| 164 template<> | 149 |
| 150 template <> |
| 165 class WTF_EXPORT StringTypeAdapter<const UChar*> { | 151 class WTF_EXPORT StringTypeAdapter<const UChar*> { |
| 166 DISALLOW_NEW(); | 152 DISALLOW_NEW(); |
| 167 public: | 153 |
| 168 StringTypeAdapter(const UChar* buffer); | 154 public: |
| 169 | 155 StringTypeAdapter(const UChar* buffer); |
| 170 unsigned length() { return m_length; } | 156 |
| 171 | 157 unsigned length() { return m_length; } |
| 172 bool is8Bit() { return false; } | 158 |
| 173 | 159 bool is8Bit() { return false; } |
| 174 NO_RETURN_DUE_TO_CRASH void writeTo(LChar*) | 160 |
| 175 { | 161 NO_RETURN_DUE_TO_CRASH void writeTo(LChar*) { RELEASE_ASSERT(false); } |
| 176 RELEASE_ASSERT(false); | 162 |
| 177 } | 163 void writeTo(UChar* destination); |
| 178 | 164 |
| 179 void writeTo(UChar* destination); | 165 private: |
| 180 | 166 const UChar* m_buffer; |
| 181 private: | 167 unsigned m_length; |
| 182 const UChar* m_buffer; | 168 }; |
| 183 unsigned m_length; | 169 |
| 184 }; | 170 template <> |
| 185 | |
| 186 template<> | |
| 187 class WTF_EXPORT StringTypeAdapter<const char*> { | 171 class WTF_EXPORT StringTypeAdapter<const char*> { |
| 188 DISALLOW_NEW(); | 172 DISALLOW_NEW(); |
| 189 public: | 173 |
| 190 StringTypeAdapter<const char*>(const char* buffer); | 174 public: |
| 191 | 175 StringTypeAdapter<const char*>(const char* buffer); |
| 192 unsigned length() { return m_length; } | 176 |
| 193 | 177 unsigned length() { return m_length; } |
| 194 bool is8Bit() { return true; } | 178 |
| 195 | 179 bool is8Bit() { return true; } |
| 196 void writeTo(LChar* destination); | 180 |
| 197 | 181 void writeTo(LChar* destination); |
| 198 void writeTo(UChar* destination); | 182 |
| 199 | 183 void writeTo(UChar* destination); |
| 200 private: | 184 |
| 201 const char* m_buffer; | 185 private: |
| 202 unsigned m_length; | 186 const char* m_buffer; |
| 203 }; | 187 unsigned m_length; |
| 204 | 188 }; |
| 205 template<> | 189 |
| 190 template <> |
| 206 class WTF_EXPORT StringTypeAdapter<const LChar*> { | 191 class WTF_EXPORT StringTypeAdapter<const LChar*> { |
| 207 DISALLOW_NEW(); | 192 DISALLOW_NEW(); |
| 208 public: | 193 |
| 209 StringTypeAdapter<const LChar*>(const LChar* buffer); | 194 public: |
| 210 | 195 StringTypeAdapter<const LChar*>(const LChar* buffer); |
| 211 unsigned length() { return m_length; } | 196 |
| 212 | 197 unsigned length() { return m_length; } |
| 213 bool is8Bit() { return true; } | 198 |
| 214 | 199 bool is8Bit() { return true; } |
| 215 void writeTo(LChar* destination); | 200 |
| 216 | 201 void writeTo(LChar* destination); |
| 217 void writeTo(UChar* destination); | 202 |
| 218 | 203 void writeTo(UChar* destination); |
| 219 private: | 204 |
| 220 const LChar* m_buffer; | 205 private: |
| 221 unsigned m_length; | 206 const LChar* m_buffer; |
| 222 }; | 207 unsigned m_length; |
| 223 | 208 }; |
| 224 template<> | 209 |
| 210 template <> |
| 225 class WTF_EXPORT StringTypeAdapter<Vector<char>> { | 211 class WTF_EXPORT StringTypeAdapter<Vector<char>> { |
| 226 DISALLOW_NEW(); | 212 DISALLOW_NEW(); |
| 227 public: | 213 |
| 228 StringTypeAdapter<Vector<char>>(const Vector<char>& buffer) | 214 public: |
| 229 : m_buffer(buffer) | 215 StringTypeAdapter<Vector<char>>(const Vector<char>& buffer) |
| 230 { | 216 : m_buffer(buffer) {} |
| 231 } | 217 |
| 232 | 218 size_t length() { return m_buffer.size(); } |
| 233 size_t length() { return m_buffer.size(); } | 219 |
| 234 | 220 bool is8Bit() { return true; } |
| 235 bool is8Bit() { return true; } | 221 |
| 236 | 222 void writeTo(LChar* destination); |
| 237 void writeTo(LChar* destination); | 223 |
| 238 | 224 void writeTo(UChar* destination); |
| 239 void writeTo(UChar* destination); | 225 |
| 240 | 226 private: |
| 241 private: | 227 const Vector<char>& m_buffer; |
| 242 const Vector<char>& m_buffer; | 228 }; |
| 243 }; | 229 |
| 244 | 230 template <> |
| 245 template<> | |
| 246 class StringTypeAdapter<Vector<LChar>> { | 231 class StringTypeAdapter<Vector<LChar>> { |
| 247 DISALLOW_NEW(); | 232 DISALLOW_NEW(); |
| 248 public: | 233 |
| 249 StringTypeAdapter<Vector<LChar>>(const Vector<LChar>& buffer) | 234 public: |
| 250 : m_buffer(buffer) | 235 StringTypeAdapter<Vector<LChar>>(const Vector<LChar>& buffer) |
| 251 { | 236 : m_buffer(buffer) {} |
| 252 } | 237 |
| 253 | 238 size_t length() { return m_buffer.size(); } |
| 254 size_t length() { return m_buffer.size(); } | 239 |
| 255 | 240 bool is8Bit() { return true; } |
| 256 bool is8Bit() { return true; } | 241 |
| 257 | 242 void writeTo(LChar* destination); |
| 258 void writeTo(LChar* destination); | 243 |
| 259 | 244 void writeTo(UChar* destination); |
| 260 void writeTo(UChar* destination); | 245 |
| 261 | 246 private: |
| 262 private: | 247 const Vector<LChar>& m_buffer; |
| 263 const Vector<LChar>& m_buffer; | 248 }; |
| 264 }; | 249 |
| 265 | 250 template <> |
| 266 template<> | |
| 267 class WTF_EXPORT StringTypeAdapter<String> { | 251 class WTF_EXPORT StringTypeAdapter<String> { |
| 268 DISALLOW_NEW(); | 252 DISALLOW_NEW(); |
| 269 public: | 253 |
| 270 StringTypeAdapter<String>(const String& string) | 254 public: |
| 271 : m_buffer(string) | 255 StringTypeAdapter<String>(const String& string) : m_buffer(string) {} |
| 272 { | 256 |
| 273 } | 257 unsigned length() { return m_buffer.length(); } |
| 274 | 258 |
| 275 unsigned length() { return m_buffer.length(); } | 259 bool is8Bit() { return m_buffer.isNull() || m_buffer.is8Bit(); } |
| 276 | 260 |
| 277 bool is8Bit() { return m_buffer.isNull() || m_buffer.is8Bit(); } | 261 void writeTo(LChar* destination); |
| 278 | 262 |
| 279 void writeTo(LChar* destination); | 263 void writeTo(UChar* destination); |
| 280 | 264 |
| 281 void writeTo(UChar* destination); | 265 private: |
| 282 | 266 const String& m_buffer; |
| 283 private: | 267 }; |
| 284 const String& m_buffer; | 268 |
| 285 }; | 269 template <> |
| 286 | |
| 287 template<> | |
| 288 class StringTypeAdapter<AtomicString> { | 270 class StringTypeAdapter<AtomicString> { |
| 289 DISALLOW_NEW(); | 271 DISALLOW_NEW(); |
| 290 public: | 272 |
| 291 StringTypeAdapter<AtomicString>(const AtomicString& string) | 273 public: |
| 292 : m_adapter(string.string()) | 274 StringTypeAdapter<AtomicString>(const AtomicString& string) |
| 293 { | 275 : m_adapter(string.string()) {} |
| 294 } | 276 |
| 295 | 277 unsigned length() { return m_adapter.length(); } |
| 296 unsigned length() { return m_adapter.length(); } | 278 |
| 297 | 279 bool is8Bit() { return m_adapter.is8Bit(); } |
| 298 bool is8Bit() { return m_adapter.is8Bit(); } | 280 |
| 299 | 281 void writeTo(LChar* destination) { m_adapter.writeTo(destination); } |
| 300 void writeTo(LChar* destination) { m_adapter.writeTo(destination); } | 282 void writeTo(UChar* destination) { m_adapter.writeTo(destination); } |
| 301 void writeTo(UChar* destination) { m_adapter.writeTo(destination); } | 283 |
| 302 | 284 private: |
| 303 private: | 285 StringTypeAdapter<String> m_adapter; |
| 304 StringTypeAdapter<String> m_adapter; | 286 }; |
| 305 }; | 287 |
| 306 | 288 inline void sumWithOverflow(unsigned& total, unsigned addend, bool& overflow) { |
| 307 inline void sumWithOverflow(unsigned& total, unsigned addend, bool& overflow) | 289 unsigned oldTotal = total; |
| 308 { | 290 total = oldTotal + addend; |
| 309 unsigned oldTotal = total; | 291 if (total < oldTotal) |
| 310 total = oldTotal + addend; | 292 overflow = true; |
| 311 if (total < oldTotal) | |
| 312 overflow = true; | |
| 313 } | 293 } |
| 314 | 294 |
| 315 template<typename StringType1, typename StringType2> | 295 template <typename StringType1, typename StringType2> |
| 316 PassRefPtr<StringImpl> makeString(StringType1 string1, StringType2 string2) | 296 PassRefPtr<StringImpl> makeString(StringType1 string1, StringType2 string2) { |
| 317 { | 297 StringTypeAdapter<StringType1> adapter1(string1); |
| 318 StringTypeAdapter<StringType1> adapter1(string1); | 298 StringTypeAdapter<StringType2> adapter2(string2); |
| 319 StringTypeAdapter<StringType2> adapter2(string2); | 299 |
| 320 | 300 bool overflow = false; |
| 321 bool overflow = false; | 301 unsigned length = adapter1.length(); |
| 322 unsigned length = adapter1.length(); | 302 sumWithOverflow(length, adapter2.length(), overflow); |
| 323 sumWithOverflow(length, adapter2.length(), overflow); | 303 if (overflow) |
| 324 if (overflow) | 304 return nullptr; |
| 325 return nullptr; | 305 |
| 326 | 306 if (adapter1.is8Bit() && adapter2.is8Bit()) { |
| 327 if (adapter1.is8Bit() && adapter2.is8Bit()) { | 307 LChar* buffer; |
| 328 LChar* buffer; | 308 RefPtr<StringImpl> resultImpl = |
| 329 RefPtr<StringImpl> resultImpl = StringImpl::createUninitialized(length,
buffer); | 309 StringImpl::createUninitialized(length, buffer); |
| 330 if (!resultImpl) | |
| 331 return nullptr; | |
| 332 | |
| 333 LChar* result = buffer; | |
| 334 adapter1.writeTo(result); | |
| 335 result += adapter1.length(); | |
| 336 adapter2.writeTo(result); | |
| 337 | |
| 338 return resultImpl.release(); | |
| 339 } | |
| 340 | |
| 341 UChar* buffer; | |
| 342 RefPtr<StringImpl> resultImpl = StringImpl::createUninitialized(length, buff
er); | |
| 343 if (!resultImpl) | 310 if (!resultImpl) |
| 344 return nullptr; | 311 return nullptr; |
| 345 | 312 |
| 346 UChar* result = buffer; | 313 LChar* result = buffer; |
| 347 adapter1.writeTo(result); | 314 adapter1.writeTo(result); |
| 348 result += adapter1.length(); | 315 result += adapter1.length(); |
| 349 adapter2.writeTo(result); | 316 adapter2.writeTo(result); |
| 350 | 317 |
| 351 return resultImpl.release(); | 318 return resultImpl.release(); |
| 319 } |
| 320 |
| 321 UChar* buffer; |
| 322 RefPtr<StringImpl> resultImpl = |
| 323 StringImpl::createUninitialized(length, buffer); |
| 324 if (!resultImpl) |
| 325 return nullptr; |
| 326 |
| 327 UChar* result = buffer; |
| 328 adapter1.writeTo(result); |
| 329 result += adapter1.length(); |
| 330 adapter2.writeTo(result); |
| 331 |
| 332 return resultImpl.release(); |
| 352 } | 333 } |
| 353 | 334 |
| 354 } // namespace WTF | 335 } // namespace WTF |
| 355 | 336 |
| 356 #include "wtf/text/StringOperators.h" | 337 #include "wtf/text/StringOperators.h" |
| 357 #endif | 338 #endif |
| OLD | NEW |