2 * (C) 1999 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2010, 2012 Apple Inc. All rights reserved.
4 * Copyright (C) 2007-2009 Torch Mobile, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 #include "WTFString.h"
25 #include "IntegerToStringConversion.h"
27 #include "wtf/ASCIICType.h"
28 #include "wtf/DataLog.h"
29 #include "wtf/HexNumber.h"
30 #include "wtf/MathExtras.h"
31 #include "wtf/text/CString.h"
32 #include "wtf/StringExtras.h"
33 #include "wtf/Vector.h"
35 #include "wtf/unicode/CharacterNames.h"
36 #include "wtf/unicode/UTF8.h"
37 #include "wtf/unicode/Unicode.h"
43 using namespace Unicode;
46 // Construct a string with UTF-16 data.
47 String::String(const UChar* characters, unsigned length)
48 : m_impl(characters ? StringImpl::create(characters, length) : nullptr)
52 // Construct a string with UTF-16 data, from a null-terminated source.
53 String::String(const UChar* str)
57 m_impl = StringImpl::create(str, lengthOfNullTerminatedString(str));
60 // Construct a string with latin1 data.
61 String::String(const LChar* characters, unsigned length)
62 : m_impl(characters ? StringImpl::create(characters, length) : nullptr)
66 String::String(const char* characters, unsigned length)
67 : m_impl(characters ? StringImpl::create(reinterpret_cast<const LChar*>(characters), length) : nullptr)
71 // Construct a string with latin1 data, from a null-terminated source.
72 String::String(const LChar* characters)
73 : m_impl(characters ? StringImpl::create(characters) : nullptr)
77 String::String(const char* characters)
78 : m_impl(characters ? StringImpl::create(reinterpret_cast<const LChar*>(characters)) : nullptr)
82 void String::append(const String& string)
87 m_impl = string.m_impl;
91 // FIXME: This is extremely inefficient. So much so that we might want to take this
92 // out of String's API. We can make it better by optimizing the case where exactly
93 // one String is pointing at this StringImpl, but even then it's going to require a
94 // call into the allocator every single time.
96 if (m_impl->is8Bit() && string.m_impl->is8Bit()) {
98 RELEASE_ASSERT(string.length() <= numeric_limits<unsigned>::max() - m_impl->length());
99 RefPtr<StringImpl> newImpl = StringImpl::createUninitialized(m_impl->length() + string.length(), data);
100 memcpy(data, m_impl->characters8(), m_impl->length() * sizeof(LChar));
101 memcpy(data + m_impl->length(), string.characters8(), string.length() * sizeof(LChar));
102 m_impl = newImpl.release();
107 RELEASE_ASSERT(string.length() <= numeric_limits<unsigned>::max() - m_impl->length());
108 RefPtr<StringImpl> newImpl = StringImpl::createUninitialized(m_impl->length() + string.length(), data);
110 if (m_impl->is8Bit())
111 StringImpl::copyChars(data, m_impl->characters8(), m_impl->length());
113 StringImpl::copyChars(data, m_impl->characters16(), m_impl->length());
115 if (string.impl()->is8Bit())
116 StringImpl::copyChars(data + m_impl->length(), string.impl()->characters8(), string.impl()->length());
118 StringImpl::copyChars(data + m_impl->length(), string.impl()->characters16(), string.impl()->length());
120 m_impl = newImpl.release();
123 template <typename CharacterType>
124 inline void String::appendInternal(CharacterType c)
126 // FIXME: This is extremely inefficient. So much so that we might want to take this
127 // out of String's API. We can make it better by optimizing the case where exactly
128 // one String is pointing at this StringImpl, but even then it's going to require a
129 // call into the allocator every single time.
131 m_impl = StringImpl::create(&c, 1);
135 UChar* data; // FIXME: We should be able to create an 8 bit string via this code path.
136 RELEASE_ASSERT(m_impl->length() < numeric_limits<unsigned>::max());
137 RefPtr<StringImpl> newImpl = StringImpl::createUninitialized(m_impl->length() + 1, data);
138 if (m_impl->is8Bit())
139 StringImpl::copyChars(data, m_impl->characters8(), m_impl->length());
141 StringImpl::copyChars(data, m_impl->characters16(), m_impl->length());
142 data[m_impl->length()] = c;
143 m_impl = newImpl.release();
146 void String::append(LChar c)
151 void String::append(UChar c)
156 int codePointCompare(const String& a, const String& b)
158 return codePointCompare(a.impl(), b.impl());
161 void String::insert(const String& string, unsigned position)
163 if (string.isEmpty()) {
167 m_impl = string.impl();
172 insert(string.impl()->characters8(), string.length(), position);
174 insert(string.impl()->characters16(), string.length(), position);
177 void String::append(const LChar* charactersToAppend, unsigned lengthToAppend)
180 if (!charactersToAppend)
182 m_impl = StringImpl::create(charactersToAppend, lengthToAppend);
189 ASSERT(charactersToAppend);
191 unsigned strLength = m_impl->length();
193 if (m_impl->is8Bit()) {
194 RELEASE_ASSERT(lengthToAppend <= numeric_limits<unsigned>::max() - strLength);
196 RefPtr<StringImpl> newImpl = StringImpl::createUninitialized(strLength + lengthToAppend, data);
197 StringImpl::copyChars(data, m_impl->characters8(), strLength);
198 StringImpl::copyChars(data + strLength, charactersToAppend, lengthToAppend);
199 m_impl = newImpl.release();
203 RELEASE_ASSERT(lengthToAppend <= numeric_limits<unsigned>::max() - strLength);
205 RefPtr<StringImpl> newImpl = StringImpl::createUninitialized(length() + lengthToAppend, data);
206 StringImpl::copyChars(data, m_impl->characters16(), strLength);
207 StringImpl::copyChars(data + strLength, charactersToAppend, lengthToAppend);
208 m_impl = newImpl.release();
211 void String::append(const UChar* charactersToAppend, unsigned lengthToAppend)
214 if (!charactersToAppend)
216 m_impl = StringImpl::create(charactersToAppend, lengthToAppend);
223 unsigned strLength = m_impl->length();
225 ASSERT(charactersToAppend);
226 RELEASE_ASSERT(lengthToAppend <= numeric_limits<unsigned>::max() - strLength);
228 RefPtr<StringImpl> newImpl = StringImpl::createUninitialized(strLength + lengthToAppend, data);
229 if (m_impl->is8Bit())
230 StringImpl::copyChars(data, characters8(), strLength);
232 StringImpl::copyChars(data, characters16(), strLength);
233 StringImpl::copyChars(data + strLength, charactersToAppend, lengthToAppend);
234 m_impl = newImpl.release();
237 template<typename CharType>
238 PassRefPtr<StringImpl> insertInternal(PassRefPtr<StringImpl> impl, const CharType* charactersToInsert, unsigned lengthToInsert, unsigned position)
243 ASSERT(charactersToInsert);
244 UChar* data; // FIXME: We should be able to create an 8 bit string here.
245 RELEASE_ASSERT(lengthToInsert <= numeric_limits<unsigned>::max() - impl->length());
246 RefPtr<StringImpl> newImpl = StringImpl::createUninitialized(impl->length() + lengthToInsert, data);
249 StringImpl::copyChars(data, impl->characters8(), position);
251 StringImpl::copyChars(data, impl->characters16(), position);
253 StringImpl::copyChars(data + position, charactersToInsert, lengthToInsert);
256 StringImpl::copyChars(data + position + lengthToInsert, impl->characters8() + position, impl->length() - position);
258 StringImpl::copyChars(data + position + lengthToInsert, impl->characters16() + position, impl->length() - position);
260 return newImpl.release();
263 void String::insert(const UChar* charactersToInsert, unsigned lengthToInsert, unsigned position)
265 if (position >= length()) {
266 append(charactersToInsert, lengthToInsert);
270 m_impl = insertInternal(m_impl.release(), charactersToInsert, lengthToInsert, position);
273 void String::insert(const LChar* charactersToInsert, unsigned lengthToInsert, unsigned position)
275 if (position >= length()) {
276 append(charactersToInsert, lengthToInsert);
280 m_impl = insertInternal(m_impl.release(), charactersToInsert, lengthToInsert, position);
283 UChar32 String::characterStartingAt(unsigned i) const
285 if (!m_impl || i >= m_impl->length())
287 return m_impl->characterStartingAt(i);
290 void String::ensure16Bit()
292 unsigned length = this->length();
293 if (!length || !is8Bit())
295 m_impl = make16BitFrom8BitSource(m_impl->characters8(), length).impl();
298 void String::truncate(unsigned position)
300 if (position >= length())
302 if (m_impl->is8Bit()) {
304 RefPtr<StringImpl> newImpl = StringImpl::createUninitialized(position, data);
305 memcpy(data, m_impl->characters8(), position * sizeof(LChar));
306 m_impl = newImpl.release();
309 RefPtr<StringImpl> newImpl = StringImpl::createUninitialized(position, data);
310 memcpy(data, m_impl->characters16(), position * sizeof(UChar));
311 m_impl = newImpl.release();
315 template <typename CharacterType>
316 inline void String::removeInternal(const CharacterType* characters, unsigned position, int lengthToRemove)
319 RefPtr<StringImpl> newImpl = StringImpl::createUninitialized(length() - lengthToRemove, data);
320 memcpy(data, characters, position * sizeof(CharacterType));
321 memcpy(data + position, characters + position + lengthToRemove,
322 (length() - lengthToRemove - position) * sizeof(CharacterType));
324 m_impl = newImpl.release();
327 void String::remove(unsigned position, int lengthToRemove)
329 if (lengthToRemove <= 0)
331 if (position >= length())
333 if (static_cast<unsigned>(lengthToRemove) > length() - position)
334 lengthToRemove = length() - position;
337 removeInternal(characters8(), position, lengthToRemove);
342 removeInternal(characters16(), position, lengthToRemove);
345 String String::substring(unsigned pos, unsigned len) const
349 return m_impl->substring(pos, len);
352 String String::lower() const
356 return m_impl->lower();
359 String String::upper() const
363 return m_impl->upper();
366 String String::lower(const AtomicString& localeIdentifier) const
370 return m_impl->lower(localeIdentifier);
373 String String::upper(const AtomicString& localeIdentifier) const
377 return m_impl->upper(localeIdentifier);
380 String String::stripWhiteSpace() const
384 return m_impl->stripWhiteSpace();
387 String String::stripWhiteSpace(IsWhiteSpaceFunctionPtr isWhiteSpace) const
391 return m_impl->stripWhiteSpace(isWhiteSpace);
394 String String::simplifyWhiteSpace(StripBehavior stripBehavior) const
398 return m_impl->simplifyWhiteSpace(stripBehavior);
401 String String::simplifyWhiteSpace(IsWhiteSpaceFunctionPtr isWhiteSpace, StripBehavior stripBehavior) const
405 return m_impl->simplifyWhiteSpace(isWhiteSpace, stripBehavior);
408 String String::removeCharacters(CharacterMatchFunctionPtr findMatch) const
412 return m_impl->removeCharacters(findMatch);
415 String String::foldCase() const
419 return m_impl->foldCase();
422 Vector<UChar> String::charactersWithNullTermination() const
425 return Vector<UChar>();
427 Vector<UChar> result;
428 result.reserveInitialCapacity(length() + 1);
434 unsigned String::copyTo(UChar* buffer, unsigned pos, unsigned maxLength) const
436 unsigned length = this->length();
437 RELEASE_ASSERT(pos <= length);
438 unsigned numCharacters = std::min(length - pos, maxLength);
442 StringImpl::copyChars(buffer, characters8() + pos, numCharacters);
444 StringImpl::copyChars(buffer, characters16() + pos, numCharacters);
445 return numCharacters;
448 String String::format(const char *format, ...)
451 va_start(args, format);
453 Vector<char, 256> buffer;
455 // Do the format once to get the length.
457 int result = _vscprintf(format, args);
460 int result = vsnprintf(&ch, 1, format, args);
461 // We need to call va_end() and then va_start() again here, as the
462 // contents of args is undefined after the call to vsnprintf
463 // according to http://man.cx/snprintf(3)
465 // Not calling va_end/va_start here happens to work on lots of
466 // systems, but fails e.g. on 64bit Linux.
468 va_start(args, format);
475 unsigned len = result;
476 buffer.grow(len + 1);
478 // Now do the formatting again, guaranteed to fit.
479 vsnprintf(buffer.data(), buffer.size(), format, args);
483 return StringImpl::create(reinterpret_cast<const LChar*>(buffer.data()), len);
486 String String::number(int number)
488 return numberToStringSigned<String>(number);
491 String String::number(unsigned number)
493 return numberToStringUnsigned<String>(number);
496 String String::number(long number)
498 return numberToStringSigned<String>(number);
501 String String::number(unsigned long number)
503 return numberToStringUnsigned<String>(number);
506 String String::number(long long number)
508 return numberToStringSigned<String>(number);
511 String String::number(unsigned long long number)
513 return numberToStringUnsigned<String>(number);
516 String String::number(double number, unsigned precision, TrailingZerosTruncatingPolicy trailingZerosTruncatingPolicy)
518 NumberToStringBuffer buffer;
519 return String(numberToFixedPrecisionString(number, precision, buffer, trailingZerosTruncatingPolicy == TruncateTrailingZeros));
522 String String::numberToStringECMAScript(double number)
524 NumberToStringBuffer buffer;
525 return String(numberToString(number, buffer));
528 String String::numberToStringFixedWidth(double number, unsigned decimalPlaces)
530 NumberToStringBuffer buffer;
531 return String(numberToFixedWidthString(number, decimalPlaces, buffer));
534 int String::toIntStrict(bool* ok, int base) const
541 return m_impl->toIntStrict(ok, base);
544 unsigned String::toUIntStrict(bool* ok, int base) const
551 return m_impl->toUIntStrict(ok, base);
554 int64_t String::toInt64Strict(bool* ok, int base) const
561 return m_impl->toInt64Strict(ok, base);
564 uint64_t String::toUInt64Strict(bool* ok, int base) const
571 return m_impl->toUInt64Strict(ok, base);
574 int String::toInt(bool* ok) const
581 return m_impl->toInt(ok);
584 unsigned String::toUInt(bool* ok) const
591 return m_impl->toUInt(ok);
594 int64_t String::toInt64(bool* ok) const
601 return m_impl->toInt64(ok);
604 uint64_t String::toUInt64(bool* ok) const
611 return m_impl->toUInt64(ok);
614 double String::toDouble(bool* ok) const
621 return m_impl->toDouble(ok);
624 float String::toFloat(bool* ok) const
631 return m_impl->toFloat(ok);
634 String String::isolatedCopy() const
638 return m_impl->isolatedCopy();
641 bool String::isSafeToSendToAnotherThread() const
645 if (impl()->isStatic())
647 // AtomicStrings are not safe to send between threads as ~StringImpl()
648 // will try to remove them from the wrong AtomicStringTable.
649 if (impl()->isAtomic())
651 if (impl()->hasOneRef())
656 void String::split(const String& separator, bool allowEmptyEntries, Vector<String>& result) const
660 unsigned startPos = 0;
662 while ((endPos = find(separator, startPos)) != kNotFound) {
663 if (allowEmptyEntries || startPos != endPos)
664 result.append(substring(startPos, endPos - startPos));
665 startPos = endPos + separator.length();
667 if (allowEmptyEntries || startPos != length())
668 result.append(substring(startPos));
671 void String::split(UChar separator, bool allowEmptyEntries, Vector<String>& result) const
675 unsigned startPos = 0;
677 while ((endPos = find(separator, startPos)) != kNotFound) {
678 if (allowEmptyEntries || startPos != endPos)
679 result.append(substring(startPos, endPos - startPos));
680 startPos = endPos + 1;
682 if (allowEmptyEntries || startPos != length())
683 result.append(substring(startPos));
686 CString String::ascii() const
688 // Printable ASCII characters 32..127 and the null character are
689 // preserved, characters outside of this range are converted to '?'.
691 unsigned length = this->length();
693 char* characterBuffer;
694 return CString::newUninitialized(length, characterBuffer);
697 if (this->is8Bit()) {
698 const LChar* characters = this->characters8();
700 char* characterBuffer;
701 CString result = CString::newUninitialized(length, characterBuffer);
703 for (unsigned i = 0; i < length; ++i) {
704 LChar ch = characters[i];
705 characterBuffer[i] = ch && (ch < 0x20 || ch > 0x7f) ? '?' : ch;
711 const UChar* characters = this->characters16();
713 char* characterBuffer;
714 CString result = CString::newUninitialized(length, characterBuffer);
716 for (unsigned i = 0; i < length; ++i) {
717 UChar ch = characters[i];
718 characterBuffer[i] = ch && (ch < 0x20 || ch > 0x7f) ? '?' : static_cast<char>(ch);
724 CString String::latin1() const
726 // Basic Latin1 (ISO) encoding - Unicode characters 0..255 are
727 // preserved, characters outside of this range are converted to '?'.
729 unsigned length = this->length();
732 return CString("", 0);
735 return CString(reinterpret_cast<const char*>(this->characters8()), length);
737 const UChar* characters = this->characters16();
739 char* characterBuffer;
740 CString result = CString::newUninitialized(length, characterBuffer);
742 for (unsigned i = 0; i < length; ++i) {
743 UChar ch = characters[i];
744 characterBuffer[i] = ch > 0xff ? '?' : static_cast<char>(ch);
750 // Helper to write a three-byte UTF-8 code point to the buffer, caller must check room is available.
751 static inline void putUTF8Triple(char*& buffer, UChar ch)
753 ASSERT(ch >= 0x0800);
754 *buffer++ = static_cast<char>(((ch >> 12) & 0x0F) | 0xE0);
755 *buffer++ = static_cast<char>(((ch >> 6) & 0x3F) | 0x80);
756 *buffer++ = static_cast<char>((ch & 0x3F) | 0x80);
759 CString String::utf8(UTF8ConversionMode mode) const
761 unsigned length = this->length();
764 return CString("", 0);
766 // Allocate a buffer big enough to hold all the characters
767 // (an individual UTF-16 UChar can only expand to 3 UTF-8 bytes).
768 // Optimization ideas, if we find this function is hot:
769 // * We could speculatively create a CStringBuffer to contain 'length'
770 // characters, and resize if necessary (i.e. if the buffer contains
771 // non-ascii characters). (Alternatively, scan the buffer first for
772 // ascii characters, so we know this will be sufficient).
773 // * We could allocate a CStringBuffer with an appropriate size to
774 // have a good chance of being able to write the string into the
775 // buffer without reallocing (say, 1.5 x length).
776 if (length > numeric_limits<unsigned>::max() / 3)
778 Vector<char, 1024> bufferVector(length * 3);
780 char* buffer = bufferVector.data();
783 const LChar* characters = this->characters8();
785 ConversionResult result = convertLatin1ToUTF8(&characters, characters + length, &buffer, buffer + bufferVector.size());
786 ASSERT_UNUSED(result, result != targetExhausted); // (length * 3) should be sufficient for any conversion
788 const UChar* characters = this->characters16();
790 if (mode == StrictUTF8ConversionReplacingUnpairedSurrogatesWithFFFD) {
791 const UChar* charactersEnd = characters + length;
792 char* bufferEnd = buffer + bufferVector.size();
793 while (characters < charactersEnd) {
794 // Use strict conversion to detect unpaired surrogates.
795 ConversionResult result = convertUTF16ToUTF8(&characters, charactersEnd, &buffer, bufferEnd, true);
796 ASSERT(result != targetExhausted);
797 // Conversion fails when there is an unpaired surrogate.
798 // Put replacement character (U+FFFD) instead of the unpaired surrogate.
799 if (result != conversionOK) {
800 ASSERT((0xD800 <= *characters && *characters <= 0xDFFF));
801 // There should be room left, since one UChar hasn't been converted.
802 ASSERT((buffer + 3) <= bufferEnd);
803 putUTF8Triple(buffer, replacementCharacter);
808 bool strict = mode == StrictUTF8Conversion;
809 ConversionResult result = convertUTF16ToUTF8(&characters, characters + length, &buffer, buffer + bufferVector.size(), strict);
810 ASSERT(result != targetExhausted); // (length * 3) should be sufficient for any conversion
812 // Only produced from strict conversion.
813 if (result == sourceIllegal) {
818 // Check for an unconverted high surrogate.
819 if (result == sourceExhausted) {
822 // This should be one unpaired high surrogate. Treat it the same
823 // was as an unpaired high surrogate would have been handled in
824 // the middle of a string with non-strict conversion - which is
825 // to say, simply encode it to UTF-8.
826 ASSERT((characters + 1) == (this->characters16() + length));
827 ASSERT((*characters >= 0xD800) && (*characters <= 0xDBFF));
828 // There should be room left, since one UChar hasn't been converted.
829 ASSERT((buffer + 3) <= (buffer + bufferVector.size()));
830 putUTF8Triple(buffer, *characters);
835 return CString(bufferVector.data(), buffer - bufferVector.data());
838 String String::make8BitFrom16BitSource(const UChar* source, size_t length)
841 return emptyString();
844 String result = String::createUninitialized(length, destination);
846 copyLCharsFromUCharSource(destination, source, length);
851 String String::make16BitFrom8BitSource(const LChar* source, size_t length)
854 return emptyString16Bit();
857 String result = String::createUninitialized(length, destination);
859 StringImpl::copyChars(destination, source, length);
864 String String::fromUTF8(const LChar* stringStart, size_t length)
866 RELEASE_ASSERT(length <= numeric_limits<unsigned>::max());
872 return emptyString();
874 if (charactersAreAllASCII(stringStart, length))
875 return StringImpl::create(stringStart, length);
877 Vector<UChar, 1024> buffer(length);
878 UChar* bufferStart = buffer.data();
880 UChar* bufferCurrent = bufferStart;
881 const char* stringCurrent = reinterpret_cast<const char*>(stringStart);
882 if (convertUTF8ToUTF16(&stringCurrent, reinterpret_cast<const char *>(stringStart + length), &bufferCurrent, bufferCurrent + buffer.size()) != conversionOK)
885 unsigned utf16Length = bufferCurrent - bufferStart;
886 ASSERT(utf16Length < length);
887 return StringImpl::create(bufferStart, utf16Length);
890 String String::fromUTF8(const LChar* string)
894 return fromUTF8(string, strlen(reinterpret_cast<const char*>(string)));
897 String String::fromUTF8(const CString& s)
899 return fromUTF8(s.data());
902 String String::fromUTF8WithLatin1Fallback(const LChar* string, size_t size)
904 String utf8 = fromUTF8(string, size);
906 return String(string, size);
912 static bool isCharacterAllowedInBase(UChar c, int base)
917 return c - '0' < base;
918 if (isASCIIAlpha(c)) {
921 return (c >= 'a' && c < 'a' + base - 10)
922 || (c >= 'A' && c < 'A' + base - 10);
927 template <typename IntegralType, typename CharType>
928 static inline IntegralType toIntegralType(const CharType* data, size_t length, bool* ok, int base)
930 static const IntegralType integralMax = numeric_limits<IntegralType>::max();
931 static const bool isSigned = numeric_limits<IntegralType>::is_signed;
932 const IntegralType maxMultiplier = integralMax / base;
934 IntegralType value = 0;
936 bool isNegative = false;
941 // skip leading whitespace
942 while (length && isSpaceOrNewline(*data)) {
947 if (isSigned && length && *data == '-') {
951 } else if (length && *data == '+') {
956 if (!length || !isCharacterAllowedInBase(*data, base))
959 while (length && isCharacterAllowedInBase(*data, base)) {
961 IntegralType digitValue;
964 digitValue = c - '0';
966 digitValue = c - 'a' + 10;
968 digitValue = c - 'A' + 10;
970 if (value > maxMultiplier || (value == maxMultiplier && digitValue > (integralMax % base) + isNegative))
973 value = base * value + digitValue;
978 #pragma warning(push, 0)
979 #pragma warning(disable:4146)
989 // skip trailing space
990 while (length && isSpaceOrNewline(*data)) {
1000 return isOk ? value : 0;
1003 template <typename CharType>
1004 static unsigned lengthOfCharactersAsInteger(const CharType* data, size_t length)
1008 // Allow leading spaces.
1009 for (; i != length; ++i) {
1010 if (!isSpaceOrNewline(data[i]))
1015 if (i != length && (data[i] == '+' || data[i] == '-'))
1019 for (; i != length; ++i) {
1020 if (!isASCIIDigit(data[i]))
1027 int charactersToIntStrict(const LChar* data, size_t length, bool* ok, int base)
1029 return toIntegralType<int, LChar>(data, length, ok, base);
1032 int charactersToIntStrict(const UChar* data, size_t length, bool* ok, int base)
1034 return toIntegralType<int, UChar>(data, length, ok, base);
1037 unsigned charactersToUIntStrict(const LChar* data, size_t length, bool* ok, int base)
1039 return toIntegralType<unsigned, LChar>(data, length, ok, base);
1042 unsigned charactersToUIntStrict(const UChar* data, size_t length, bool* ok, int base)
1044 return toIntegralType<unsigned, UChar>(data, length, ok, base);
1047 int64_t charactersToInt64Strict(const LChar* data, size_t length, bool* ok, int base)
1049 return toIntegralType<int64_t, LChar>(data, length, ok, base);
1052 int64_t charactersToInt64Strict(const UChar* data, size_t length, bool* ok, int base)
1054 return toIntegralType<int64_t, UChar>(data, length, ok, base);
1057 uint64_t charactersToUInt64Strict(const LChar* data, size_t length, bool* ok, int base)
1059 return toIntegralType<uint64_t, LChar>(data, length, ok, base);
1062 uint64_t charactersToUInt64Strict(const UChar* data, size_t length, bool* ok, int base)
1064 return toIntegralType<uint64_t, UChar>(data, length, ok, base);
1067 int charactersToInt(const LChar* data, size_t length, bool* ok)
1069 return toIntegralType<int, LChar>(data, lengthOfCharactersAsInteger<LChar>(data, length), ok, 10);
1072 int charactersToInt(const UChar* data, size_t length, bool* ok)
1074 return toIntegralType<int, UChar>(data, lengthOfCharactersAsInteger(data, length), ok, 10);
1077 unsigned charactersToUInt(const LChar* data, size_t length, bool* ok)
1079 return toIntegralType<unsigned, LChar>(data, lengthOfCharactersAsInteger<LChar>(data, length), ok, 10);
1082 unsigned charactersToUInt(const UChar* data, size_t length, bool* ok)
1084 return toIntegralType<unsigned, UChar>(data, lengthOfCharactersAsInteger<UChar>(data, length), ok, 10);
1087 int64_t charactersToInt64(const LChar* data, size_t length, bool* ok)
1089 return toIntegralType<int64_t, LChar>(data, lengthOfCharactersAsInteger<LChar>(data, length), ok, 10);
1092 int64_t charactersToInt64(const UChar* data, size_t length, bool* ok)
1094 return toIntegralType<int64_t, UChar>(data, lengthOfCharactersAsInteger<UChar>(data, length), ok, 10);
1097 uint64_t charactersToUInt64(const LChar* data, size_t length, bool* ok)
1099 return toIntegralType<uint64_t, LChar>(data, lengthOfCharactersAsInteger<LChar>(data, length), ok, 10);
1102 uint64_t charactersToUInt64(const UChar* data, size_t length, bool* ok)
1104 return toIntegralType<uint64_t, UChar>(data, lengthOfCharactersAsInteger<UChar>(data, length), ok, 10);
1107 enum TrailingJunkPolicy { DisallowTrailingJunk, AllowTrailingJunk };
1109 template <typename CharType, TrailingJunkPolicy policy>
1110 static inline double toDoubleType(const CharType* data, size_t length, bool* ok, size_t& parsedLength)
1112 size_t leadingSpacesLength = 0;
1113 while (leadingSpacesLength < length && isASCIISpace(data[leadingSpacesLength]))
1114 ++leadingSpacesLength;
1116 double number = parseDouble(data + leadingSpacesLength, length - leadingSpacesLength, parsedLength);
1117 if (!parsedLength) {
1123 parsedLength += leadingSpacesLength;
1125 *ok = policy == AllowTrailingJunk || parsedLength == length;
1129 double charactersToDouble(const LChar* data, size_t length, bool* ok)
1131 size_t parsedLength;
1132 return toDoubleType<LChar, DisallowTrailingJunk>(data, length, ok, parsedLength);
1135 double charactersToDouble(const UChar* data, size_t length, bool* ok)
1137 size_t parsedLength;
1138 return toDoubleType<UChar, DisallowTrailingJunk>(data, length, ok, parsedLength);
1141 float charactersToFloat(const LChar* data, size_t length, bool* ok)
1143 // FIXME: This will return ok even when the string fits into a double but not a float.
1144 size_t parsedLength;
1145 return static_cast<float>(toDoubleType<LChar, DisallowTrailingJunk>(data, length, ok, parsedLength));
1148 float charactersToFloat(const UChar* data, size_t length, bool* ok)
1150 // FIXME: This will return ok even when the string fits into a double but not a float.
1151 size_t parsedLength;
1152 return static_cast<float>(toDoubleType<UChar, DisallowTrailingJunk>(data, length, ok, parsedLength));
1155 float charactersToFloat(const LChar* data, size_t length, size_t& parsedLength)
1157 // FIXME: This will return ok even when the string fits into a double but not a float.
1158 return static_cast<float>(toDoubleType<LChar, AllowTrailingJunk>(data, length, 0, parsedLength));
1161 float charactersToFloat(const UChar* data, size_t length, size_t& parsedLength)
1163 // FIXME: This will return ok even when the string fits into a double but not a float.
1164 return static_cast<float>(toDoubleType<UChar, AllowTrailingJunk>(data, length, 0, parsedLength));
1167 const String& emptyString()
1169 DEFINE_STATIC_LOCAL(String, emptyString, (StringImpl::empty()));
1173 const String& emptyString16Bit()
1175 DEFINE_STATIC_LOCAL(String, emptyString, (StringImpl::empty16Bit()));
1182 // For use in the debugger
1183 String* string(const char*);
1184 Vector<char> asciiDebug(StringImpl* impl);
1185 Vector<char> asciiDebug(String& string);
1187 void String::show() const
1189 dataLogF("%s\n", asciiDebug(impl()).data());
1192 String* string(const char* s)
1195 return new String(s);
1198 Vector<char> asciiDebug(StringImpl* impl)
1201 return asciiDebug(String("[null]").impl());
1203 Vector<char> buffer;
1204 for (unsigned i = 0; i < impl->length(); ++i) {
1205 UChar ch = (*impl)[i];
1206 if (isASCIIPrintable(ch)) {
1208 buffer.append('\\');
1209 buffer.append(static_cast<char>(ch));
1211 buffer.append('\\');
1213 appendUnsignedAsHexFixedSize(ch, buffer, 4);
1216 buffer.append('\0');
1220 Vector<char> asciiDebug(String& string)
1222 return asciiDebug(string.impl());