OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2009, 2010, 2012, 2013 Apple Inc. All rights reserved. | 2 * Copyright (C) 2009, 2010, 2012, 2013 Apple Inc. All rights reserved. |
3 * Copyright (C) 2012 Google Inc. All rights reserved. | 3 * Copyright (C) 2012 Google Inc. All rights reserved. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
7 * are met: | 7 * are met: |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
(...skipping 11 matching lines...) Expand all Loading... |
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
25 */ | 25 */ |
26 | 26 |
27 #ifndef StringBuilder_h | 27 #ifndef StringBuilder_h |
28 #define StringBuilder_h | 28 #define StringBuilder_h |
29 | 29 |
30 #include "wtf/WTFExport.h" | 30 #include "wtf/WTFExport.h" |
31 #include "wtf/text/AtomicString.h" | 31 #include "wtf/text/AtomicString.h" |
32 #include "wtf/text/StringView.h" | |
33 #include "wtf/text/WTFString.h" | 32 #include "wtf/text/WTFString.h" |
34 | 33 |
35 namespace WTF { | 34 namespace WTF { |
36 | 35 |
37 class WTF_EXPORT StringBuilder { | 36 class WTF_EXPORT StringBuilder { |
38 // Disallow copying since it's expensive and we don't want code to do it by
accident. | 37 // Disallow copying since it's expensive and we don't want code to do it by
accident. |
39 WTF_MAKE_NONCOPYABLE(StringBuilder); | 38 WTF_MAKE_NONCOPYABLE(StringBuilder); |
40 | 39 |
41 public: | 40 public: |
42 StringBuilder() | 41 StringBuilder() |
43 : m_bufferCharacters8(0) | 42 : m_bufferCharacters8(0) |
44 , m_length(0) | 43 , m_length(0) |
45 , m_is8Bit(true) | 44 , m_is8Bit(true) |
46 { | 45 { |
47 } | 46 } |
48 | 47 |
49 void append(const UChar*, unsigned); | 48 void append(const UChar*, unsigned); |
50 void append(const LChar*, unsigned); | 49 void append(const LChar*, unsigned); |
51 | 50 |
52 ALWAYS_INLINE void append(const char* characters, unsigned length) { append(
reinterpret_cast<const LChar*>(characters), length); } | 51 ALWAYS_INLINE void append(const char* characters, unsigned length) { append(
reinterpret_cast<const LChar*>(characters), length); } |
53 | 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 |
54 void append(const StringBuilder& other) | 73 void append(const StringBuilder& other) |
55 { | 74 { |
56 if (!other.m_length) | 75 if (!other.m_length) |
57 return; | 76 return; |
58 | 77 |
59 // If we're appending to an empty string, and there is not a buffer (res
erveCapacity has not been called) | 78 // If we're appending to an empty string, and there is not a buffer (res
erveCapacity has not been called) |
60 // then just retain the string. | 79 // then just retain the string. |
61 if (!m_length && !m_buffer && !other.m_string.isNull()) { | 80 if (!m_length && !m_buffer && !other.m_string.isNull()) { |
62 m_string = other.m_string; | 81 m_string = other.m_string; |
63 m_length = other.m_length; | 82 m_length = other.m_length; |
64 return; | 83 return; |
65 } | 84 } |
66 | 85 |
67 if (other.is8Bit()) | 86 if (other.is8Bit()) |
68 append(other.characters8(), other.m_length); | 87 append(other.characters8(), other.m_length); |
69 else | 88 else |
70 append(other.characters16(), other.m_length); | 89 append(other.characters16(), other.m_length); |
71 } | 90 } |
72 | 91 |
73 // TODO(esprehn): This method is just duplicating what StringView itself | 92 void append(const String& string, unsigned offset, unsigned length) |
74 // does. Remove it and replace callers with append(StringView(string, offset
, length)). | |
75 void append(const StringView& string, unsigned offset, unsigned length) | |
76 { | 93 { |
77 if (!string.length()) | 94 if (!string.length()) |
78 return; | 95 return; |
79 | 96 |
80 unsigned extent = offset + length; | 97 unsigned extent = offset + length; |
81 if (extent < offset || extent > string.length()) | 98 if (extent < offset || extent > string.length()) |
82 return; | 99 return; |
83 | 100 |
84 if (string.is8Bit()) | 101 if (string.is8Bit()) |
85 append(string.characters8() + offset, length); | 102 append(string.characters8() + offset, length); |
86 else | 103 else |
87 append(string.characters16() + offset, length); | 104 append(string.characters16() + offset, length); |
88 } | 105 } |
89 | 106 |
90 void append(const StringView& string) | 107 void append(const StringView& string) |
91 { | 108 { |
92 if (!string.length()) | 109 if (!string.length()) |
93 return; | 110 return; |
94 | 111 |
95 if (string.is8Bit()) | 112 if (string.is8Bit()) |
96 append(string.characters8(), string.length()); | 113 append(string.characters8(), string.length()); |
97 else | 114 else |
98 append(string.characters16(), string.length()); | 115 append(string.characters16(), string.length()); |
99 } | 116 } |
100 | 117 |
| 118 void append(const char* characters) |
| 119 { |
| 120 if (characters) |
| 121 append(characters, strlen(characters)); |
| 122 } |
| 123 |
101 void append(UChar c) | 124 void append(UChar c) |
102 { | 125 { |
103 if (m_buffer && m_length < m_buffer->length() && m_string.isNull()) { | 126 if (m_buffer && m_length < m_buffer->length() && m_string.isNull()) { |
104 if (!m_is8Bit) { | 127 if (!m_is8Bit) { |
105 m_bufferCharacters16[m_length++] = c; | 128 m_bufferCharacters16[m_length++] = c; |
106 return; | 129 return; |
107 } | 130 } |
108 | 131 |
109 if (!(c & ~0xff)) { | 132 if (!(c & ~0xff)) { |
110 m_bufferCharacters8[m_length++] = static_cast<LChar>(c); | 133 m_bufferCharacters8[m_length++] = static_cast<LChar>(c); |
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
371 inline bool operator==(const StringBuilder& a, const String& b) { return equal(a
, b); } | 394 inline bool operator==(const StringBuilder& a, const String& b) { return equal(a
, b); } |
372 inline bool operator!=(const StringBuilder& a, const String& b) { return !equal(
a, b); } | 395 inline bool operator!=(const StringBuilder& a, const String& b) { return !equal(
a, b); } |
373 inline bool operator==(const String& a, const StringBuilder& b) { return equal(b
, a); } | 396 inline bool operator==(const String& a, const StringBuilder& b) { return equal(b
, a); } |
374 inline bool operator!=(const String& a, const StringBuilder& b) { return !equal(
b, a); } | 397 inline bool operator!=(const String& a, const StringBuilder& b) { return !equal(
b, a); } |
375 | 398 |
376 } // namespace WTF | 399 } // namespace WTF |
377 | 400 |
378 using WTF::StringBuilder; | 401 using WTF::StringBuilder; |
379 | 402 |
380 #endif // StringBuilder_h | 403 #endif // StringBuilder_h |
OLD | NEW |