| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2009, 2010, 2012, 2013 Apple Inc. All rights reserved. | |
| 3 * Copyright (C) 2012 Google Inc. All rights reserved. | |
| 4 * | |
| 5 * Redistribution and use in source and binary forms, with or without | |
| 6 * modification, are permitted provided that the following conditions | |
| 7 * are met: | |
| 8 * 1. Redistributions of source code must retain the above copyright | |
| 9 * notice, this list of conditions and the following disclaimer. | |
| 10 * 2. Redistributions in binary form must reproduce the above copyright | |
| 11 * notice, this list of conditions and the following disclaimer in the | |
| 12 * documentation and/or other materials provided with the distribution. | |
| 13 * | |
| 14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY | |
| 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
| 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR | |
| 18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
| 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 25 */ | |
| 26 | |
| 27 #ifndef StringBuilder_h | |
| 28 #define StringBuilder_h | |
| 29 | |
| 30 #include <wtf/text/AtomicString.h> | |
| 31 #include <wtf/text/WTFString.h> | |
| 32 | |
| 33 namespace WTF { | |
| 34 | |
| 35 class StringBuilder { | |
| 36 // Disallow copying since it's expensive and we don't want code to do it by
accident. | |
| 37 WTF_MAKE_NONCOPYABLE(StringBuilder); | |
| 38 | |
| 39 public: | |
| 40 StringBuilder() | |
| 41 : m_length(0) | |
| 42 , m_is8Bit(true) | |
| 43 , m_valid16BitShadowLength(0) | |
| 44 , m_bufferCharacters8(0) | |
| 45 { | |
| 46 } | |
| 47 | |
| 48 WTF_EXPORT_PRIVATE void append(const UChar*, unsigned); | |
| 49 WTF_EXPORT_PRIVATE void append(const LChar*, unsigned); | |
| 50 | |
| 51 ALWAYS_INLINE void append(const char* characters, unsigned length) { append(
reinterpret_cast<const LChar*>(characters), length); } | |
| 52 | |
| 53 void append(const String& string) | |
| 54 { | |
| 55 if (!string.length()) | |
| 56 return; | |
| 57 | |
| 58 // If we're appending to an empty string, and there is not a buffer (res
erveCapacity has not been called) | |
| 59 // then just retain the string. | |
| 60 if (!m_length && !m_buffer) { | |
| 61 m_string = string; | |
| 62 m_length = string.length(); | |
| 63 m_is8Bit = m_string.is8Bit(); | |
| 64 return; | |
| 65 } | |
| 66 | |
| 67 if (string.is8Bit()) | |
| 68 append(string.characters8(), string.length()); | |
| 69 else | |
| 70 append(string.characters16(), string.length()); | |
| 71 } | |
| 72 | |
| 73 void append(const StringBuilder& other) | |
| 74 { | |
| 75 if (!other.m_length) | |
| 76 return; | |
| 77 | |
| 78 // If we're appending to an empty string, and there is not a buffer (res
erveCapacity has not been called) | |
| 79 // then just retain the string. | |
| 80 if (!m_length && !m_buffer && !other.m_string.isNull()) { | |
| 81 m_string = other.m_string; | |
| 82 m_length = other.m_length; | |
| 83 return; | |
| 84 } | |
| 85 | |
| 86 if (other.is8Bit()) | |
| 87 append(other.characters8(), other.m_length); | |
| 88 else | |
| 89 append(other.characters16(), other.m_length); | |
| 90 } | |
| 91 | |
| 92 void append(const String& string, unsigned offset, unsigned length) | |
| 93 { | |
| 94 if (!string.length()) | |
| 95 return; | |
| 96 | |
| 97 if ((offset + length) > string.length()) | |
| 98 return; | |
| 99 | |
| 100 if (string.is8Bit()) | |
| 101 append(string.characters8() + offset, length); | |
| 102 else | |
| 103 append(string.characters16() + offset, length); | |
| 104 } | |
| 105 | |
| 106 void append(const char* characters) | |
| 107 { | |
| 108 if (characters) | |
| 109 append(characters, strlen(characters)); | |
| 110 } | |
| 111 | |
| 112 void append(UChar c) | |
| 113 { | |
| 114 if (m_buffer && m_length < m_buffer->length() && m_string.isNull()) { | |
| 115 if (!m_is8Bit) { | |
| 116 m_bufferCharacters16[m_length++] = c; | |
| 117 return; | |
| 118 } | |
| 119 | |
| 120 if (!(c & ~0xff)) { | |
| 121 m_bufferCharacters8[m_length++] = static_cast<LChar>(c); | |
| 122 return; | |
| 123 } | |
| 124 } | |
| 125 append(&c, 1); | |
| 126 } | |
| 127 | |
| 128 void append(LChar c) | |
| 129 { | |
| 130 if (m_buffer && m_length < m_buffer->length() && m_string.isNull()) { | |
| 131 if (m_is8Bit) | |
| 132 m_bufferCharacters8[m_length++] = c; | |
| 133 else | |
| 134 m_bufferCharacters16[m_length++] = c; | |
| 135 } else | |
| 136 append(&c, 1); | |
| 137 } | |
| 138 | |
| 139 void append(char c) | |
| 140 { | |
| 141 append(static_cast<LChar>(c)); | |
| 142 } | |
| 143 | |
| 144 void append(UChar32 c) | |
| 145 { | |
| 146 if (U_IS_BMP(c)) { | |
| 147 append(static_cast<UChar>(c)); | |
| 148 return; | |
| 149 } | |
| 150 append(U16_LEAD(c)); | |
| 151 append(U16_TRAIL(c)); | |
| 152 } | |
| 153 | |
| 154 template<unsigned charactersCount> | |
| 155 ALWAYS_INLINE void appendLiteral(const char (&characters)[charactersCount])
{ append(characters, charactersCount - 1); } | |
| 156 | |
| 157 WTF_EXPORT_PRIVATE void appendNumber(int); | |
| 158 WTF_EXPORT_PRIVATE void appendNumber(unsigned int); | |
| 159 WTF_EXPORT_PRIVATE void appendNumber(long); | |
| 160 WTF_EXPORT_PRIVATE void appendNumber(unsigned long); | |
| 161 WTF_EXPORT_PRIVATE void appendNumber(long long); | |
| 162 WTF_EXPORT_PRIVATE void appendNumber(unsigned long long); | |
| 163 | |
| 164 String toString() | |
| 165 { | |
| 166 shrinkToFit(); | |
| 167 if (m_string.isNull()) | |
| 168 reifyString(); | |
| 169 return m_string; | |
| 170 } | |
| 171 | |
| 172 const String& toStringPreserveCapacity() const | |
| 173 { | |
| 174 if (m_string.isNull()) | |
| 175 reifyString(); | |
| 176 return m_string; | |
| 177 } | |
| 178 | |
| 179 AtomicString toAtomicString() const | |
| 180 { | |
| 181 if (!m_length) | |
| 182 return emptyAtom; | |
| 183 | |
| 184 // If the buffer is sufficiently over-allocated, make a new AtomicString
from a copy so its buffer is not so large. | |
| 185 if (canShrink()) { | |
| 186 if (is8Bit()) | |
| 187 return AtomicString(characters8(), length()); | |
| 188 return AtomicString(characters16(), length()); | |
| 189 } | |
| 190 | |
| 191 if (!m_string.isNull()) | |
| 192 return AtomicString(m_string); | |
| 193 | |
| 194 ASSERT(m_buffer); | |
| 195 return AtomicString(m_buffer.get(), 0, m_length); | |
| 196 } | |
| 197 | |
| 198 unsigned length() const | |
| 199 { | |
| 200 return m_length; | |
| 201 } | |
| 202 | |
| 203 bool isEmpty() const { return !m_length; } | |
| 204 | |
| 205 WTF_EXPORT_PRIVATE void reserveCapacity(unsigned newCapacity); | |
| 206 | |
| 207 unsigned capacity() const | |
| 208 { | |
| 209 return m_buffer ? m_buffer->length() : m_length; | |
| 210 } | |
| 211 | |
| 212 WTF_EXPORT_PRIVATE void resize(unsigned newSize); | |
| 213 | |
| 214 WTF_EXPORT_PRIVATE bool canShrink() const; | |
| 215 | |
| 216 WTF_EXPORT_PRIVATE void shrinkToFit(); | |
| 217 | |
| 218 UChar operator[](unsigned i) const | |
| 219 { | |
| 220 ASSERT_WITH_SECURITY_IMPLICATION(i < m_length); | |
| 221 if (m_is8Bit) | |
| 222 return characters8()[i]; | |
| 223 return characters16()[i]; | |
| 224 } | |
| 225 | |
| 226 const LChar* characters8() const | |
| 227 { | |
| 228 ASSERT(m_is8Bit); | |
| 229 if (!m_length) | |
| 230 return 0; | |
| 231 if (!m_string.isNull()) | |
| 232 return m_string.characters8(); | |
| 233 ASSERT(m_buffer); | |
| 234 return m_buffer->characters8(); | |
| 235 } | |
| 236 | |
| 237 const UChar* characters16() const | |
| 238 { | |
| 239 ASSERT(!m_is8Bit); | |
| 240 if (!m_length) | |
| 241 return 0; | |
| 242 if (!m_string.isNull()) | |
| 243 return m_string.characters16(); | |
| 244 ASSERT(m_buffer); | |
| 245 return m_buffer->characters16(); | |
| 246 } | |
| 247 | |
| 248 const UChar* characters() const | |
| 249 { | |
| 250 if (!m_length) | |
| 251 return 0; | |
| 252 if (!m_string.isNull()) | |
| 253 return m_string.characters(); | |
| 254 ASSERT(m_buffer); | |
| 255 if (m_buffer->has16BitShadow() && m_valid16BitShadowLength < m_length) | |
| 256 m_buffer->upconvertCharacters(m_valid16BitShadowLength, m_length); | |
| 257 | |
| 258 m_valid16BitShadowLength = m_length; | |
| 259 | |
| 260 return m_buffer->characters(); | |
| 261 } | |
| 262 | |
| 263 bool is8Bit() const { return m_is8Bit; } | |
| 264 | |
| 265 void clear() | |
| 266 { | |
| 267 m_length = 0; | |
| 268 m_string = String(); | |
| 269 m_buffer = 0; | |
| 270 m_bufferCharacters8 = 0; | |
| 271 m_is8Bit = true; | |
| 272 m_valid16BitShadowLength = 0; | |
| 273 } | |
| 274 | |
| 275 void swap(StringBuilder& stringBuilder) | |
| 276 { | |
| 277 std::swap(m_length, stringBuilder.m_length); | |
| 278 m_string.swap(stringBuilder.m_string); | |
| 279 m_buffer.swap(stringBuilder.m_buffer); | |
| 280 std::swap(m_is8Bit, stringBuilder.m_is8Bit); | |
| 281 std::swap(m_valid16BitShadowLength, stringBuilder.m_valid16BitShadowLeng
th); | |
| 282 std::swap(m_bufferCharacters8, stringBuilder.m_bufferCharacters8); | |
| 283 } | |
| 284 | |
| 285 private: | |
| 286 void allocateBuffer(const LChar* currentCharacters, unsigned requiredLength)
; | |
| 287 void allocateBuffer(const UChar* currentCharacters, unsigned requiredLength)
; | |
| 288 void allocateBufferUpConvert(const LChar* currentCharacters, unsigned requir
edLength); | |
| 289 template <typename CharType> | |
| 290 void reallocateBuffer(unsigned requiredLength); | |
| 291 template <typename CharType> | |
| 292 ALWAYS_INLINE CharType* appendUninitialized(unsigned length); | |
| 293 template <typename CharType> | |
| 294 CharType* appendUninitializedSlow(unsigned length); | |
| 295 template <typename CharType> | |
| 296 ALWAYS_INLINE CharType * getBufferCharacters(); | |
| 297 WTF_EXPORT_PRIVATE void reifyString() const; | |
| 298 | |
| 299 unsigned m_length; | |
| 300 mutable String m_string; | |
| 301 RefPtr<StringImpl> m_buffer; | |
| 302 bool m_is8Bit; | |
| 303 mutable unsigned m_valid16BitShadowLength; | |
| 304 union { | |
| 305 LChar* m_bufferCharacters8; | |
| 306 UChar* m_bufferCharacters16; | |
| 307 }; | |
| 308 }; | |
| 309 | |
| 310 template <> | |
| 311 ALWAYS_INLINE LChar* StringBuilder::getBufferCharacters<LChar>() | |
| 312 { | |
| 313 ASSERT(m_is8Bit); | |
| 314 return m_bufferCharacters8; | |
| 315 } | |
| 316 | |
| 317 template <> | |
| 318 ALWAYS_INLINE UChar* StringBuilder::getBufferCharacters<UChar>() | |
| 319 { | |
| 320 ASSERT(!m_is8Bit); | |
| 321 return m_bufferCharacters16; | |
| 322 } | |
| 323 | |
| 324 template <typename CharType> | |
| 325 bool equal(const StringBuilder& s, const CharType* buffer, unsigned length) | |
| 326 { | |
| 327 if (s.length() != length) | |
| 328 return false; | |
| 329 | |
| 330 if (s.is8Bit()) | |
| 331 return equal(s.characters8(), buffer, length); | |
| 332 | |
| 333 return equal(s.characters16(), buffer, length); | |
| 334 } | |
| 335 | |
| 336 template <typename StringType> | |
| 337 bool equal(const StringBuilder& a, const StringType& b) | |
| 338 { | |
| 339 if (a.length() != b.length()) | |
| 340 return false; | |
| 341 | |
| 342 if (!a.length()) | |
| 343 return true; | |
| 344 | |
| 345 if (a.is8Bit()) { | |
| 346 if (b.is8Bit()) | |
| 347 return equal(a.characters8(), b.characters8(), a.length()); | |
| 348 return equal(a.characters8(), b.characters16(), a.length()); | |
| 349 } | |
| 350 | |
| 351 if (b.is8Bit()) | |
| 352 return equal(a.characters16(), b.characters8(), a.length()); | |
| 353 return equal(a.characters16(), b.characters16(), a.length()); | |
| 354 } | |
| 355 | |
| 356 inline bool operator==(const StringBuilder& a, const StringBuilder& b) { return
equal(a, b); } | |
| 357 inline bool operator!=(const StringBuilder& a, const StringBuilder& b) { return
!equal(a, b); } | |
| 358 inline bool operator==(const StringBuilder& a, const String& b) { return equal(a
, b); } | |
| 359 inline bool operator!=(const StringBuilder& a, const String& b) { return !equal(
a, b); } | |
| 360 inline bool operator==(const String& a, const StringBuilder& b) { return equal(b
, a); } | |
| 361 inline bool operator!=(const String& a, const StringBuilder& b) { return !equal(
b, a); } | |
| 362 | |
| 363 } // namespace WTF | |
| 364 | |
| 365 using WTF::StringBuilder; | |
| 366 | |
| 367 #endif // StringBuilder_h | |
| OLD | NEW |