| OLD | NEW |
| 1 /* | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 * Copyright (C) 2009, 2010, 2012, 2013 Apple Inc. All rights reserved. | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 * Copyright (C) 2012 Google Inc. All rights reserved. | 3 // found in the LICENSE file. |
| 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 | 4 |
| 27 #ifndef StringBuilder_h | 5 #include "platform/wtf/text/StringBuilder.h" |
| 28 #define StringBuilder_h | |
| 29 | 6 |
| 30 #include "wtf/WTFExport.h" | 7 // The contents of this header was moved to platform/wtf as part of |
| 31 #include "wtf/text/AtomicString.h" | 8 // WTF migration project. See the following post for details: |
| 32 #include "wtf/text/StringView.h" | 9 // https://groups.google.com/a/chromium.org/d/msg/blink-dev/tLdAZCTlcAA/bYXVT8gY
CAAJ |
| 33 #include "wtf/text/WTFString.h" | |
| 34 | |
| 35 namespace WTF { | |
| 36 | |
| 37 class WTF_EXPORT StringBuilder { | |
| 38 WTF_MAKE_NONCOPYABLE(StringBuilder); | |
| 39 | |
| 40 public: | |
| 41 StringBuilder() : m_buffer(nullptr), m_length(0), m_is8Bit(true) {} | |
| 42 | |
| 43 ~StringBuilder() { clear(); } | |
| 44 | |
| 45 void append(const UChar*, unsigned length); | |
| 46 void append(const LChar*, unsigned length); | |
| 47 | |
| 48 ALWAYS_INLINE void append(const char* characters, unsigned length) { | |
| 49 append(reinterpret_cast<const LChar*>(characters), length); | |
| 50 } | |
| 51 | |
| 52 void append(const StringBuilder& other) { | |
| 53 if (!other.m_length) | |
| 54 return; | |
| 55 | |
| 56 if (!m_length && !hasBuffer() && !other.m_string.isNull()) { | |
| 57 m_string = other.m_string; | |
| 58 m_length = other.m_string.length(); | |
| 59 m_is8Bit = other.m_string.is8Bit(); | |
| 60 return; | |
| 61 } | |
| 62 | |
| 63 if (other.is8Bit()) | |
| 64 append(other.characters8(), other.m_length); | |
| 65 else | |
| 66 append(other.characters16(), other.m_length); | |
| 67 } | |
| 68 | |
| 69 // NOTE: The semantics of this are different than StringView(..., offset, | |
| 70 // length) in that an invalid offset or invalid length is a no-op instead of | |
| 71 // an error. | |
| 72 // TODO(esprehn): We should probably unify the semantics instead. | |
| 73 void append(const StringView& string, unsigned offset, unsigned length) { | |
| 74 unsigned extent = offset + length; | |
| 75 if (extent < offset || extent > string.length()) | |
| 76 return; | |
| 77 | |
| 78 // We can't do this before the above check since StringView's constructor | |
| 79 // doesn't accept invalid offsets or lengths. | |
| 80 append(StringView(string, offset, length)); | |
| 81 } | |
| 82 | |
| 83 void append(const StringView& string) { | |
| 84 if (string.isEmpty()) | |
| 85 return; | |
| 86 | |
| 87 // If we're appending to an empty builder, and there is not a buffer | |
| 88 // (reserveCapacity has not been called), then share the impl if | |
| 89 // possible. | |
| 90 // | |
| 91 // This is important to avoid string copies inside dom operations like | |
| 92 // Node::textContent when there's only a single Text node child, or | |
| 93 // inside the parser in the common case when flushing buffered text to | |
| 94 // a Text node. | |
| 95 StringImpl* impl = string.sharedImpl(); | |
| 96 if (!m_length && !hasBuffer() && impl) { | |
| 97 m_string = impl; | |
| 98 m_length = impl->length(); | |
| 99 m_is8Bit = impl->is8Bit(); | |
| 100 return; | |
| 101 } | |
| 102 | |
| 103 if (string.is8Bit()) | |
| 104 append(string.characters8(), string.length()); | |
| 105 else | |
| 106 append(string.characters16(), string.length()); | |
| 107 } | |
| 108 | |
| 109 void append(UChar c) { | |
| 110 if (m_is8Bit && c <= 0xFF) { | |
| 111 append(static_cast<LChar>(c)); | |
| 112 return; | |
| 113 } | |
| 114 ensureBuffer16(1); | |
| 115 m_buffer16->push_back(c); | |
| 116 ++m_length; | |
| 117 } | |
| 118 | |
| 119 void append(LChar c) { | |
| 120 if (!m_is8Bit) { | |
| 121 append(static_cast<UChar>(c)); | |
| 122 return; | |
| 123 } | |
| 124 ensureBuffer8(1); | |
| 125 m_buffer8->push_back(c); | |
| 126 ++m_length; | |
| 127 } | |
| 128 | |
| 129 void append(char c) { append(static_cast<LChar>(c)); } | |
| 130 | |
| 131 void append(UChar32 c) { | |
| 132 if (U_IS_BMP(c)) { | |
| 133 append(static_cast<UChar>(c)); | |
| 134 return; | |
| 135 } | |
| 136 append(U16_LEAD(c)); | |
| 137 append(U16_TRAIL(c)); | |
| 138 } | |
| 139 | |
| 140 void appendNumber(int); | |
| 141 void appendNumber(unsigned); | |
| 142 void appendNumber(long); | |
| 143 void appendNumber(unsigned long); | |
| 144 void appendNumber(long long); | |
| 145 void appendNumber(unsigned long long); | |
| 146 void appendNumber(double, unsigned precision = 6); | |
| 147 | |
| 148 String toString(); | |
| 149 AtomicString toAtomicString(); | |
| 150 String substring(unsigned start, unsigned length) const; | |
| 151 | |
| 152 unsigned length() const { return m_length; } | |
| 153 bool isEmpty() const { return !m_length; } | |
| 154 | |
| 155 unsigned capacity() const; | |
| 156 void reserveCapacity(unsigned newCapacity); | |
| 157 | |
| 158 // TODO(esprehn): Rename to shrink(). | |
| 159 void resize(unsigned newSize); | |
| 160 | |
| 161 UChar operator[](unsigned i) const { | |
| 162 SECURITY_DCHECK(i < m_length); | |
| 163 if (m_is8Bit) | |
| 164 return characters8()[i]; | |
| 165 return characters16()[i]; | |
| 166 } | |
| 167 | |
| 168 const LChar* characters8() const { | |
| 169 DCHECK(m_is8Bit); | |
| 170 if (!length()) | |
| 171 return nullptr; | |
| 172 if (!m_string.isNull()) | |
| 173 return m_string.characters8(); | |
| 174 DCHECK(m_buffer8); | |
| 175 return m_buffer8->data(); | |
| 176 } | |
| 177 | |
| 178 const UChar* characters16() const { | |
| 179 DCHECK(!m_is8Bit); | |
| 180 if (!length()) | |
| 181 return nullptr; | |
| 182 if (!m_string.isNull()) | |
| 183 return m_string.characters16(); | |
| 184 DCHECK(m_buffer16); | |
| 185 return m_buffer16->data(); | |
| 186 } | |
| 187 | |
| 188 bool is8Bit() const { return m_is8Bit; } | |
| 189 | |
| 190 void clear(); | |
| 191 void swap(StringBuilder&); | |
| 192 | |
| 193 private: | |
| 194 static const unsigned kInlineBufferSize = 16; | |
| 195 static unsigned initialBufferSize() { return kInlineBufferSize; } | |
| 196 | |
| 197 typedef Vector<LChar, kInlineBufferSize> Buffer8; | |
| 198 typedef Vector<UChar, kInlineBufferSize> Buffer16; | |
| 199 | |
| 200 void ensureBuffer8(unsigned addedSize) { | |
| 201 DCHECK(m_is8Bit); | |
| 202 if (!hasBuffer()) | |
| 203 createBuffer8(addedSize); | |
| 204 } | |
| 205 | |
| 206 void ensureBuffer16(unsigned addedSize) { | |
| 207 if (m_is8Bit || !hasBuffer()) | |
| 208 createBuffer16(addedSize); | |
| 209 } | |
| 210 | |
| 211 void createBuffer8(unsigned addedSize); | |
| 212 void createBuffer16(unsigned addedSize); | |
| 213 void clearBuffer(); | |
| 214 bool hasBuffer() const { return m_buffer; } | |
| 215 | |
| 216 String m_string; | |
| 217 union { | |
| 218 Buffer8* m_buffer8; | |
| 219 Buffer16* m_buffer16; | |
| 220 void* m_buffer; | |
| 221 }; | |
| 222 unsigned m_length; | |
| 223 bool m_is8Bit; | |
| 224 }; | |
| 225 | |
| 226 template <typename CharType> | |
| 227 bool equal(const StringBuilder& s, const CharType* buffer, unsigned length) { | |
| 228 if (s.length() != length) | |
| 229 return false; | |
| 230 | |
| 231 if (s.is8Bit()) | |
| 232 return equal(s.characters8(), buffer, length); | |
| 233 | |
| 234 return equal(s.characters16(), buffer, length); | |
| 235 } | |
| 236 | |
| 237 template <typename CharType> | |
| 238 bool equalIgnoringCase(const StringBuilder& s, | |
| 239 const CharType* buffer, | |
| 240 unsigned length) { | |
| 241 if (s.length() != length) | |
| 242 return false; | |
| 243 | |
| 244 if (s.is8Bit()) | |
| 245 return equalIgnoringCase(s.characters8(), buffer, length); | |
| 246 | |
| 247 return equalIgnoringCase(s.characters16(), buffer, length); | |
| 248 } | |
| 249 | |
| 250 // Unicode aware case insensitive string matching. Non-ASCII characters might | |
| 251 // match to ASCII characters. This function is rarely used to implement web | |
| 252 // platform features. | |
| 253 inline bool equalIgnoringCase(const StringBuilder& s, const char* string) { | |
| 254 return equalIgnoringCase(s, reinterpret_cast<const LChar*>(string), | |
| 255 strlen(string)); | |
| 256 } | |
| 257 | |
| 258 template <typename StringType> | |
| 259 bool equal(const StringBuilder& a, const StringType& b) { | |
| 260 if (a.length() != b.length()) | |
| 261 return false; | |
| 262 | |
| 263 if (!a.length()) | |
| 264 return true; | |
| 265 | |
| 266 if (a.is8Bit()) { | |
| 267 if (b.is8Bit()) | |
| 268 return equal(a.characters8(), b.characters8(), a.length()); | |
| 269 return equal(a.characters8(), b.characters16(), a.length()); | |
| 270 } | |
| 271 | |
| 272 if (b.is8Bit()) | |
| 273 return equal(a.characters16(), b.characters8(), a.length()); | |
| 274 return equal(a.characters16(), b.characters16(), a.length()); | |
| 275 } | |
| 276 | |
| 277 inline bool operator==(const StringBuilder& a, const StringBuilder& b) { | |
| 278 return equal(a, b); | |
| 279 } | |
| 280 inline bool operator!=(const StringBuilder& a, const StringBuilder& b) { | |
| 281 return !equal(a, b); | |
| 282 } | |
| 283 inline bool operator==(const StringBuilder& a, const String& b) { | |
| 284 return equal(a, b); | |
| 285 } | |
| 286 inline bool operator!=(const StringBuilder& a, const String& b) { | |
| 287 return !equal(a, b); | |
| 288 } | |
| 289 inline bool operator==(const String& a, const StringBuilder& b) { | |
| 290 return equal(b, a); | |
| 291 } | |
| 292 inline bool operator!=(const String& a, const StringBuilder& b) { | |
| 293 return !equal(b, a); | |
| 294 } | |
| 295 | |
| 296 } // namespace WTF | |
| 297 | |
| 298 using WTF::StringBuilder; | |
| 299 | |
| 300 #endif // StringBuilder_h | |
| OLD | NEW |