OLD | NEW |
| (Empty) |
1 /* | |
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. | |
4 * | |
5 * This library is free software; you can redistribute it and/or | |
6 * modify it under the terms of the GNU Library General Public | |
7 * License as published by the Free Software Foundation; either | |
8 * version 2 of the License, or (at your option) any later version. | |
9 * | |
10 * This library is distributed in the hope that it will be useful, | |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 * Library General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU Library General Public License | |
16 * along with this library; see the file COPYING.LIB. If not, write to | |
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | |
18 * Boston, MA 02110-1301, USA. | |
19 * | |
20 */ | |
21 | |
22 #ifndef WTFString_h | |
23 #define WTFString_h | |
24 | |
25 // This file would be called String.h, but that conflicts with <string.h> | |
26 // on systems without case-sensitive file systems. | |
27 | |
28 #include <wtf/text/ASCIIFastPath.h> | |
29 #include <wtf/text/StringImpl.h> | |
30 | |
31 #ifdef __OBJC__ | |
32 #include <objc/objc.h> | |
33 #endif | |
34 | |
35 namespace WTF { | |
36 | |
37 class CString; | |
38 class MemoryObjectInfo; | |
39 struct StringHash; | |
40 | |
41 // Declarations of string operations | |
42 | |
43 WTF_EXPORT_STRING_API int charactersToIntStrict(const LChar*, size_t, bool* ok =
0, int base = 10); | |
44 WTF_EXPORT_STRING_API int charactersToIntStrict(const UChar*, size_t, bool* ok =
0, int base = 10); | |
45 WTF_EXPORT_STRING_API unsigned charactersToUIntStrict(const LChar*, size_t, bool
* ok = 0, int base = 10); | |
46 WTF_EXPORT_STRING_API unsigned charactersToUIntStrict(const UChar*, size_t, bool
* ok = 0, int base = 10); | |
47 int64_t charactersToInt64Strict(const LChar*, size_t, bool* ok = 0, int base = 1
0); | |
48 int64_t charactersToInt64Strict(const UChar*, size_t, bool* ok = 0, int base = 1
0); | |
49 uint64_t charactersToUInt64Strict(const LChar*, size_t, bool* ok = 0, int base =
10); | |
50 uint64_t charactersToUInt64Strict(const UChar*, size_t, bool* ok = 0, int base =
10); | |
51 intptr_t charactersToIntPtrStrict(const LChar*, size_t, bool* ok = 0, int base =
10); | |
52 intptr_t charactersToIntPtrStrict(const UChar*, size_t, bool* ok = 0, int base =
10); | |
53 | |
54 int charactersToInt(const LChar*, size_t, bool* ok = 0); // ignores trailing gar
bage | |
55 WTF_EXPORT_STRING_API int charactersToInt(const UChar*, size_t, bool* ok = 0); /
/ ignores trailing garbage | |
56 unsigned charactersToUInt(const LChar*, size_t, bool* ok = 0); // ignores traili
ng garbage | |
57 unsigned charactersToUInt(const UChar*, size_t, bool* ok = 0); // ignores traili
ng garbage | |
58 int64_t charactersToInt64(const LChar*, size_t, bool* ok = 0); // ignores traili
ng garbage | |
59 int64_t charactersToInt64(const UChar*, size_t, bool* ok = 0); // ignores traili
ng garbage | |
60 uint64_t charactersToUInt64(const LChar*, size_t, bool* ok = 0); // ignores trai
ling garbage | |
61 uint64_t charactersToUInt64(const UChar*, size_t, bool* ok = 0); // ignores trai
ling garbage | |
62 intptr_t charactersToIntPtr(const LChar*, size_t, bool* ok = 0); // ignores trai
ling garbage | |
63 intptr_t charactersToIntPtr(const UChar*, size_t, bool* ok = 0); // ignores trai
ling garbage | |
64 | |
65 // FIXME: Like the strict functions above, these give false for "ok" when there
is trailing garbage. | |
66 // Like the non-strict functions above, these return the value when there is tra
iling garbage. | |
67 // It would be better if these were more consistent with the above functions ins
tead. | |
68 WTF_EXPORT_STRING_API double charactersToDouble(const LChar*, size_t, bool* ok =
0); | |
69 WTF_EXPORT_STRING_API double charactersToDouble(const UChar*, size_t, bool* ok =
0); | |
70 float charactersToFloat(const LChar*, size_t, bool* ok = 0); | |
71 WTF_EXPORT_STRING_API float charactersToFloat(const UChar*, size_t, bool* ok = 0
); | |
72 WTF_EXPORT_STRING_API float charactersToFloat(const LChar*, size_t, size_t& pars
edLength); | |
73 WTF_EXPORT_STRING_API float charactersToFloat(const UChar*, size_t, size_t& pars
edLength); | |
74 | |
75 class ASCIILiteral; | |
76 | |
77 enum TrailingZerosTruncatingPolicy { | |
78 KeepTrailingZeros, | |
79 TruncateTrailingZeros | |
80 }; | |
81 | |
82 template<bool isSpecialCharacter(UChar), typename CharacterType> | |
83 bool isAllSpecialCharacters(const CharacterType*, size_t); | |
84 | |
85 class String { | |
86 public: | |
87 // Construct a null string, distinguishable from an empty string. | |
88 String() { } | |
89 | |
90 // Construct a string with UTF-16 data. | |
91 WTF_EXPORT_STRING_API String(const UChar* characters, unsigned length); | |
92 | |
93 // Construct a string by copying the contents of a vector. To avoid | |
94 // copying, consider using String::adopt instead. | |
95 // This method will never create a null string. Vectors with size() == 0 | |
96 // will return the empty string. | |
97 // NOTE: This is different from String(vector.data(), vector.size()) | |
98 // which will sometimes return a null string when vector.data() is null | |
99 // which can only occur for vectors without inline capacity. | |
100 // See: https://bugs.webkit.org/show_bug.cgi?id=109792 | |
101 template<size_t inlineCapacity> | |
102 explicit String(const Vector<UChar, inlineCapacity>&); | |
103 | |
104 // Construct a string with UTF-16 data, from a null-terminated source. | |
105 WTF_EXPORT_STRING_API String(const UChar*); | |
106 | |
107 // Construct a string with latin1 data. | |
108 WTF_EXPORT_STRING_API String(const LChar* characters, unsigned length); | |
109 WTF_EXPORT_STRING_API String(const char* characters, unsigned length); | |
110 | |
111 // Construct a string with latin1 data, from a null-terminated source. | |
112 WTF_EXPORT_STRING_API String(const LChar* characters); | |
113 WTF_EXPORT_STRING_API String(const char* characters); | |
114 | |
115 // Construct a string referencing an existing StringImpl. | |
116 String(StringImpl* impl) : m_impl(impl) { } | |
117 String(PassRefPtr<StringImpl> impl) : m_impl(impl) { } | |
118 String(RefPtr<StringImpl> impl) : m_impl(impl) { } | |
119 | |
120 // Construct a string from a constant string literal. | |
121 WTF_EXPORT_STRING_API String(ASCIILiteral characters); | |
122 | |
123 // Construct a string from a constant string literal. | |
124 // This constructor is the "big" version, as it put the length in the functi
on call and generate bigger code. | |
125 enum ConstructFromLiteralTag { ConstructFromLiteral }; | |
126 template<unsigned charactersCount> | |
127 String(const char (&characters)[charactersCount], ConstructFromLiteralTag) :
m_impl(StringImpl::createFromLiteral<charactersCount>(characters)) { } | |
128 | |
129 #if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES) | |
130 // We have to declare the copy constructor and copy assignment operator as w
ell, otherwise | |
131 // they'll be implicitly deleted by adding the move constructor and move ass
ignment operator. | |
132 String(const String& other) : m_impl(other.m_impl) { } | |
133 String(String&& other) : m_impl(other.m_impl.release()) { } | |
134 String& operator=(const String& other) { m_impl = other.m_impl; return *this
; } | |
135 String& operator=(String&& other) { m_impl = other.m_impl.release(); return
*this; } | |
136 #endif | |
137 | |
138 // Inline the destructor. | |
139 ALWAYS_INLINE ~String() { } | |
140 | |
141 void swap(String& o) { m_impl.swap(o.m_impl); } | |
142 | |
143 static String adopt(StringBuffer<LChar>& buffer) { return StringImpl::adopt(
buffer); } | |
144 static String adopt(StringBuffer<UChar>& buffer) { return StringImpl::adopt(
buffer); } | |
145 template<typename CharacterType, size_t inlineCapacity> | |
146 static String adopt(Vector<CharacterType, inlineCapacity>& vector) { return
StringImpl::adopt(vector); } | |
147 | |
148 bool isNull() const { return !m_impl; } | |
149 bool isEmpty() const { return !m_impl || !m_impl->length(); } | |
150 | |
151 StringImpl* impl() const { return m_impl.get(); } | |
152 PassRefPtr<StringImpl> releaseImpl() { return m_impl.release(); } | |
153 | |
154 unsigned length() const | |
155 { | |
156 if (!m_impl) | |
157 return 0; | |
158 return m_impl->length(); | |
159 } | |
160 | |
161 const UChar* characters() const | |
162 { | |
163 if (!m_impl) | |
164 return 0; | |
165 return m_impl->characters(); | |
166 } | |
167 | |
168 const LChar* characters8() const | |
169 { | |
170 if (!m_impl) | |
171 return 0; | |
172 ASSERT(m_impl->is8Bit()); | |
173 return m_impl->characters8(); | |
174 } | |
175 | |
176 const UChar* characters16() const | |
177 { | |
178 if (!m_impl) | |
179 return 0; | |
180 ASSERT(!m_impl->is8Bit()); | |
181 return m_impl->characters16(); | |
182 } | |
183 | |
184 // Return characters8() or characters16() depending on CharacterType. | |
185 template <typename CharacterType> | |
186 inline const CharacterType* getCharacters() const; | |
187 | |
188 // Like getCharacters() and upconvert if CharacterType is UChar on a 8bit st
ring. | |
189 template <typename CharacterType> | |
190 inline const CharacterType* getCharactersWithUpconvert() const; | |
191 | |
192 bool is8Bit() const { return m_impl->is8Bit(); } | |
193 | |
194 unsigned sizeInBytes() const | |
195 { | |
196 if (!m_impl) | |
197 return 0; | |
198 return m_impl->length() * (is8Bit() ? sizeof(LChar) : sizeof(UChar)); | |
199 } | |
200 | |
201 WTF_EXPORT_STRING_API CString ascii() const; | |
202 WTF_EXPORT_STRING_API CString latin1() const; | |
203 | |
204 typedef enum { | |
205 LenientConversion, | |
206 StrictConversion, | |
207 StrictConversionReplacingUnpairedSurrogatesWithFFFD, | |
208 } ConversionMode; | |
209 | |
210 WTF_EXPORT_STRING_API CString utf8(ConversionMode = LenientConversion) const
; | |
211 | |
212 UChar operator[](unsigned index) const | |
213 { | |
214 if (!m_impl || index >= m_impl->length()) | |
215 return 0; | |
216 return (*m_impl)[index]; | |
217 } | |
218 | |
219 WTF_EXPORT_STRING_API static String number(int); | |
220 WTF_EXPORT_STRING_API static String number(unsigned int); | |
221 WTF_EXPORT_STRING_API static String number(long); | |
222 WTF_EXPORT_STRING_API static String number(unsigned long); | |
223 WTF_EXPORT_STRING_API static String number(long long); | |
224 WTF_EXPORT_STRING_API static String number(unsigned long long); | |
225 | |
226 WTF_EXPORT_STRING_API static String number(double, unsigned precision = 6, T
railingZerosTruncatingPolicy = TruncateTrailingZeros); | |
227 | |
228 // Number to String conversion following the ECMAScript definition. | |
229 WTF_EXPORT_STRING_API static String numberToStringECMAScript(double); | |
230 WTF_EXPORT_STRING_API static String numberToStringFixedWidth(double, unsigne
d decimalPlaces); | |
231 | |
232 // Find a single character or string, also with match function & latin1 form
s. | |
233 size_t find(UChar c, unsigned start = 0) const | |
234 { return m_impl ? m_impl->find(c, start) : notFound; } | |
235 | |
236 size_t find(const String& str) const | |
237 { return m_impl ? m_impl->find(str.impl()) : notFound; } | |
238 size_t find(const String& str, unsigned start) const | |
239 { return m_impl ? m_impl->find(str.impl(), start) : notFound; } | |
240 | |
241 size_t find(CharacterMatchFunctionPtr matchFunction, unsigned start = 0) con
st | |
242 { return m_impl ? m_impl->find(matchFunction, start) : notFound; } | |
243 size_t find(const LChar* str, unsigned start = 0) const | |
244 { return m_impl ? m_impl->find(str, start) : notFound; } | |
245 | |
246 size_t findNextLineStart(unsigned start = 0) const | |
247 { return m_impl ? m_impl->findNextLineStart(start) : notFound; } | |
248 | |
249 // Find the last instance of a single character or string. | |
250 size_t reverseFind(UChar c, unsigned start = UINT_MAX) const | |
251 { return m_impl ? m_impl->reverseFind(c, start) : notFound; } | |
252 size_t reverseFind(const String& str, unsigned start = UINT_MAX) const | |
253 { return m_impl ? m_impl->reverseFind(str.impl(), start) : notFound; } | |
254 | |
255 // Case insensitive string matching. | |
256 size_t findIgnoringCase(const LChar* str, unsigned start = 0) const | |
257 { return m_impl ? m_impl->findIgnoringCase(str, start) : notFound; } | |
258 size_t findIgnoringCase(const String& str, unsigned start = 0) const | |
259 { return m_impl ? m_impl->findIgnoringCase(str.impl(), start) : notFound
; } | |
260 size_t reverseFindIgnoringCase(const String& str, unsigned start = UINT_MAX)
const | |
261 { return m_impl ? m_impl->reverseFindIgnoringCase(str.impl(), start) : n
otFound; } | |
262 | |
263 // Wrappers for find & reverseFind adding dynamic sensitivity check. | |
264 size_t find(const LChar* str, unsigned start, bool caseSensitive) const | |
265 { return caseSensitive ? find(str, start) : findIgnoringCase(str, start)
; } | |
266 size_t find(const String& str, unsigned start, bool caseSensitive) const | |
267 { return caseSensitive ? find(str, start) : findIgnoringCase(str, start)
; } | |
268 size_t reverseFind(const String& str, unsigned start, bool caseSensitive) co
nst | |
269 { return caseSensitive ? reverseFind(str, start) : reverseFindIgnoringCa
se(str, start); } | |
270 | |
271 WTF_EXPORT_STRING_API const UChar* charactersWithNullTermination(); | |
272 | |
273 WTF_EXPORT_STRING_API UChar32 characterStartingAt(unsigned) const; // Ditto. | |
274 | |
275 bool contains(UChar c) const { return find(c) != notFound; } | |
276 bool contains(const LChar* str, bool caseSensitive = true) const { return fi
nd(str, 0, caseSensitive) != notFound; } | |
277 bool contains(const String& str, bool caseSensitive = true) const { return f
ind(str, 0, caseSensitive) != notFound; } | |
278 | |
279 bool startsWith(const String& s, bool caseSensitive = true) const | |
280 { return m_impl ? m_impl->startsWith(s.impl(), caseSensitive) : s.isEmpt
y(); } | |
281 bool startsWith(UChar character) const | |
282 { return m_impl ? m_impl->startsWith(character) : false; } | |
283 template<unsigned matchLength> | |
284 bool startsWith(const char (&prefix)[matchLength], bool caseSensitive = true
) const | |
285 { return m_impl ? m_impl->startsWith<matchLength>(prefix, caseSensitive)
: !matchLength; } | |
286 | |
287 bool endsWith(const String& s, bool caseSensitive = true) const | |
288 { return m_impl ? m_impl->endsWith(s.impl(), caseSensitive) : s.isEmpty(
); } | |
289 bool endsWith(UChar character) const | |
290 { return m_impl ? m_impl->endsWith(character) : false; } | |
291 template<unsigned matchLength> | |
292 bool endsWith(const char (&prefix)[matchLength], bool caseSensitive = true)
const | |
293 { return m_impl ? m_impl->endsWith<matchLength>(prefix, caseSensitive) :
!matchLength; } | |
294 | |
295 WTF_EXPORT_STRING_API void append(const String&); | |
296 WTF_EXPORT_STRING_API void append(LChar); | |
297 void append(char c) { append(static_cast<LChar>(c)); }; | |
298 WTF_EXPORT_STRING_API void append(UChar); | |
299 WTF_EXPORT_STRING_API void append(const LChar*, unsigned length); | |
300 WTF_EXPORT_STRING_API void append(const UChar*, unsigned length); | |
301 WTF_EXPORT_STRING_API void insert(const String&, unsigned pos); | |
302 void insert(const UChar*, unsigned length, unsigned pos); | |
303 | |
304 String& replace(UChar a, UChar b) { if (m_impl) m_impl = m_impl->replace(a,
b); return *this; } | |
305 String& replace(UChar a, const String& b) { if (m_impl) m_impl = m_impl->rep
lace(a, b.impl()); return *this; } | |
306 String& replace(const String& a, const String& b) { if (m_impl) m_impl = m_i
mpl->replace(a.impl(), b.impl()); return *this; } | |
307 String& replace(unsigned index, unsigned len, const String& b) { if (m_impl)
m_impl = m_impl->replace(index, len, b.impl()); return *this; } | |
308 | |
309 template<unsigned charactersCount> | |
310 ALWAYS_INLINE String& replaceWithLiteral(UChar a, const char (&characters)[c
haractersCount]) | |
311 { | |
312 if (m_impl) | |
313 m_impl = m_impl->replace(a, characters, charactersCount - 1); | |
314 | |
315 return *this; | |
316 } | |
317 | |
318 void makeLower() { if (m_impl) m_impl = m_impl->lower(); } | |
319 void makeUpper() { if (m_impl) m_impl = m_impl->upper(); } | |
320 void fill(UChar c) { if (m_impl) m_impl = m_impl->fill(c); } | |
321 | |
322 WTF_EXPORT_STRING_API void truncate(unsigned len); | |
323 WTF_EXPORT_STRING_API void remove(unsigned pos, int len = 1); | |
324 | |
325 WTF_EXPORT_STRING_API String substring(unsigned pos, unsigned len = UINT_MAX
) const; | |
326 WTF_EXPORT_STRING_API String substringSharingImpl(unsigned pos, unsigned len
= UINT_MAX) const; | |
327 String left(unsigned len) const { return substring(0, len); } | |
328 String right(unsigned len) const { return substring(length() - len, len); } | |
329 | |
330 // Returns a lowercase/uppercase version of the string | |
331 WTF_EXPORT_STRING_API String lower() const; | |
332 WTF_EXPORT_STRING_API String upper() const; | |
333 | |
334 WTF_EXPORT_STRING_API String stripWhiteSpace() const; | |
335 WTF_EXPORT_STRING_API String stripWhiteSpace(IsWhiteSpaceFunctionPtr) const; | |
336 WTF_EXPORT_STRING_API String simplifyWhiteSpace() const; | |
337 WTF_EXPORT_STRING_API String simplifyWhiteSpace(IsWhiteSpaceFunctionPtr) con
st; | |
338 | |
339 WTF_EXPORT_STRING_API String removeCharacters(CharacterMatchFunctionPtr) con
st; | |
340 template<bool isSpecialCharacter(UChar)> bool isAllSpecialCharacters() const
; | |
341 | |
342 // Return the string with case folded for case insensitive comparison. | |
343 WTF_EXPORT_STRING_API String foldCase() const; | |
344 | |
345 WTF_EXPORT_STRING_API static String format(const char *, ...) WTF_ATTRIBUTE_
PRINTF(1, 2); | |
346 | |
347 // Returns an uninitialized string. The characters needs to be written | |
348 // into the buffer returned in data before the returned string is used. | |
349 // Failure to do this will have unpredictable results. | |
350 static String createUninitialized(unsigned length, UChar*& data) { return St
ringImpl::createUninitialized(length, data); } | |
351 static String createUninitialized(unsigned length, LChar*& data) { return St
ringImpl::createUninitialized(length, data); } | |
352 | |
353 WTF_EXPORT_STRING_API void split(const String& separator, bool allowEmptyEnt
ries, Vector<String>& result) const; | |
354 void split(const String& separator, Vector<String>& result) const | |
355 { | |
356 split(separator, false, result); | |
357 } | |
358 WTF_EXPORT_STRING_API void split(UChar separator, bool allowEmptyEntries, Ve
ctor<String>& result) const; | |
359 void split(UChar separator, Vector<String>& result) const | |
360 { | |
361 split(separator, false, result); | |
362 } | |
363 | |
364 WTF_EXPORT_STRING_API int toIntStrict(bool* ok = 0, int base = 10) const; | |
365 WTF_EXPORT_STRING_API unsigned toUIntStrict(bool* ok = 0, int base = 10) con
st; | |
366 WTF_EXPORT_STRING_API int64_t toInt64Strict(bool* ok = 0, int base = 10) con
st; | |
367 uint64_t toUInt64Strict(bool* ok = 0, int base = 10) const; | |
368 intptr_t toIntPtrStrict(bool* ok = 0, int base = 10) const; | |
369 | |
370 WTF_EXPORT_STRING_API int toInt(bool* ok = 0) const; | |
371 WTF_EXPORT_STRING_API unsigned toUInt(bool* ok = 0) const; | |
372 int64_t toInt64(bool* ok = 0) const; | |
373 WTF_EXPORT_STRING_API uint64_t toUInt64(bool* ok = 0) const; | |
374 WTF_EXPORT_STRING_API intptr_t toIntPtr(bool* ok = 0) const; | |
375 | |
376 // FIXME: Like the strict functions above, these give false for "ok" when th
ere is trailing garbage. | |
377 // Like the non-strict functions above, these return the value when there is
trailing garbage. | |
378 // It would be better if these were more consistent with the above functions
instead. | |
379 WTF_EXPORT_STRING_API double toDouble(bool* ok = 0) const; | |
380 WTF_EXPORT_STRING_API float toFloat(bool* ok = 0) const; | |
381 | |
382 bool percentage(int& percentage) const; | |
383 | |
384 WTF_EXPORT_STRING_API String isolatedCopy() const; | |
385 WTF_EXPORT_STRING_API bool isSafeToSendToAnotherThread() const; | |
386 | |
387 // Prevent Strings from being implicitly convertable to bool as it will be a
mbiguous on any platform that | |
388 // allows implicit conversion to another pointer type (e.g., Mac allows impl
icit conversion to NSString*). | |
389 typedef struct ImplicitConversionFromWTFStringToBoolDisallowedA* (String::*U
nspecifiedBoolTypeA); | |
390 typedef struct ImplicitConversionFromWTFStringToBoolDisallowedB* (String::*U
nspecifiedBoolTypeB); | |
391 operator UnspecifiedBoolTypeA() const; | |
392 operator UnspecifiedBoolTypeB() const; | |
393 | |
394 #if USE(CF) | |
395 String(CFStringRef); | |
396 RetainPtr<CFStringRef> createCFString() const; | |
397 #endif | |
398 | |
399 #ifdef __OBJC__ | |
400 String(NSString*); | |
401 | |
402 // This conversion maps NULL to "", which loses the meaning of NULL, but we | |
403 // need this mapping because AppKit crashes when passed nil NSStrings. | |
404 operator NSString*() const { if (!m_impl) return @""; return *m_impl; } | |
405 #endif | |
406 | |
407 WTF_EXPORT_STRING_API static String make8BitFrom16BitSource(const UChar*, si
ze_t); | |
408 template<size_t inlineCapacity> | |
409 static String make8BitFrom16BitSource(const Vector<UChar, inlineCapacity>& b
uffer) | |
410 { | |
411 return make8BitFrom16BitSource(buffer.data(), buffer.size()); | |
412 } | |
413 | |
414 WTF_EXPORT_STRING_API static String make16BitFrom8BitSource(const LChar*, si
ze_t); | |
415 | |
416 // String::fromUTF8 will return a null string if | |
417 // the input data contains invalid UTF-8 sequences. | |
418 WTF_EXPORT_STRING_API static String fromUTF8(const LChar*, size_t); | |
419 WTF_EXPORT_STRING_API static String fromUTF8(const LChar*); | |
420 static String fromUTF8(const char* s, size_t length) { return fromUTF8(reint
erpret_cast<const LChar*>(s), length); }; | |
421 static String fromUTF8(const char* s) { return fromUTF8(reinterpret_cast<con
st LChar*>(s)); }; | |
422 static String fromUTF8(const CString&); | |
423 | |
424 // Tries to convert the passed in string to UTF-8, but will fall back to Lat
in-1 if the string is not valid UTF-8. | |
425 WTF_EXPORT_STRING_API static String fromUTF8WithLatin1Fallback(const LChar*,
size_t); | |
426 static String fromUTF8WithLatin1Fallback(const char* s, size_t length) { ret
urn fromUTF8WithLatin1Fallback(reinterpret_cast<const LChar*>(s), length); }; | |
427 | |
428 // Determines the writing direction using the Unicode Bidi Algorithm rules P
2 and P3. | |
429 WTF::Unicode::Direction defaultWritingDirection(bool* hasStrongDirectionalit
y = 0) const | |
430 { | |
431 if (m_impl) | |
432 return m_impl->defaultWritingDirection(hasStrongDirectionality); | |
433 if (hasStrongDirectionality) | |
434 *hasStrongDirectionality = false; | |
435 return WTF::Unicode::LeftToRight; | |
436 } | |
437 | |
438 bool containsOnlyASCII() const; | |
439 bool containsOnlyLatin1() const; | |
440 bool containsOnlyWhitespace() const { return !m_impl || m_impl->containsOnly
Whitespace(); } | |
441 | |
442 // Hash table deleted values, which are only constructed and never copied or
destroyed. | |
443 String(WTF::HashTableDeletedValueType) : m_impl(WTF::HashTableDeletedValue)
{ } | |
444 bool isHashTableDeletedValue() const { return m_impl.isHashTableDeletedValue
(); } | |
445 | |
446 #ifndef NDEBUG | |
447 WTF_EXPORT_STRING_API void show() const; | |
448 #endif | |
449 | |
450 // Workaround for a compiler bug. Use operator[] instead. | |
451 UChar characterAt(unsigned index) const | |
452 { | |
453 if (!m_impl || index >= m_impl->length()) | |
454 return 0; | |
455 return (*m_impl)[index]; | |
456 } | |
457 | |
458 private: | |
459 template <typename CharacterType> | |
460 void removeInternal(const CharacterType*, unsigned, int); | |
461 | |
462 RefPtr<StringImpl> m_impl; | |
463 }; | |
464 | |
465 inline bool operator==(const String& a, const String& b) { return equal(a.impl()
, b.impl()); } | |
466 inline bool operator==(const String& a, const LChar* b) { return equal(a.impl(),
b); } | |
467 inline bool operator==(const String& a, const char* b) { return equal(a.impl(),
reinterpret_cast<const LChar*>(b)); } | |
468 inline bool operator==(const LChar* a, const String& b) { return equal(a, b.impl
()); } | |
469 inline bool operator==(const char* a, const String& b) { return equal(reinterpre
t_cast<const LChar*>(a), b.impl()); } | |
470 template<size_t inlineCapacity> | |
471 inline bool operator==(const Vector<char, inlineCapacity>& a, const String& b) {
return equal(b.impl(), a.data(), a.size()); } | |
472 template<size_t inlineCapacity> | |
473 inline bool operator==(const String& a, const Vector<char, inlineCapacity>& b) {
return b == a; } | |
474 | |
475 | |
476 inline bool operator!=(const String& a, const String& b) { return !equal(a.impl(
), b.impl()); } | |
477 inline bool operator!=(const String& a, const LChar* b) { return !equal(a.impl()
, b); } | |
478 inline bool operator!=(const String& a, const char* b) { return !equal(a.impl(),
reinterpret_cast<const LChar*>(b)); } | |
479 inline bool operator!=(const LChar* a, const String& b) { return !equal(a, b.imp
l()); } | |
480 inline bool operator!=(const char* a, const String& b) { return !equal(reinterpr
et_cast<const LChar*>(a), b.impl()); } | |
481 template<size_t inlineCapacity> | |
482 inline bool operator!=(const Vector<char, inlineCapacity>& a, const String& b) {
return !(a == b); } | |
483 template<size_t inlineCapacity> | |
484 inline bool operator!=(const String& a, const Vector<char, inlineCapacity>& b) {
return b != a; } | |
485 | |
486 inline bool equalIgnoringCase(const String& a, const String& b) { return equalIg
noringCase(a.impl(), b.impl()); } | |
487 inline bool equalIgnoringCase(const String& a, const LChar* b) { return equalIgn
oringCase(a.impl(), b); } | |
488 inline bool equalIgnoringCase(const String& a, const char* b) { return equalIgno
ringCase(a.impl(), reinterpret_cast<const LChar*>(b)); } | |
489 inline bool equalIgnoringCase(const LChar* a, const String& b) { return equalIgn
oringCase(a, b.impl()); } | |
490 inline bool equalIgnoringCase(const char* a, const String& b) { return equalIgno
ringCase(reinterpret_cast<const LChar*>(a), b.impl()); } | |
491 | |
492 inline bool equalPossiblyIgnoringCase(const String& a, const String& b, bool ign
oreCase) | |
493 { | |
494 return ignoreCase ? equalIgnoringCase(a, b) : (a == b); | |
495 } | |
496 | |
497 inline bool equalIgnoringNullity(const String& a, const String& b) { return equa
lIgnoringNullity(a.impl(), b.impl()); } | |
498 | |
499 template<size_t inlineCapacity> | |
500 inline bool equalIgnoringNullity(const Vector<UChar, inlineCapacity>& a, const S
tring& b) { return equalIgnoringNullity(a, b.impl()); } | |
501 | |
502 inline bool operator!(const String& str) { return str.isNull(); } | |
503 | |
504 inline void swap(String& a, String& b) { a.swap(b); } | |
505 | |
506 // Definitions of string operations | |
507 | |
508 template<size_t inlineCapacity> | |
509 String::String(const Vector<UChar, inlineCapacity>& vector) | |
510 : m_impl(vector.size() ? StringImpl::create(vector.data(), vector.size()) :
StringImpl::empty()) | |
511 { | |
512 } | |
513 | |
514 template<> | |
515 inline const LChar* String::getCharacters<LChar>() const | |
516 { | |
517 ASSERT(is8Bit()); | |
518 return characters8(); | |
519 } | |
520 | |
521 template<> | |
522 inline const UChar* String::getCharacters<UChar>() const | |
523 { | |
524 ASSERT(!is8Bit()); | |
525 return characters16(); | |
526 } | |
527 | |
528 template<> | |
529 inline const LChar* String::getCharactersWithUpconvert<LChar>() const | |
530 { | |
531 ASSERT(is8Bit()); | |
532 return characters8(); | |
533 } | |
534 | |
535 template<> | |
536 inline const UChar* String::getCharactersWithUpconvert<UChar>() const | |
537 { | |
538 return characters(); | |
539 } | |
540 | |
541 inline bool String::containsOnlyLatin1() const | |
542 { | |
543 if (isEmpty()) | |
544 return true; | |
545 | |
546 if (is8Bit()) | |
547 return true; | |
548 | |
549 const UChar* characters = characters16(); | |
550 UChar ored = 0; | |
551 for (size_t i = 0; i < m_impl->length(); ++i) | |
552 ored |= characters[i]; | |
553 return !(ored & 0xFF00); | |
554 } | |
555 | |
556 | |
557 #ifdef __OBJC__ | |
558 // This is for situations in WebKit where the long standing behavior has been | |
559 // "nil if empty", so we try to maintain longstanding behavior for the sake of | |
560 // entrenched clients | |
561 inline NSString* nsStringNilIfEmpty(const String& str) { return str.isEmpty() ?
nil : (NSString*)str; } | |
562 #endif | |
563 | |
564 inline bool String::containsOnlyASCII() const | |
565 { | |
566 if (isEmpty()) | |
567 return true; | |
568 | |
569 if (is8Bit()) | |
570 return charactersAreAllASCII(characters8(), m_impl->length()); | |
571 | |
572 return charactersAreAllASCII(characters16(), m_impl->length()); | |
573 } | |
574 | |
575 WTF_EXPORT_STRING_API int codePointCompare(const String&, const String&); | |
576 | |
577 inline bool codePointCompareLessThan(const String& a, const String& b) | |
578 { | |
579 return codePointCompare(a.impl(), b.impl()) < 0; | |
580 } | |
581 | |
582 template<size_t inlineCapacity> | |
583 inline void append(Vector<UChar, inlineCapacity>& vector, const String& string) | |
584 { | |
585 vector.append(string.characters(), string.length()); | |
586 } | |
587 | |
588 template<typename CharacterType> | |
589 inline void appendNumber(Vector<CharacterType>& vector, unsigned char number) | |
590 { | |
591 int numberLength = number > 99 ? 3 : (number > 9 ? 2 : 1); | |
592 size_t vectorSize = vector.size(); | |
593 vector.grow(vectorSize + numberLength); | |
594 | |
595 switch (numberLength) { | |
596 case 3: | |
597 vector[vectorSize + 2] = number % 10 + '0'; | |
598 number /= 10; | |
599 | |
600 case 2: | |
601 vector[vectorSize + 1] = number % 10 + '0'; | |
602 number /= 10; | |
603 | |
604 case 1: | |
605 vector[vectorSize] = number % 10 + '0'; | |
606 } | |
607 } | |
608 | |
609 template<bool isSpecialCharacter(UChar), typename CharacterType> | |
610 inline bool isAllSpecialCharacters(const CharacterType* characters, size_t lengt
h) | |
611 { | |
612 for (size_t i = 0; i < length; ++i) { | |
613 if (!isSpecialCharacter(characters[i])) | |
614 return false; | |
615 } | |
616 return true; | |
617 } | |
618 | |
619 template<bool isSpecialCharacter(UChar)> | |
620 inline bool String::isAllSpecialCharacters() const | |
621 { | |
622 size_t len = length(); | |
623 | |
624 if (!len) | |
625 return true; | |
626 | |
627 if (is8Bit()) | |
628 return WTF::isAllSpecialCharacters<isSpecialCharacter, LChar>(characters
8(), len); | |
629 return WTF::isAllSpecialCharacters<isSpecialCharacter, UChar>(characters(),
len); | |
630 } | |
631 | |
632 // StringHash is the default hash for String | |
633 template<typename T> struct DefaultHash; | |
634 template<> struct DefaultHash<String> { | |
635 typedef StringHash Hash; | |
636 }; | |
637 | |
638 template <> struct VectorTraits<String> : SimpleClassVectorTraits { }; | |
639 | |
640 class ASCIILiteral { | |
641 public: | |
642 explicit ASCIILiteral(const char* characters) : m_characters(characters) { } | |
643 operator const char*() { return m_characters; } | |
644 | |
645 private: | |
646 const char* m_characters; | |
647 }; | |
648 | |
649 // Shared global empty string. | |
650 WTF_EXPORT_STRING_API const String& emptyString(); | |
651 | |
652 } | |
653 | |
654 using WTF::CString; | |
655 using WTF::KeepTrailingZeros; | |
656 using WTF::String; | |
657 using WTF::emptyString; | |
658 using WTF::append; | |
659 using WTF::appendNumber; | |
660 using WTF::charactersAreAllASCII; | |
661 using WTF::charactersToIntStrict; | |
662 using WTF::charactersToUIntStrict; | |
663 using WTF::charactersToInt64Strict; | |
664 using WTF::charactersToUInt64Strict; | |
665 using WTF::charactersToIntPtrStrict; | |
666 using WTF::charactersToInt; | |
667 using WTF::charactersToUInt; | |
668 using WTF::charactersToInt64; | |
669 using WTF::charactersToUInt64; | |
670 using WTF::charactersToIntPtr; | |
671 using WTF::charactersToDouble; | |
672 using WTF::charactersToFloat; | |
673 using WTF::equal; | |
674 using WTF::equalIgnoringCase; | |
675 using WTF::find; | |
676 using WTF::isAllSpecialCharacters; | |
677 using WTF::isSpaceOrNewline; | |
678 using WTF::reverseFind; | |
679 using WTF::ASCIILiteral; | |
680 | |
681 #include <wtf/text/AtomicString.h> | |
682 #endif | |
OLD | NEW |