Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(343)

Side by Side Diff: third_party/WebKit/Source/wtf/text/WTFString.h

Issue 1436153002: Apply clang-format with Chromium-style without column limit. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * (C) 1999 Lars Knoll (knoll@kde.org) 2 * (C) 1999 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012, 2013 Apple Inc. All rights reserved. 3 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012, 2013 Apple Inc. All rights reserved.
4 * 4 *
5 * This library is free software; you can redistribute it and/or 5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public 6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either 7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version. 8 * version 2 of the License, or (at your option) any later version.
9 * 9 *
10 * This library is distributed in the hope that it will be useful, 10 * This library is distributed in the hope that it will be useful,
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 47
48 WTF_EXPORT int charactersToIntStrict(const LChar*, size_t, bool* ok = 0, int bas e = 10); 48 WTF_EXPORT int charactersToIntStrict(const LChar*, size_t, bool* ok = 0, int bas e = 10);
49 WTF_EXPORT int charactersToIntStrict(const UChar*, size_t, bool* ok = 0, int bas e = 10); 49 WTF_EXPORT int charactersToIntStrict(const UChar*, size_t, bool* ok = 0, int bas e = 10);
50 WTF_EXPORT unsigned charactersToUIntStrict(const LChar*, size_t, bool* ok = 0, i nt base = 10); 50 WTF_EXPORT unsigned charactersToUIntStrict(const LChar*, size_t, bool* ok = 0, i nt base = 10);
51 WTF_EXPORT unsigned charactersToUIntStrict(const UChar*, size_t, bool* ok = 0, i nt base = 10); 51 WTF_EXPORT unsigned charactersToUIntStrict(const UChar*, size_t, bool* ok = 0, i nt base = 10);
52 WTF_EXPORT int64_t charactersToInt64Strict(const LChar*, size_t, bool* ok = 0, i nt base = 10); 52 WTF_EXPORT int64_t charactersToInt64Strict(const LChar*, size_t, bool* ok = 0, i nt base = 10);
53 WTF_EXPORT int64_t charactersToInt64Strict(const UChar*, size_t, bool* ok = 0, i nt base = 10); 53 WTF_EXPORT int64_t charactersToInt64Strict(const UChar*, size_t, bool* ok = 0, i nt base = 10);
54 WTF_EXPORT uint64_t charactersToUInt64Strict(const LChar*, size_t, bool* ok = 0, int base = 10); 54 WTF_EXPORT uint64_t charactersToUInt64Strict(const LChar*, size_t, bool* ok = 0, int base = 10);
55 WTF_EXPORT uint64_t charactersToUInt64Strict(const UChar*, size_t, bool* ok = 0, int base = 10); 55 WTF_EXPORT uint64_t charactersToUInt64Strict(const UChar*, size_t, bool* ok = 0, int base = 10);
56 56
57 WTF_EXPORT int charactersToInt(const LChar*, size_t, bool* ok = 0); // ignores t railing garbage 57 WTF_EXPORT int charactersToInt(const LChar*, size_t, bool* ok = 0); // ignores trailing garbage
58 WTF_EXPORT int charactersToInt(const UChar*, size_t, bool* ok = 0); // ignores t railing garbage 58 WTF_EXPORT int charactersToInt(const UChar*, size_t, bool* ok = 0); // ignores trailing garbage
59 WTF_EXPORT unsigned charactersToUInt(const LChar*, size_t, bool* ok = 0); // ign ores trailing garbage 59 WTF_EXPORT unsigned charactersToUInt(const LChar*, size_t, bool* ok = 0); // ignores trailing garbage
60 WTF_EXPORT unsigned charactersToUInt(const UChar*, size_t, bool* ok = 0); // ign ores trailing garbage 60 WTF_EXPORT unsigned charactersToUInt(const UChar*, size_t, bool* ok = 0); // ignores trailing garbage
61 WTF_EXPORT int64_t charactersToInt64(const LChar*, size_t, bool* ok = 0); // ign ores trailing garbage 61 WTF_EXPORT int64_t charactersToInt64(const LChar*, size_t, bool* ok = 0); // ignores trailing garbage
62 WTF_EXPORT int64_t charactersToInt64(const UChar*, size_t, bool* ok = 0); // ign ores trailing garbage 62 WTF_EXPORT int64_t charactersToInt64(const UChar*, size_t, bool* ok = 0); // ignores trailing garbage
63 WTF_EXPORT uint64_t charactersToUInt64(const LChar*, size_t, bool* ok = 0); // i gnores trailing garbage 63 WTF_EXPORT uint64_t charactersToUInt64(const LChar*, size_t, bool* ok = 0); // ignores trailing garbage
64 WTF_EXPORT uint64_t charactersToUInt64(const UChar*, size_t, bool* ok = 0); // i gnores trailing garbage 64 WTF_EXPORT uint64_t charactersToUInt64(const UChar*, size_t, bool* ok = 0); // ignores trailing garbage
65 65
66 // FIXME: Like the strict functions above, these give false for "ok" when there 66 // FIXME: Like the strict functions above, these give false for "ok" when there
67 // is trailing garbage. Like the non-strict functions above, these return the 67 // is trailing garbage. Like the non-strict functions above, these return the
68 // value when there is trailing garbage. It would be better if these were more 68 // value when there is trailing garbage. It would be better if these were more
69 // consistent with the above functions instead. 69 // consistent with the above functions instead.
70 WTF_EXPORT double charactersToDouble(const LChar*, size_t, bool* ok = 0); 70 WTF_EXPORT double charactersToDouble(const LChar*, size_t, bool* ok = 0);
71 WTF_EXPORT double charactersToDouble(const UChar*, size_t, bool* ok = 0); 71 WTF_EXPORT double charactersToDouble(const UChar*, size_t, bool* ok = 0);
72 WTF_EXPORT float charactersToFloat(const LChar*, size_t, bool* ok = 0); 72 WTF_EXPORT float charactersToFloat(const LChar*, size_t, bool* ok = 0);
73 WTF_EXPORT float charactersToFloat(const UChar*, size_t, bool* ok = 0); 73 WTF_EXPORT float charactersToFloat(const UChar*, size_t, bool* ok = 0);
74 WTF_EXPORT float charactersToFloat(const LChar*, size_t, size_t& parsedLength); 74 WTF_EXPORT float charactersToFloat(const LChar*, size_t, size_t& parsedLength);
75 WTF_EXPORT float charactersToFloat(const UChar*, size_t, size_t& parsedLength); 75 WTF_EXPORT float charactersToFloat(const UChar*, size_t, size_t& parsedLength);
76 76
77 enum TrailingZerosTruncatingPolicy { 77 enum TrailingZerosTruncatingPolicy {
78 KeepTrailingZeros, 78 KeepTrailingZeros,
79 TruncateTrailingZeros 79 TruncateTrailingZeros
80 }; 80 };
81 81
82 enum UTF8ConversionMode { 82 enum UTF8ConversionMode {
83 LenientUTF8Conversion, 83 LenientUTF8Conversion,
84 StrictUTF8Conversion, 84 StrictUTF8Conversion,
85 StrictUTF8ConversionReplacingUnpairedSurrogatesWithFFFD 85 StrictUTF8ConversionReplacingUnpairedSurrogatesWithFFFD
86 }; 86 };
87 87
88 template<bool isSpecialCharacter(UChar), typename CharacterType> 88 template <bool isSpecialCharacter(UChar), typename CharacterType>
89 bool isAllSpecialCharacters(const CharacterType*, size_t); 89 bool isAllSpecialCharacters(const CharacterType*, size_t);
90 90
91 // You can find documentation about this class in this doc: 91 // You can find documentation about this class in this doc:
92 // https://docs.google.com/document/d/1kOCUlJdh2WJMJGDf-WoEQhmnjKLaOYRbiHz5TiGJl 14/edit?usp=sharing 92 // https://docs.google.com/document/d/1kOCUlJdh2WJMJGDf-WoEQhmnjKLaOYRbiHz5TiGJl 14/edit?usp=sharing
93 class WTF_EXPORT String { 93 class WTF_EXPORT String {
94 public: 94 public:
95 // Construct a null string, distinguishable from an empty string. 95 // Construct a null string, distinguishable from an empty string.
96 String() { } 96 String() {}
97 97
98 // Construct a string with UTF-16 data. 98 // Construct a string with UTF-16 data.
99 String(const UChar* characters, unsigned length); 99 String(const UChar* characters, unsigned length);
100 100
101 // Construct a string by copying the contents of a vector. 101 // Construct a string by copying the contents of a vector.
102 // This method will never create a null string. Vectors with size() == 0 102 // This method will never create a null string. Vectors with size() == 0
103 // will return the empty string. 103 // will return the empty string.
104 // NOTE: This is different from String(vector.data(), vector.size()) 104 // NOTE: This is different from String(vector.data(), vector.size())
105 // which will sometimes return a null string when vector.data() is null 105 // which will sometimes return a null string when vector.data() is null
106 // which can only occur for vectors without inline capacity. 106 // which can only occur for vectors without inline capacity.
107 // See: https://bugs.webkit.org/show_bug.cgi?id=109792 107 // See: https://bugs.webkit.org/show_bug.cgi?id=109792
108 template<size_t inlineCapacity> 108 template <size_t inlineCapacity>
109 explicit String(const Vector<UChar, inlineCapacity>&); 109 explicit String(const Vector<UChar, inlineCapacity>&);
110 110
111 // Construct a string with UTF-16 data, from a null-terminated source. 111 // Construct a string with UTF-16 data, from a null-terminated source.
112 String(const UChar*); 112 String(const UChar*);
113 113
114 // Construct a string with latin1 data. 114 // Construct a string with latin1 data.
115 String(const LChar* characters, unsigned length); 115 String(const LChar* characters, unsigned length);
116 String(const char* characters, unsigned length); 116 String(const char* characters, unsigned length);
117 117
118 // Construct a string with latin1 data, from a null-terminated source. 118 // Construct a string with latin1 data, from a null-terminated source.
119 String(const LChar* characters); 119 String(const LChar* characters);
120 String(const char* characters); 120 String(const char* characters);
121 121
122 // Construct a string referencing an existing StringImpl. 122 // Construct a string referencing an existing StringImpl.
123 String(StringImpl* impl) : m_impl(impl) { } 123 String(StringImpl* impl)
124 String(PassRefPtr<StringImpl> impl) : m_impl(impl) { } 124 : m_impl(impl) {}
125 125 String(PassRefPtr<StringImpl> impl)
126 void swap(String& o) { m_impl.swap(o.m_impl); } 126 : m_impl(impl) {}
127 127
128 template<typename CharType> 128 void swap(String& o) { m_impl.swap(o.m_impl); }
129 static String adopt(StringBuffer<CharType>& buffer) 129
130 { 130 template <typename CharType>
131 if (!buffer.length()) 131 static String adopt(StringBuffer<CharType>& buffer) {
132 return StringImpl::empty(); 132 if (!buffer.length())
133 return String(buffer.release()); 133 return StringImpl::empty();
134 } 134 return String(buffer.release());
135 135 }
136 bool isNull() const { return !m_impl; } 136
137 bool isEmpty() const { return !m_impl || !m_impl->length(); } 137 bool isNull() const { return !m_impl; }
138 138 bool isEmpty() const { return !m_impl || !m_impl->length(); }
139 StringImpl* impl() const { return m_impl.get(); } 139
140 PassRefPtr<StringImpl> releaseImpl() { return m_impl.release(); } 140 StringImpl* impl() const { return m_impl.get(); }
141 141 PassRefPtr<StringImpl> releaseImpl() { return m_impl.release(); }
142 unsigned length() const 142
143 { 143 unsigned length() const {
144 if (!m_impl) 144 if (!m_impl)
145 return 0; 145 return 0;
146 return m_impl->length(); 146 return m_impl->length();
147 } 147 }
148 148
149 const LChar* characters8() const 149 const LChar* characters8() const {
150 { 150 if (!m_impl)
151 if (!m_impl) 151 return 0;
152 return 0; 152 ASSERT(m_impl->is8Bit());
153 ASSERT(m_impl->is8Bit()); 153 return m_impl->characters8();
154 return m_impl->characters8(); 154 }
155 } 155
156 156 const UChar* characters16() const {
157 const UChar* characters16() const 157 if (!m_impl)
158 { 158 return 0;
159 if (!m_impl) 159 ASSERT(!m_impl->is8Bit());
160 return 0; 160 return m_impl->characters16();
161 ASSERT(!m_impl->is8Bit()); 161 }
162 return m_impl->characters16(); 162
163 } 163 // Return characters8() or characters16() depending on CharacterType.
164 164 template <typename CharacterType>
165 // Return characters8() or characters16() depending on CharacterType. 165 inline const CharacterType* getCharacters() const;
166 template <typename CharacterType> 166
167 inline const CharacterType* getCharacters() const; 167 bool is8Bit() const { return m_impl->is8Bit(); }
168 168
169 bool is8Bit() const { return m_impl->is8Bit(); } 169 unsigned sizeInBytes() const {
170 170 if (!m_impl)
171 unsigned sizeInBytes() const 171 return 0;
172 { 172 return m_impl->length() * (is8Bit() ? sizeof(LChar) : sizeof(UChar));
173 if (!m_impl) 173 }
174 return 0; 174
175 return m_impl->length() * (is8Bit() ? sizeof(LChar) : sizeof(UChar)); 175 CString ascii() const;
176 } 176 CString latin1() const;
177 177 CString utf8(UTF8ConversionMode = LenientUTF8Conversion) const;
178 CString ascii() const; 178
179 CString latin1() const; 179 UChar operator[](unsigned index) const {
180 CString utf8(UTF8ConversionMode = LenientUTF8Conversion) const; 180 if (!m_impl || index >= m_impl->length())
181 181 return 0;
182 UChar operator[](unsigned index) const 182 return (*m_impl)[index];
183 { 183 }
184 if (!m_impl || index >= m_impl->length()) 184
185 return 0; 185 static String number(int);
186 return (*m_impl)[index]; 186 static String number(unsigned);
187 } 187 static String number(long);
188 188 static String number(unsigned long);
189 static String number(int); 189 static String number(long long);
190 static String number(unsigned); 190 static String number(unsigned long long);
191 static String number(long); 191
192 static String number(unsigned long); 192 static String number(double, unsigned precision = 6, TrailingZerosTruncatingPo licy = TruncateTrailingZeros);
193 static String number(long long); 193
194 static String number(unsigned long long); 194 // Number to String conversion following the ECMAScript definition.
195 195 static String numberToStringECMAScript(double);
196 static String number(double, unsigned precision = 6, TrailingZerosTruncating Policy = TruncateTrailingZeros); 196 static String numberToStringFixedWidth(double, unsigned decimalPlaces);
197 197
198 // Number to String conversion following the ECMAScript definition. 198 // Find a single character or string, also with match function & latin1
199 static String numberToStringECMAScript(double); 199 // forms.
200 static String numberToStringFixedWidth(double, unsigned decimalPlaces); 200 size_t find(UChar c, unsigned start = 0) const { return m_impl ? m_impl->find( c, start) : kNotFound; }
201 201 size_t find(LChar c, unsigned start = 0) const { return m_impl ? m_impl->find( c, start) : kNotFound; }
202 // Find a single character or string, also with match function & latin1 202 size_t find(char c, unsigned start = 0) const { return find(static_cast<LChar> (c), start); }
203 // forms. 203
204 size_t find(UChar c, unsigned start = 0) const 204 size_t find(const String& str) const { return m_impl ? m_impl->find(str.impl() ) : kNotFound; }
205 { return m_impl ? m_impl->find(c, start) : kNotFound; } 205 size_t find(const String& str, unsigned start) const { return m_impl ? m_impl- >find(str.impl(), start) : kNotFound; }
206 size_t find(LChar c, unsigned start = 0) const 206
207 { return m_impl ? m_impl->find(c, start) : kNotFound; } 207 size_t find(CharacterMatchFunctionPtr matchFunction, unsigned start = 0) const { return m_impl ? m_impl->find(matchFunction, start) : kNotFound; }
208 size_t find(char c, unsigned start = 0) const { return find(static_cast<LCha r>(c), start); } 208 size_t find(const LChar* str, unsigned start = 0) const { return m_impl ? m_im pl->find(str, start) : kNotFound; }
209 209
210 size_t find(const String& str) const 210 size_t findNextLineStart(unsigned start = 0) const { return m_impl ? m_impl->f indNextLineStart(start) : kNotFound; }
211 { return m_impl ? m_impl->find(str.impl()) : kNotFound; } 211
212 size_t find(const String& str, unsigned start) const 212 // Find the last instance of a single character or string.
213 { return m_impl ? m_impl->find(str.impl(), start) : kNotFound; } 213 size_t reverseFind(UChar c, unsigned start = UINT_MAX) const { return m_impl ? m_impl->reverseFind(c, start) : kNotFound; }
214 214 size_t reverseFind(const String& str, unsigned start = UINT_MAX) const { retur n m_impl ? m_impl->reverseFind(str.impl(), start) : kNotFound; }
215 size_t find(CharacterMatchFunctionPtr matchFunction, unsigned start = 0) con st 215
216 { return m_impl ? m_impl->find(matchFunction, start) : kNotFound; } 216 // Case insensitive string matching.
217 size_t find(const LChar* str, unsigned start = 0) const 217 size_t findIgnoringCase(const LChar* str, unsigned start = 0) const { return m _impl ? m_impl->findIgnoringCase(str, start) : kNotFound; }
218 { return m_impl ? m_impl->find(str, start) : kNotFound; } 218 size_t findIgnoringCase(const String& str, unsigned start = 0) const { return m_impl ? m_impl->findIgnoringCase(str.impl(), start) : kNotFound; }
219 219 size_t reverseFindIgnoringCase(const String& str, unsigned start = UINT_MAX) c onst { return m_impl ? m_impl->reverseFindIgnoringCase(str.impl(), start) : kNot Found; }
220 size_t findNextLineStart(unsigned start = 0) const 220
221 { return m_impl ? m_impl->findNextLineStart(start) : kNotFound; } 221 // Wrappers for find & reverseFind adding dynamic sensitivity check.
222 222 size_t find(const LChar* str, unsigned start, TextCaseSensitivity caseSensitiv ity) const { return (caseSensitivity == TextCaseSensitive) ? find(str, start) : findIgnoringCase(str, start); }
223 // Find the last instance of a single character or string. 223 size_t find(const String& str, unsigned start, TextCaseSensitivity caseSensiti vity) const { return (caseSensitivity == TextCaseSensitive) ? find(str, start) : findIgnoringCase(str, start); }
224 size_t reverseFind(UChar c, unsigned start = UINT_MAX) const 224 size_t reverseFind(const String& str, unsigned start, TextCaseSensitivity case Sensitivity) const { return (caseSensitivity == TextCaseSensitive) ? reverseFind (str, start) : reverseFindIgnoringCase(str, start); }
225 { return m_impl ? m_impl->reverseFind(c, start) : kNotFound; } 225
226 size_t reverseFind(const String& str, unsigned start = UINT_MAX) const 226 Vector<UChar> charactersWithNullTermination() const;
227 { return m_impl ? m_impl->reverseFind(str.impl(), start) : kNotFound; } 227 unsigned copyTo(UChar* buffer, unsigned pos, unsigned maxLength) const;
228 228
229 // Case insensitive string matching. 229 template <size_t inlineCapacity>
230 size_t findIgnoringCase(const LChar* str, unsigned start = 0) const 230 void appendTo(Vector<UChar, inlineCapacity>&, unsigned pos = 0, unsigned len = UINT_MAX) const;
231 { return m_impl ? m_impl->findIgnoringCase(str, start) : kNotFound; } 231
232 size_t findIgnoringCase(const String& str, unsigned start = 0) const 232 template <typename BufferType>
233 { return m_impl ? m_impl->findIgnoringCase(str.impl(), start) : kNotFoun d; } 233 void appendTo(BufferType&, unsigned pos = 0, unsigned len = UINT_MAX) const;
234 size_t reverseFindIgnoringCase(const String& str, unsigned start = UINT_MAX) const 234
235 { return m_impl ? m_impl->reverseFindIgnoringCase(str.impl(), start) : k NotFound; } 235 template <size_t inlineCapacity>
236 236 void prependTo(Vector<UChar, inlineCapacity>&, unsigned pos = 0, unsigned len = UINT_MAX) const;
237 // Wrappers for find & reverseFind adding dynamic sensitivity check. 237
238 size_t find(const LChar* str, unsigned start, TextCaseSensitivity caseSensit ivity) const 238 UChar32 characterStartingAt(unsigned) const;
239 { return (caseSensitivity == TextCaseSensitive) ? find(str, start) : fin dIgnoringCase(str, start); } 239 template <typename CharacterType>
240 size_t find(const String& str, unsigned start, TextCaseSensitivity caseSensi tivity) const 240 bool contains(CharacterType c) const { return find(c) != kNotFound; }
241 { return (caseSensitivity == TextCaseSensitive) ? find(str, start) : fin dIgnoringCase(str, start); } 241 bool contains(const LChar* str, TextCaseSensitivity caseSensitivity = TextCase Sensitive) const { return find(str, 0, caseSensitivity) != kNotFound; }
242 size_t reverseFind(const String& str, unsigned start, TextCaseSensitivity ca seSensitivity) const 242 bool contains(const String& str, TextCaseSensitivity caseSensitivity = TextCas eSensitive) const { return find(str, 0, caseSensitivity) != kNotFound; }
243 { return (caseSensitivity == TextCaseSensitive) ? reverseFind(str, start ) : reverseFindIgnoringCase(str, start); } 243
244 244 bool startsWith(const String& s, TextCaseSensitivity caseSensitivity = TextCas eSensitive) const { return m_impl ? m_impl->startsWith(s.impl(), caseSensitivity ) : s.isEmpty(); }
245 Vector<UChar> charactersWithNullTermination() const; 245 bool startsWith(UChar character) const { return m_impl ? m_impl->startsWith(ch aracter) : false; }
246 unsigned copyTo(UChar* buffer, unsigned pos, unsigned maxLength) const; 246 template <unsigned matchLength>
247 247 bool startsWith(const char(&prefix)[matchLength], TextCaseSensitivity caseSens itivity = TextCaseSensitive) const { return m_impl ? m_impl->startsWith<matchLen gth>(prefix, caseSensitivity) : !matchLength; }
248 template<size_t inlineCapacity> 248
249 void appendTo(Vector<UChar, inlineCapacity>&, unsigned pos = 0, unsigned len = UINT_MAX) const; 249 bool endsWith(const String& s, TextCaseSensitivity caseSensitivity = TextCaseS ensitive) const { return m_impl ? m_impl->endsWith(s.impl(), caseSensitivity) : s.isEmpty(); }
250 250 bool endsWith(UChar character) const { return m_impl ? m_impl->endsWith(charac ter) : false; }
251 template<typename BufferType> 251 template <unsigned matchLength>
252 void appendTo(BufferType&, unsigned pos = 0, unsigned len = UINT_MAX) const; 252 bool endsWith(const char(&prefix)[matchLength], TextCaseSensitivity caseSensit ivity = TextCaseSensitive) const { return m_impl ? m_impl->endsWith<matchLength> (prefix, caseSensitivity) : !matchLength; }
253 253
254 template<size_t inlineCapacity> 254 void append(const String&);
255 void prependTo(Vector<UChar, inlineCapacity>&, unsigned pos = 0, unsigned le n = UINT_MAX) const; 255 void append(LChar);
256 256 void append(char c) { append(static_cast<LChar>(c)); }
257 UChar32 characterStartingAt(unsigned) const; 257 void append(UChar);
258 template<typename CharacterType> 258 void append(const LChar*, unsigned length);
259 bool contains(CharacterType c) const { return find(c) != kNotFound; } 259 void append(const char* charactersToAppend, unsigned length) { append(reinterp ret_cast<const LChar*>(charactersToAppend), length); }
260 bool contains(const LChar* str, TextCaseSensitivity caseSensitivity = TextCa seSensitive) const { return find(str, 0, caseSensitivity) != kNotFound; } 260 void append(const UChar*, unsigned length);
261 bool contains(const String& str, TextCaseSensitivity caseSensitivity = TextC aseSensitive) const { return find(str, 0, caseSensitivity) != kNotFound; } 261 void insert(const String&, unsigned pos);
262 262 void insert(const LChar*, unsigned length, unsigned pos);
263 bool startsWith(const String& s, TextCaseSensitivity caseSensitivity = TextC aseSensitive) const 263 void insert(const UChar*, unsigned length, unsigned pos);
264 { return m_impl ? m_impl->startsWith(s.impl(), caseSensitivity) : s.isEm pty(); } 264
265 bool startsWith(UChar character) const 265 String& replace(UChar a, UChar b) {
266 { return m_impl ? m_impl->startsWith(character) : false; } 266 if (m_impl)
267 template<unsigned matchLength> 267 m_impl = m_impl->replace(a, b);
268 bool startsWith(const char (&prefix)[matchLength], TextCaseSensitivity caseS ensitivity = TextCaseSensitive) const 268 return *this;
269 { return m_impl ? m_impl->startsWith<matchLength>(prefix, caseSensitivit y) : !matchLength; } 269 }
270 270 String& replace(UChar a, const String& b) {
271 bool endsWith(const String& s, TextCaseSensitivity caseSensitivity = TextCas eSensitive) const 271 if (m_impl)
272 { return m_impl ? m_impl->endsWith(s.impl(), caseSensitivity) : s.isEmpt y(); } 272 m_impl = m_impl->replace(a, b.impl());
273 bool endsWith(UChar character) const 273 return *this;
274 { return m_impl ? m_impl->endsWith(character) : false; } 274 }
275 template<unsigned matchLength> 275 String& replace(const String& a, const String& b) {
276 bool endsWith(const char (&prefix)[matchLength], TextCaseSensitivity caseSen sitivity = TextCaseSensitive) const 276 if (m_impl)
277 { return m_impl ? m_impl->endsWith<matchLength>(prefix, caseSensitivity) : !matchLength; } 277 m_impl = m_impl->replace(a.impl(), b.impl());
278 278 return *this;
279 void append(const String&); 279 }
280 void append(LChar); 280 String& replace(unsigned index, unsigned len, const String& b) {
281 void append(char c) { append(static_cast<LChar>(c)); } 281 if (m_impl)
282 void append(UChar); 282 m_impl = m_impl->replace(index, len, b.impl());
283 void append(const LChar*, unsigned length); 283 return *this;
284 void append(const char* charactersToAppend, unsigned length) { append(reinte rpret_cast<const LChar*>(charactersToAppend), length); } 284 }
285 void append(const UChar*, unsigned length); 285
286 void insert(const String&, unsigned pos); 286 template <unsigned charactersCount>
287 void insert(const LChar*, unsigned length, unsigned pos); 287 ALWAYS_INLINE String& replaceWithLiteral(UChar a, const char(&characters)[char actersCount]) {
288 void insert(const UChar*, unsigned length, unsigned pos); 288 if (m_impl)
289 289 m_impl = m_impl->replace(a, characters, charactersCount - 1);
290 String& replace(UChar a, UChar b) 290
291 { 291 return *this;
292 if (m_impl) 292 }
293 m_impl = m_impl->replace(a, b); 293
294 return *this; 294 void fill(UChar c) {
295 } 295 if (m_impl)
296 String& replace(UChar a, const String& b) 296 m_impl = m_impl->fill(c);
297 { 297 }
298 if (m_impl) 298
299 m_impl = m_impl->replace(a, b.impl()); 299 void ensure16Bit();
300 return *this; 300
301 } 301 void truncate(unsigned len);
302 String& replace(const String& a, const String& b) 302 void remove(unsigned pos, int len = 1);
303 { 303
304 if (m_impl) 304 String substring(unsigned pos, unsigned len = UINT_MAX) const;
305 m_impl = m_impl->replace(a.impl(), b.impl()); 305 String left(unsigned len) const { return substring(0, len); }
306 return *this; 306 String right(unsigned len) const { return substring(length() - len, len); }
307 } 307
308 String& replace(unsigned index, unsigned len, const String& b) 308 StringView createView() const { return StringView(impl()); }
309 { 309 StringView createView(unsigned offset, unsigned length) const { return StringV iew(impl(), offset, length); }
310 if (m_impl) 310
311 m_impl = m_impl->replace(index, len, b.impl()); 311 // Returns a lowercase/uppercase version of the string
312 return *this; 312 String lower() const;
313 } 313 String upper() const;
314 314
315 template<unsigned charactersCount> 315 String lower(const AtomicString& localeIdentifier) const;
316 ALWAYS_INLINE String& replaceWithLiteral(UChar a, const char (&characters)[c haractersCount]) 316 String upper(const AtomicString& localeIdentifier) const;
317 { 317
318 if (m_impl) 318 String stripWhiteSpace() const;
319 m_impl = m_impl->replace(a, characters, charactersCount - 1); 319 String stripWhiteSpace(IsWhiteSpaceFunctionPtr) const;
320 320 String simplifyWhiteSpace(StripBehavior = StripExtraWhiteSpace) const;
321 return *this; 321 String simplifyWhiteSpace(IsWhiteSpaceFunctionPtr, StripBehavior = StripExtraW hiteSpace) const;
322 } 322
323 323 String removeCharacters(CharacterMatchFunctionPtr) const;
324 void fill(UChar c) 324 template <bool isSpecialCharacter(UChar)>
325 { 325 bool isAllSpecialCharacters() const;
326 if (m_impl) 326
327 m_impl = m_impl->fill(c); 327 // Return the string with case folded for case insensitive comparison.
328 } 328 String foldCase() const;
329 329
330 void ensure16Bit(); 330 static String format(const char*, ...) WTF_ATTRIBUTE_PRINTF(1, 2);
331 331
332 void truncate(unsigned len); 332 // Returns an uninitialized string. The characters needs to be written
333 void remove(unsigned pos, int len = 1); 333 // into the buffer returned in data before the returned string is used.
334 334 // Failure to do this will have unpredictable results.
335 String substring(unsigned pos, unsigned len = UINT_MAX) const; 335 static String createUninitialized(unsigned length, UChar*& data) { return Stri ngImpl::createUninitialized(length, data); }
336 String left(unsigned len) const { return substring(0, len); } 336 static String createUninitialized(unsigned length, LChar*& data) { return Stri ngImpl::createUninitialized(length, data); }
337 String right(unsigned len) const { return substring(length() - len, len); } 337
338 338 void split(const String& separator, bool allowEmptyEntries, Vector<String>& re sult) const;
339 StringView createView() const { return StringView(impl()); } 339 void split(const String& separator, Vector<String>& result) const {
340 StringView createView(unsigned offset, unsigned length) const { return Strin gView(impl(), offset, length); } 340 split(separator, false, result);
341 341 }
342 // Returns a lowercase/uppercase version of the string 342 void split(UChar separator, bool allowEmptyEntries, Vector<String>& result) co nst;
343 String lower() const; 343 void split(UChar separator, Vector<String>& result) const {
344 String upper() const; 344 split(separator, false, result);
345 345 }
346 String lower(const AtomicString& localeIdentifier) const; 346
347 String upper(const AtomicString& localeIdentifier) const; 347 int toIntStrict(bool* ok = 0, int base = 10) const;
348 348 unsigned toUIntStrict(bool* ok = 0, int base = 10) const;
349 String stripWhiteSpace() const; 349 int64_t toInt64Strict(bool* ok = 0, int base = 10) const;
350 String stripWhiteSpace(IsWhiteSpaceFunctionPtr) const; 350 uint64_t toUInt64Strict(bool* ok = 0, int base = 10) const;
351 String simplifyWhiteSpace(StripBehavior = StripExtraWhiteSpace) const; 351
352 String simplifyWhiteSpace(IsWhiteSpaceFunctionPtr, StripBehavior = StripExtr aWhiteSpace) const; 352 int toInt(bool* ok = 0) const;
353 353 unsigned toUInt(bool* ok = 0) const;
354 String removeCharacters(CharacterMatchFunctionPtr) const; 354 int64_t toInt64(bool* ok = 0) const;
355 template<bool isSpecialCharacter(UChar)> bool isAllSpecialCharacters() const ; 355 uint64_t toUInt64(bool* ok = 0) const;
356 356
357 // Return the string with case folded for case insensitive comparison. 357 // FIXME: Like the strict functions above, these give false for "ok" when
358 String foldCase() const; 358 // there is trailing garbage. Like the non-strict functions above, these
359 359 // return the value when there is trailing garbage. It would be better if
360 static String format(const char *, ...) WTF_ATTRIBUTE_PRINTF(1, 2); 360 // these were more consistent with the above functions instead.
361 361 double toDouble(bool* ok = 0) const;
362 // Returns an uninitialized string. The characters needs to be written 362 float toFloat(bool* ok = 0) const;
363 // into the buffer returned in data before the returned string is used. 363
364 // Failure to do this will have unpredictable results. 364 String isolatedCopy() const;
365 static String createUninitialized(unsigned length, UChar*& data) { return St ringImpl::createUninitialized(length, data); } 365 bool isSafeToSendToAnotherThread() const;
366 static String createUninitialized(unsigned length, LChar*& data) { return St ringImpl::createUninitialized(length, data); }
367
368 void split(const String& separator, bool allowEmptyEntries, Vector<String>& result) const;
369 void split(const String& separator, Vector<String>& result) const
370 {
371 split(separator, false, result);
372 }
373 void split(UChar separator, bool allowEmptyEntries, Vector<String>& result) const;
374 void split(UChar separator, Vector<String>& result) const
375 {
376 split(separator, false, result);
377 }
378
379 int toIntStrict(bool* ok = 0, int base = 10) const;
380 unsigned toUIntStrict(bool* ok = 0, int base = 10) const;
381 int64_t toInt64Strict(bool* ok = 0, int base = 10) const;
382 uint64_t toUInt64Strict(bool* ok = 0, int base = 10) const;
383
384 int toInt(bool* ok = 0) const;
385 unsigned toUInt(bool* ok = 0) const;
386 int64_t toInt64(bool* ok = 0) const;
387 uint64_t toUInt64(bool* ok = 0) const;
388
389 // FIXME: Like the strict functions above, these give false for "ok" when
390 // there is trailing garbage. Like the non-strict functions above, these
391 // return the value when there is trailing garbage. It would be better if
392 // these were more consistent with the above functions instead.
393 double toDouble(bool* ok = 0) const;
394 float toFloat(bool* ok = 0) const;
395
396 String isolatedCopy() const;
397 bool isSafeToSendToAnotherThread() const;
398 366
399 #ifdef __OBJC__ 367 #ifdef __OBJC__
400 String(NSString*); 368 String(NSString*);
401 369
402 // This conversion maps null string to "", which loses the meaning of null 370 // This conversion maps null string to "", which loses the meaning of null
403 // string, but we need this mapping because AppKit crashes when passed nil 371 // string, but we need this mapping because AppKit crashes when passed nil
404 // NSStrings. 372 // NSStrings.
405 operator NSString*() const 373 operator NSString*() const {
406 { 374 if (!m_impl)
407 if (!m_impl) 375 return @"";
408 return @""; 376 return *m_impl;
409 return *m_impl; 377 }
410 }
411 #endif 378 #endif
412 379
413 static String make8BitFrom16BitSource(const UChar*, size_t); 380 static String make8BitFrom16BitSource(const UChar*, size_t);
414 template<size_t inlineCapacity> 381 template <size_t inlineCapacity>
415 static String make8BitFrom16BitSource(const Vector<UChar, inlineCapacity>& b uffer) 382 static String make8BitFrom16BitSource(const Vector<UChar, inlineCapacity>& buf fer) {
416 { 383 return make8BitFrom16BitSource(buffer.data(), buffer.size());
417 return make8BitFrom16BitSource(buffer.data(), buffer.size()); 384 }
418 } 385
419 386 static String make16BitFrom8BitSource(const LChar*, size_t);
420 static String make16BitFrom8BitSource(const LChar*, size_t); 387
421 388 // String::fromUTF8 will return a null string if
422 // String::fromUTF8 will return a null string if 389 // the input data contains invalid UTF-8 sequences.
423 // the input data contains invalid UTF-8 sequences. 390 static String fromUTF8(const LChar*, size_t);
424 static String fromUTF8(const LChar*, size_t); 391 static String fromUTF8(const LChar*);
425 static String fromUTF8(const LChar*); 392 static String fromUTF8(const char* s, size_t length) { return fromUTF8(reinter pret_cast<const LChar*>(s), length); }
426 static String fromUTF8(const char* s, size_t length) { return fromUTF8(reint erpret_cast<const LChar*>(s), length); } 393 static String fromUTF8(const char* s) { return fromUTF8(reinterpret_cast<const LChar*>(s)); }
427 static String fromUTF8(const char* s) { return fromUTF8(reinterpret_cast<con st LChar*>(s)); } 394 static String fromUTF8(const CString&);
428 static String fromUTF8(const CString&); 395
429 396 // Tries to convert the passed in string to UTF-8, but will fall back to
430 // Tries to convert the passed in string to UTF-8, but will fall back to 397 // Latin-1 if the string is not valid UTF-8.
431 // Latin-1 if the string is not valid UTF-8. 398 static String fromUTF8WithLatin1Fallback(const LChar*, size_t);
432 static String fromUTF8WithLatin1Fallback(const LChar*, size_t); 399 static String fromUTF8WithLatin1Fallback(const char* s, size_t length) { retur n fromUTF8WithLatin1Fallback(reinterpret_cast<const LChar*>(s), length); }
433 static String fromUTF8WithLatin1Fallback(const char* s, size_t length) { ret urn fromUTF8WithLatin1Fallback(reinterpret_cast<const LChar*>(s), length); } 400
434 401 bool containsOnlyASCII() const;
435 bool containsOnlyASCII() const; 402 bool containsOnlyLatin1() const;
436 bool containsOnlyLatin1() const; 403 bool containsOnlyWhitespace() const { return !m_impl || m_impl->containsOnlyWh itespace(); }
437 bool containsOnlyWhitespace() const { return !m_impl || m_impl->containsOnly Whitespace(); } 404
438 405 // Hash table deleted values, which are only constructed and never copied or
439 // Hash table deleted values, which are only constructed and never copied or 406 // destroyed.
440 // destroyed. 407 String(WTF::HashTableDeletedValueType)
441 String(WTF::HashTableDeletedValueType) : m_impl(WTF::HashTableDeletedValue) { } 408 : m_impl(WTF::HashTableDeletedValue) {}
442 bool isHashTableDeletedValue() const { return m_impl.isHashTableDeletedValue (); } 409 bool isHashTableDeletedValue() const { return m_impl.isHashTableDeletedValue() ; }
443 410
444 #ifndef NDEBUG 411 #ifndef NDEBUG
445 void show() const; 412 void show() const;
446 #endif 413 #endif
447 414
448 // Workaround for a compiler bug. Use operator[] instead. 415 // Workaround for a compiler bug. Use operator[] instead.
449 UChar characterAt(unsigned index) const 416 UChar characterAt(unsigned index) const {
450 { 417 if (!m_impl || index >= m_impl->length())
451 if (!m_impl || index >= m_impl->length()) 418 return 0;
452 return 0; 419 return (*m_impl)[index];
453 return (*m_impl)[index]; 420 }
454 } 421
455 422 private:
456 private: 423 typedef struct ImplicitConversionFromWTFStringToBoolDisallowed*(String::*Unspe cifiedBoolType);
457 typedef struct ImplicitConversionFromWTFStringToBoolDisallowed* (String::*Un specifiedBoolType); 424 operator UnspecifiedBoolType() const;
458 operator UnspecifiedBoolType() const; 425
459 426 template <typename CharacterType>
460 template <typename CharacterType> 427 void removeInternal(const CharacterType*, unsigned, int);
461 void removeInternal(const CharacterType*, unsigned, int); 428
462 429 template <typename CharacterType>
463 template <typename CharacterType> 430 void appendInternal(CharacterType);
464 void appendInternal(CharacterType); 431
465 432 RefPtr<StringImpl> m_impl;
466 RefPtr<StringImpl> m_impl;
467 }; 433 };
468 434
469 inline bool operator==(const String& a, const String& b) { return equal(a.impl() , b.impl()); } 435 inline bool operator==(const String& a, const String& b) {
470 inline bool operator==(const String& a, const LChar* b) { return equal(a.impl(), b); } 436 return equal(a.impl(), b.impl());
471 inline bool operator==(const String& a, const char* b) { return equal(a.impl(), reinterpret_cast<const LChar*>(b)); } 437 }
472 inline bool operator==(const LChar* a, const String& b) { return equal(a, b.impl ()); } 438 inline bool operator==(const String& a, const LChar* b) {
473 inline bool operator==(const char* a, const String& b) { return equal(reinterpre t_cast<const LChar*>(a), b.impl()); } 439 return equal(a.impl(), b);
474 template<size_t inlineCapacity> 440 }
475 inline bool operator==(const Vector<char, inlineCapacity>& a, const String& b) { return equal(b.impl(), a.data(), a.size()); } 441 inline bool operator==(const String& a, const char* b) {
476 template<size_t inlineCapacity> 442 return equal(a.impl(), reinterpret_cast<const LChar*>(b));
477 inline bool operator==(const String& a, const Vector<char, inlineCapacity>& b) { return b == a; } 443 }
478 444 inline bool operator==(const LChar* a, const String& b) {
479 445 return equal(a, b.impl());
480 inline bool operator!=(const String& a, const String& b) { return !equal(a.impl( ), b.impl()); } 446 }
481 inline bool operator!=(const String& a, const LChar* b) { return !equal(a.impl() , b); } 447 inline bool operator==(const char* a, const String& b) {
482 inline bool operator!=(const String& a, const char* b) { return !equal(a.impl(), reinterpret_cast<const LChar*>(b)); } 448 return equal(reinterpret_cast<const LChar*>(a), b.impl());
483 inline bool operator!=(const LChar* a, const String& b) { return !equal(a, b.imp l()); } 449 }
484 inline bool operator!=(const char* a, const String& b) { return !equal(reinterpr et_cast<const LChar*>(a), b.impl()); } 450 template <size_t inlineCapacity>
485 template<size_t inlineCapacity> 451 inline bool operator==(const Vector<char, inlineCapacity>& a, const String& b) {
486 inline bool operator!=(const Vector<char, inlineCapacity>& a, const String& b) { return !(a == b); } 452 return equal(b.impl(), a.data(), a.size());
487 template<size_t inlineCapacity> 453 }
488 inline bool operator!=(const String& a, const Vector<char, inlineCapacity>& b) { return b != a; } 454 template <size_t inlineCapacity>
489 455 inline bool operator==(const String& a, const Vector<char, inlineCapacity>& b) {
490 inline bool equalIgnoringCase(const String& a, const String& b) { return equalIg noringCase(a.impl(), b.impl()); } 456 return b == a;
491 inline bool equalIgnoringCase(const String& a, const LChar* b) { return equalIgn oringCase(a.impl(), b); } 457 }
492 inline bool equalIgnoringCase(const String& a, const char* b) { return equalIgno ringCase(a.impl(), reinterpret_cast<const LChar*>(b)); } 458
493 inline bool equalIgnoringCase(const LChar* a, const String& b) { return equalIgn oringCase(a, b.impl()); } 459 inline bool operator!=(const String& a, const String& b) {
494 inline bool equalIgnoringCase(const char* a, const String& b) { return equalIgno ringCase(reinterpret_cast<const LChar*>(a), b.impl()); } 460 return !equal(a.impl(), b.impl());
495 461 }
496 inline bool equalPossiblyIgnoringCase(const String& a, const String& b, bool ign oreCase) 462 inline bool operator!=(const String& a, const LChar* b) {
497 { 463 return !equal(a.impl(), b);
498 return ignoreCase ? equalIgnoringCase(a, b) : (a == b); 464 }
499 } 465 inline bool operator!=(const String& a, const char* b) {
500 466 return !equal(a.impl(), reinterpret_cast<const LChar*>(b));
501 inline bool equalIgnoringNullity(const String& a, const String& b) { return equa lIgnoringNullity(a.impl(), b.impl()); } 467 }
502 468 inline bool operator!=(const LChar* a, const String& b) {
503 template<size_t inlineCapacity> 469 return !equal(a, b.impl());
504 inline bool equalIgnoringNullity(const Vector<UChar, inlineCapacity>& a, const S tring& b) { return equalIgnoringNullity(a, b.impl()); } 470 }
505 471 inline bool operator!=(const char* a, const String& b) {
506 inline bool operator!(const String& str) { return str.isNull(); } 472 return !equal(reinterpret_cast<const LChar*>(a), b.impl());
507 473 }
508 inline void swap(String& a, String& b) { a.swap(b); } 474 template <size_t inlineCapacity>
475 inline bool operator!=(const Vector<char, inlineCapacity>& a, const String& b) {
476 return !(a == b);
477 }
478 template <size_t inlineCapacity>
479 inline bool operator!=(const String& a, const Vector<char, inlineCapacity>& b) {
480 return b != a;
481 }
482
483 inline bool equalIgnoringCase(const String& a, const String& b) {
484 return equalIgnoringCase(a.impl(), b.impl());
485 }
486 inline bool equalIgnoringCase(const String& a, const LChar* b) {
487 return equalIgnoringCase(a.impl(), b);
488 }
489 inline bool equalIgnoringCase(const String& a, const char* b) {
490 return equalIgnoringCase(a.impl(), reinterpret_cast<const LChar*>(b));
491 }
492 inline bool equalIgnoringCase(const LChar* a, const String& b) {
493 return equalIgnoringCase(a, b.impl());
494 }
495 inline bool equalIgnoringCase(const char* a, const String& b) {
496 return equalIgnoringCase(reinterpret_cast<const LChar*>(a), b.impl());
497 }
498
499 inline bool equalPossiblyIgnoringCase(const String& a, const String& b, bool ign oreCase) {
500 return ignoreCase ? equalIgnoringCase(a, b) : (a == b);
501 }
502
503 inline bool equalIgnoringNullity(const String& a, const String& b) {
504 return equalIgnoringNullity(a.impl(), b.impl());
505 }
506
507 template <size_t inlineCapacity>
508 inline bool equalIgnoringNullity(const Vector<UChar, inlineCapacity>& a, const S tring& b) {
509 return equalIgnoringNullity(a, b.impl());
510 }
511
512 inline bool operator!(const String& str) {
513 return str.isNull();
514 }
515
516 inline void swap(String& a, String& b) {
517 a.swap(b);
518 }
509 519
510 // Definitions of string operations 520 // Definitions of string operations
511 521
512 template<size_t inlineCapacity> 522 template <size_t inlineCapacity>
513 String::String(const Vector<UChar, inlineCapacity>& vector) 523 String::String(const Vector<UChar, inlineCapacity>& vector)
514 : m_impl(vector.size() ? StringImpl::create(vector.data(), vector.size()) : StringImpl::empty()) 524 : m_impl(vector.size() ? StringImpl::create(vector.data(), vector.size()) : StringImpl::empty()) {
515 { 525 }
516 } 526
517 527 template <>
518 template<> 528 inline const LChar* String::getCharacters<LChar>() const {
519 inline const LChar* String::getCharacters<LChar>() const 529 ASSERT(is8Bit());
520 { 530 return characters8();
521 ASSERT(is8Bit()); 531 }
522 return characters8(); 532
523 } 533 template <>
524 534 inline const UChar* String::getCharacters<UChar>() const {
525 template<> 535 ASSERT(!is8Bit());
526 inline const UChar* String::getCharacters<UChar>() const 536 return characters16();
527 { 537 }
528 ASSERT(!is8Bit()); 538
529 return characters16(); 539 inline bool String::containsOnlyLatin1() const {
530 } 540 if (isEmpty())
531 541 return true;
532 inline bool String::containsOnlyLatin1() const 542
533 { 543 if (is8Bit())
534 if (isEmpty()) 544 return true;
535 return true; 545
536 546 const UChar* characters = characters16();
537 if (is8Bit()) 547 UChar ored = 0;
538 return true; 548 for (size_t i = 0; i < m_impl->length(); ++i)
539 549 ored |= characters[i];
540 const UChar* characters = characters16(); 550 return !(ored & 0xFF00);
541 UChar ored = 0; 551 }
542 for (size_t i = 0; i < m_impl->length(); ++i)
543 ored |= characters[i];
544 return !(ored & 0xFF00);
545 }
546
547 552
548 #ifdef __OBJC__ 553 #ifdef __OBJC__
549 // This is for situations in WebKit where the long standing behavior has been 554 // This is for situations in WebKit where the long standing behavior has been
550 // "nil if empty", so we try to maintain longstanding behavior for the sake of 555 // "nil if empty", so we try to maintain longstanding behavior for the sake of
551 // entrenched clients 556 // entrenched clients
552 inline NSString* nsStringNilIfEmpty(const String& str) { return str.isEmpty() ? nil : (NSString*)str; } 557 inline NSString* nsStringNilIfEmpty(const String& str) {
558 return str.isEmpty() ? nil : (NSString*)str;
559 }
553 #endif 560 #endif
554 561
555 inline bool String::containsOnlyASCII() const 562 inline bool String::containsOnlyASCII() const {
556 { 563 if (isEmpty())
557 if (isEmpty()) 564 return true;
558 return true; 565
559 566 if (is8Bit())
560 if (is8Bit()) 567 return charactersAreAllASCII(characters8(), m_impl->length());
561 return charactersAreAllASCII(characters8(), m_impl->length()); 568
562 569 return charactersAreAllASCII(characters16(), m_impl->length());
563 return charactersAreAllASCII(characters16(), m_impl->length());
564 } 570 }
565 571
566 WTF_EXPORT int codePointCompare(const String&, const String&); 572 WTF_EXPORT int codePointCompare(const String&, const String&);
567 573
568 inline bool codePointCompareLessThan(const String& a, const String& b) 574 inline bool codePointCompareLessThan(const String& a, const String& b) {
569 { 575 return codePointCompare(a.impl(), b.impl()) < 0;
570 return codePointCompare(a.impl(), b.impl()) < 0; 576 }
571 } 577
572 578 template <size_t inlineCapacity>
573 template<size_t inlineCapacity> 579 inline void append(Vector<UChar, inlineCapacity>& vector, const String& string) {
574 inline void append(Vector<UChar, inlineCapacity>& vector, const String& string) 580 unsigned length = string.length();
575 { 581 if (!length)
576 unsigned length = string.length(); 582 return;
577 if (!length) 583 if (string.is8Bit()) {
578 return; 584 const LChar* characters8 = string.characters8();
579 if (string.is8Bit()) { 585 vector.reserveCapacity(vector.size() + length);
580 const LChar* characters8 = string.characters8(); 586 for (size_t i = 0; i < length; ++i)
581 vector.reserveCapacity(vector.size() + length); 587 vector.uncheckedAppend(characters8[i]);
582 for (size_t i = 0; i < length; ++i) 588 } else {
583 vector.uncheckedAppend(characters8[i]); 589 vector.append(string.characters16(), length);
584 } else { 590 }
585 vector.append(string.characters16(), length); 591 }
586 } 592
587 } 593 template <bool isSpecialCharacter(UChar), typename CharacterType>
588 594 inline bool isAllSpecialCharacters(const CharacterType* characters, size_t lengt h) {
589 template<bool isSpecialCharacter(UChar), typename CharacterType> 595 for (size_t i = 0; i < length; ++i) {
590 inline bool isAllSpecialCharacters(const CharacterType* characters, size_t lengt h) 596 if (!isSpecialCharacter(characters[i]))
591 { 597 return false;
592 for (size_t i = 0; i < length; ++i) { 598 }
593 if (!isSpecialCharacter(characters[i])) 599 return true;
594 return false; 600 }
595 } 601
602 template <bool isSpecialCharacter(UChar)>
603 inline bool String::isAllSpecialCharacters() const {
604 size_t len = length();
605
606 if (!len)
596 return true; 607 return true;
597 } 608
598 609 if (is8Bit())
599 template<bool isSpecialCharacter(UChar)> 610 return WTF::isAllSpecialCharacters<isSpecialCharacter, LChar>(characters8(), len);
600 inline bool String::isAllSpecialCharacters() const 611 return WTF::isAllSpecialCharacters<isSpecialCharacter, UChar>(characters16(), len);
601 { 612 }
602 size_t len = length(); 613
603 614 template <size_t inlineCapacity>
604 if (!len) 615 inline void String::appendTo(Vector<UChar, inlineCapacity>& result, unsigned pos , unsigned len) const {
605 return true; 616 unsigned numberOfCharactersToCopy = std::min(len, length() - pos);
606 617 if (!numberOfCharactersToCopy)
607 if (is8Bit()) 618 return;
608 return WTF::isAllSpecialCharacters<isSpecialCharacter, LChar>(characters 8(), len); 619 result.reserveCapacity(result.size() + numberOfCharactersToCopy);
609 return WTF::isAllSpecialCharacters<isSpecialCharacter, UChar>(characters16() , len); 620 if (is8Bit()) {
610 } 621 const LChar* characters8 = m_impl->characters8();
611 622 for (size_t i = 0; i < numberOfCharactersToCopy; ++i)
612 template<size_t inlineCapacity> 623 result.uncheckedAppend(characters8[pos + i]);
613 inline void String::appendTo(Vector<UChar, inlineCapacity>& result, unsigned pos , unsigned len) const 624 } else {
614 { 625 const UChar* characters16 = m_impl->characters16();
615 unsigned numberOfCharactersToCopy = std::min(len, length() - pos); 626 result.append(characters16 + pos, numberOfCharactersToCopy);
616 if (!numberOfCharactersToCopy) 627 }
617 return; 628 }
618 result.reserveCapacity(result.size() + numberOfCharactersToCopy); 629
619 if (is8Bit()) { 630 template <typename BufferType>
620 const LChar* characters8 = m_impl->characters8(); 631 inline void String::appendTo(BufferType& result, unsigned pos, unsigned len) con st {
621 for (size_t i = 0; i < numberOfCharactersToCopy; ++i) 632 unsigned numberOfCharactersToCopy = std::min(len, length() - pos);
622 result.uncheckedAppend(characters8[pos + i]); 633 if (!numberOfCharactersToCopy)
623 } else { 634 return;
624 const UChar* characters16 = m_impl->characters16(); 635 if (is8Bit())
625 result.append(characters16 + pos, numberOfCharactersToCopy); 636 result.append(m_impl->characters8() + pos, numberOfCharactersToCopy);
626 } 637 else
627 } 638 result.append(m_impl->characters16() + pos, numberOfCharactersToCopy);
628 639 }
629 template<typename BufferType> 640
630 inline void String::appendTo(BufferType& result, unsigned pos, unsigned len) con st 641 template <size_t inlineCapacity>
631 { 642 inline void String::prependTo(Vector<UChar, inlineCapacity>& result, unsigned po s, unsigned len) const {
632 unsigned numberOfCharactersToCopy = std::min(len, length() - pos); 643 unsigned numberOfCharactersToCopy = std::min(len, length() - pos);
633 if (!numberOfCharactersToCopy) 644 if (!numberOfCharactersToCopy)
634 return; 645 return;
635 if (is8Bit()) 646 if (is8Bit()) {
636 result.append(m_impl->characters8() + pos, numberOfCharactersToCopy); 647 size_t oldSize = result.size();
637 else 648 result.resize(oldSize + numberOfCharactersToCopy);
638 result.append(m_impl->characters16() + pos, numberOfCharactersToCopy); 649 memmove(result.data() + numberOfCharactersToCopy, result.data(), oldSize * s izeof(UChar));
639 } 650 StringImpl::copyChars(result.data(), m_impl->characters8() + pos, numberOfCh aractersToCopy);
640 651 } else {
641 template<size_t inlineCapacity> 652 result.prepend(m_impl->characters16() + pos, numberOfCharactersToCopy);
642 inline void String::prependTo(Vector<UChar, inlineCapacity>& result, unsigned po s, unsigned len) const 653 }
643 {
644 unsigned numberOfCharactersToCopy = std::min(len, length() - pos);
645 if (!numberOfCharactersToCopy)
646 return;
647 if (is8Bit()) {
648 size_t oldSize = result.size();
649 result.resize(oldSize + numberOfCharactersToCopy);
650 memmove(result.data() + numberOfCharactersToCopy, result.data(), oldSize * sizeof(UChar));
651 StringImpl::copyChars(result.data(), m_impl->characters8() + pos, number OfCharactersToCopy);
652 } else {
653 result.prepend(m_impl->characters16() + pos, numberOfCharactersToCopy);
654 }
655 } 654 }
656 655
657 // StringHash is the default hash for String 656 // StringHash is the default hash for String
658 template<typename T> struct DefaultHash; 657 template <typename T>
659 template<> struct DefaultHash<String> { 658 struct DefaultHash;
660 typedef StringHash Hash; 659 template <>
660 struct DefaultHash<String> {
661 typedef StringHash Hash;
661 }; 662 };
662 663
663 // Shared global empty string. 664 // Shared global empty string.
664 WTF_EXPORT const String& emptyString(); 665 WTF_EXPORT const String& emptyString();
665 WTF_EXPORT const String& emptyString16Bit(); 666 WTF_EXPORT const String& emptyString16Bit();
666 WTF_EXPORT extern const String& xmlnsWithColon; 667 WTF_EXPORT extern const String& xmlnsWithColon;
667 668
668 // Pretty printer for gtest. Declared here to avoid ODR violations. 669 // Pretty printer for gtest. Declared here to avoid ODR violations.
669 WTF_UNITTEST_HELPERS_EXPORT std::ostream& operator<<(std::ostream&, const String &); 670 WTF_UNITTEST_HELPERS_EXPORT std::ostream& operator<<(std::ostream&, const String &);
670 671
671 } // namespace WTF 672 } // namespace WTF
672 673
673 WTF_ALLOW_MOVE_AND_INIT_WITH_MEM_FUNCTIONS(String); 674 WTF_ALLOW_MOVE_AND_INIT_WITH_MEM_FUNCTIONS(String);
674 675
675 using WTF::CString; 676 using WTF::CString;
676 using WTF::KeepTrailingZeros; 677 using WTF::KeepTrailingZeros;
677 using WTF::StrictUTF8Conversion; 678 using WTF::StrictUTF8Conversion;
678 using WTF::StrictUTF8ConversionReplacingUnpairedSurrogatesWithFFFD; 679 using WTF::StrictUTF8ConversionReplacingUnpairedSurrogatesWithFFFD;
679 using WTF::String; 680 using WTF::String;
680 using WTF::emptyString; 681 using WTF::emptyString;
681 using WTF::emptyString16Bit; 682 using WTF::emptyString16Bit;
(...skipping 10 matching lines...) Expand all
692 using WTF::charactersToDouble; 693 using WTF::charactersToDouble;
693 using WTF::charactersToFloat; 694 using WTF::charactersToFloat;
694 using WTF::equal; 695 using WTF::equal;
695 using WTF::equalIgnoringCase; 696 using WTF::equalIgnoringCase;
696 using WTF::find; 697 using WTF::find;
697 using WTF::isAllSpecialCharacters; 698 using WTF::isAllSpecialCharacters;
698 using WTF::isSpaceOrNewline; 699 using WTF::isSpaceOrNewline;
699 using WTF::reverseFind; 700 using WTF::reverseFind;
700 701
701 #include "wtf/text/AtomicString.h" 702 #include "wtf/text/AtomicString.h"
702 #endif // WTFString_h 703 #endif // WTFString_h
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/wtf/text/Unicode.h ('k') | third_party/WebKit/Source/wtf/text/WTFString.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698