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