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" |
32 #include "wtf/text/WTFString.h" | 33 #include "wtf/text/WTFString.h" |
33 | 34 |
34 namespace WTF { | 35 namespace WTF { |
35 | 36 |
36 class WTF_EXPORT StringBuilder { | 37 class WTF_EXPORT StringBuilder { |
37 // Disallow copying since it's expensive and we don't want code to do it by
accident. | 38 // Disallow copying since it's expensive and we don't want code to do it by
accident. |
38 WTF_MAKE_NONCOPYABLE(StringBuilder); | 39 WTF_MAKE_NONCOPYABLE(StringBuilder); |
39 | 40 |
40 public: | 41 public: |
41 StringBuilder() | 42 StringBuilder() |
42 : m_bufferCharacters8(0) | 43 : m_bufferCharacters8(0) |
43 , m_length(0) | 44 , m_length(0) |
44 , m_is8Bit(true) | 45 , m_is8Bit(true) |
45 { | 46 { |
46 } | 47 } |
47 | 48 |
48 void append(const UChar*, unsigned); | 49 void append(const UChar*, unsigned); |
49 void append(const LChar*, unsigned); | 50 void append(const LChar*, unsigned); |
50 | 51 |
51 ALWAYS_INLINE void append(const char* characters, unsigned length) { append(
reinterpret_cast<const LChar*>(characters), length); } | 52 ALWAYS_INLINE void append(const char* characters, unsigned length) { append(
reinterpret_cast<const LChar*>(characters), length); } |
52 | 53 |
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) | 54 void append(const StringBuilder& other) |
74 { | 55 { |
75 if (!other.m_length) | 56 if (!other.m_length) |
76 return; | 57 return; |
77 | 58 |
78 // If we're appending to an empty string, and there is not a buffer (res
erveCapacity has not been called) | 59 // 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. | 60 // then just retain the string. |
80 if (!m_length && !m_buffer && !other.m_string.isNull()) { | 61 if (!m_length && !m_buffer && !other.m_string.isNull()) { |
81 m_string = other.m_string; | 62 m_string = other.m_string; |
82 m_length = other.m_length; | 63 m_length = other.m_length; |
83 return; | 64 return; |
84 } | 65 } |
85 | 66 |
86 if (other.is8Bit()) | 67 if (other.is8Bit()) |
87 append(other.characters8(), other.m_length); | 68 append(other.characters8(), other.m_length); |
88 else | 69 else |
89 append(other.characters16(), other.m_length); | 70 append(other.characters16(), other.m_length); |
90 } | 71 } |
91 | 72 |
92 void append(const String& string, unsigned offset, unsigned length) | 73 // TODO(esprehn): This method is just duplicating what StringView itself |
| 74 // does. Remove it and replace callers with append(StringView(string, offset
, length)). |
| 75 void append(const StringView& string, unsigned offset, unsigned length) |
93 { | 76 { |
94 if (!string.length()) | 77 if (!string.length()) |
95 return; | 78 return; |
96 | 79 |
97 unsigned extent = offset + length; | 80 unsigned extent = offset + length; |
98 if (extent < offset || extent > string.length()) | 81 if (extent < offset || extent > string.length()) |
99 return; | 82 return; |
100 | 83 |
101 if (string.is8Bit()) | 84 if (string.is8Bit()) |
102 append(string.characters8() + offset, length); | 85 append(string.characters8() + offset, length); |
103 else | 86 else |
104 append(string.characters16() + offset, length); | 87 append(string.characters16() + offset, length); |
105 } | 88 } |
106 | 89 |
107 void append(const StringView& string) | 90 void append(const StringView& string) |
108 { | 91 { |
109 if (!string.length()) | 92 if (!string.length()) |
110 return; | 93 return; |
111 | 94 |
112 if (string.is8Bit()) | 95 if (string.is8Bit()) |
113 append(string.characters8(), string.length()); | 96 append(string.characters8(), string.length()); |
114 else | 97 else |
115 append(string.characters16(), string.length()); | 98 append(string.characters16(), string.length()); |
116 } | 99 } |
117 | 100 |
118 void append(const char* characters) | |
119 { | |
120 if (characters) | |
121 append(characters, strlen(characters)); | |
122 } | |
123 | |
124 void append(UChar c) | 101 void append(UChar c) |
125 { | 102 { |
126 if (m_buffer && m_length < m_buffer->length() && m_string.isNull()) { | 103 if (m_buffer && m_length < m_buffer->length() && m_string.isNull()) { |
127 if (!m_is8Bit) { | 104 if (!m_is8Bit) { |
128 m_bufferCharacters16[m_length++] = c; | 105 m_bufferCharacters16[m_length++] = c; |
129 return; | 106 return; |
130 } | 107 } |
131 | 108 |
132 if (!(c & ~0xff)) { | 109 if (!(c & ~0xff)) { |
133 m_bufferCharacters8[m_length++] = static_cast<LChar>(c); | 110 m_bufferCharacters8[m_length++] = static_cast<LChar>(c); |
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
394 inline bool operator==(const StringBuilder& a, const String& b) { return equal(a
, b); } | 371 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); } | 372 inline bool operator!=(const StringBuilder& a, const String& b) { return !equal(
a, b); } |
396 inline bool operator==(const String& a, const StringBuilder& b) { return equal(b
, a); } | 373 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); } | 374 inline bool operator!=(const String& a, const StringBuilder& b) { return !equal(
b, a); } |
398 | 375 |
399 } // namespace WTF | 376 } // namespace WTF |
400 | 377 |
401 using WTF::StringBuilder; | 378 using WTF::StringBuilder; |
402 | 379 |
403 #endif // StringBuilder_h | 380 #endif // StringBuilder_h |
OLD | NEW |