]> git.saurik.com Git - apple/javascriptcore.git/blob - wtf/text/WTFString.cpp
JavaScriptCore-903.tar.gz
[apple/javascriptcore.git] / wtf / text / WTFString.cpp
1 /*
2 * (C) 1999 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2010 Apple Inc. All rights reserved.
4 * Copyright (C) 2007-2009 Torch Mobile, Inc.
5 *
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.
10 *
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.
15 *
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.
20 */
21
22 #include "config.h"
23 #include "WTFString.h"
24
25 #include <stdarg.h>
26 #include <wtf/ASCIICType.h>
27 #include <wtf/text/CString.h>
28 #include <wtf/StringExtras.h>
29 #include <wtf/Vector.h>
30 #include <wtf/dtoa.h>
31 #include <wtf/unicode/UTF8.h>
32 #include <wtf/unicode/Unicode.h>
33
34 using namespace std;
35
36 namespace WTF {
37
38 using namespace Unicode;
39 using namespace std;
40
41 // Construct a string with UTF-16 data.
42 String::String(const UChar* characters, unsigned length)
43 : m_impl(characters ? StringImpl::create(characters, length) : 0)
44 {
45 }
46
47 // Construct a string with UTF-16 data, from a null-terminated source.
48 String::String(const UChar* str)
49 {
50 if (!str)
51 return;
52
53 size_t len = 0;
54 while (str[len] != UChar(0))
55 len++;
56
57 if (len > numeric_limits<unsigned>::max())
58 CRASH();
59
60 m_impl = StringImpl::create(str, len);
61 }
62
63 // Construct a string with latin1 data.
64 String::String(const char* characters, unsigned length)
65 : m_impl(characters ? StringImpl::create(characters, length) : 0)
66 {
67 }
68
69 // Construct a string with latin1 data, from a null-terminated source.
70 String::String(const char* characters)
71 : m_impl(characters ? StringImpl::create(characters) : 0)
72 {
73 }
74
75 void String::append(const String& str)
76 {
77 if (str.isEmpty())
78 return;
79
80 // FIXME: This is extremely inefficient. So much so that we might want to take this
81 // out of String's API. We can make it better by optimizing the case where exactly
82 // one String is pointing at this StringImpl, but even then it's going to require a
83 // call to fastMalloc every single time.
84 if (str.m_impl) {
85 if (m_impl) {
86 UChar* data;
87 if (str.length() > numeric_limits<unsigned>::max() - m_impl->length())
88 CRASH();
89 RefPtr<StringImpl> newImpl = StringImpl::createUninitialized(m_impl->length() + str.length(), data);
90 memcpy(data, m_impl->characters(), m_impl->length() * sizeof(UChar));
91 memcpy(data + m_impl->length(), str.characters(), str.length() * sizeof(UChar));
92 m_impl = newImpl.release();
93 } else
94 m_impl = str.m_impl;
95 }
96 }
97
98 void String::append(char c)
99 {
100 // FIXME: This is extremely inefficient. So much so that we might want to take this
101 // out of String's API. We can make it better by optimizing the case where exactly
102 // one String is pointing at this StringImpl, but even then it's going to require a
103 // call to fastMalloc every single time.
104 if (m_impl) {
105 UChar* data;
106 if (m_impl->length() >= numeric_limits<unsigned>::max())
107 CRASH();
108 RefPtr<StringImpl> newImpl = StringImpl::createUninitialized(m_impl->length() + 1, data);
109 memcpy(data, m_impl->characters(), m_impl->length() * sizeof(UChar));
110 data[m_impl->length()] = c;
111 m_impl = newImpl.release();
112 } else
113 m_impl = StringImpl::create(&c, 1);
114 }
115
116 void String::append(UChar c)
117 {
118 // FIXME: This is extremely inefficient. So much so that we might want to take this
119 // out of String's API. We can make it better by optimizing the case where exactly
120 // one String is pointing at this StringImpl, but even then it's going to require a
121 // call to fastMalloc every single time.
122 if (m_impl) {
123 UChar* data;
124 if (m_impl->length() >= numeric_limits<unsigned>::max())
125 CRASH();
126 RefPtr<StringImpl> newImpl = StringImpl::createUninitialized(m_impl->length() + 1, data);
127 memcpy(data, m_impl->characters(), m_impl->length() * sizeof(UChar));
128 data[m_impl->length()] = c;
129 m_impl = newImpl.release();
130 } else
131 m_impl = StringImpl::create(&c, 1);
132 }
133
134 int codePointCompare(const String& a, const String& b)
135 {
136 return codePointCompare(a.impl(), b.impl());
137 }
138
139 void String::insert(const String& str, unsigned pos)
140 {
141 if (str.isEmpty()) {
142 if (str.isNull())
143 return;
144 if (isNull())
145 m_impl = str.impl();
146 return;
147 }
148 insert(str.characters(), str.length(), pos);
149 }
150
151 void String::append(const UChar* charactersToAppend, unsigned lengthToAppend)
152 {
153 if (!m_impl) {
154 if (!charactersToAppend)
155 return;
156 m_impl = StringImpl::create(charactersToAppend, lengthToAppend);
157 return;
158 }
159
160 if (!lengthToAppend)
161 return;
162
163 ASSERT(charactersToAppend);
164 UChar* data;
165 if (lengthToAppend > numeric_limits<unsigned>::max() - length())
166 CRASH();
167 RefPtr<StringImpl> newImpl = StringImpl::createUninitialized(length() + lengthToAppend, data);
168 memcpy(data, characters(), length() * sizeof(UChar));
169 memcpy(data + length(), charactersToAppend, lengthToAppend * sizeof(UChar));
170 m_impl = newImpl.release();
171 }
172
173 void String::insert(const UChar* charactersToInsert, unsigned lengthToInsert, unsigned position)
174 {
175 if (position >= length()) {
176 append(charactersToInsert, lengthToInsert);
177 return;
178 }
179
180 ASSERT(m_impl);
181
182 if (!lengthToInsert)
183 return;
184
185 ASSERT(charactersToInsert);
186 UChar* data;
187 if (lengthToInsert > numeric_limits<unsigned>::max() - length())
188 CRASH();
189 RefPtr<StringImpl> newImpl = StringImpl::createUninitialized(length() + lengthToInsert, data);
190 memcpy(data, characters(), position * sizeof(UChar));
191 memcpy(data + position, charactersToInsert, lengthToInsert * sizeof(UChar));
192 memcpy(data + position + lengthToInsert, characters() + position, (length() - position) * sizeof(UChar));
193 m_impl = newImpl.release();
194 }
195
196 UChar32 String::characterStartingAt(unsigned i) const
197 {
198 if (!m_impl || i >= m_impl->length())
199 return 0;
200 return m_impl->characterStartingAt(i);
201 }
202
203 void String::truncate(unsigned position)
204 {
205 if (position >= length())
206 return;
207 UChar* data;
208 RefPtr<StringImpl> newImpl = StringImpl::createUninitialized(position, data);
209 memcpy(data, characters(), position * sizeof(UChar));
210 m_impl = newImpl.release();
211 }
212
213 void String::remove(unsigned position, int lengthToRemove)
214 {
215 if (lengthToRemove <= 0)
216 return;
217 if (position >= length())
218 return;
219 if (static_cast<unsigned>(lengthToRemove) > length() - position)
220 lengthToRemove = length() - position;
221 UChar* data;
222 RefPtr<StringImpl> newImpl = StringImpl::createUninitialized(length() - lengthToRemove, data);
223 memcpy(data, characters(), position * sizeof(UChar));
224 memcpy(data + position, characters() + position + lengthToRemove,
225 (length() - lengthToRemove - position) * sizeof(UChar));
226 m_impl = newImpl.release();
227 }
228
229 String String::substring(unsigned pos, unsigned len) const
230 {
231 if (!m_impl)
232 return String();
233 return m_impl->substring(pos, len);
234 }
235
236 String String::substringSharingImpl(unsigned offset, unsigned length) const
237 {
238 // FIXME: We used to check against a limit of Heap::minExtraCost / sizeof(UChar).
239
240 unsigned stringLength = this->length();
241 offset = min(offset, stringLength);
242 length = min(length, stringLength - offset);
243
244 if (!offset && length == stringLength)
245 return *this;
246 return String(StringImpl::create(m_impl, offset, length));
247 }
248
249 String String::lower() const
250 {
251 if (!m_impl)
252 return String();
253 return m_impl->lower();
254 }
255
256 String String::upper() const
257 {
258 if (!m_impl)
259 return String();
260 return m_impl->upper();
261 }
262
263 String String::stripWhiteSpace() const
264 {
265 if (!m_impl)
266 return String();
267 return m_impl->stripWhiteSpace();
268 }
269
270 String String::simplifyWhiteSpace() const
271 {
272 if (!m_impl)
273 return String();
274 return m_impl->simplifyWhiteSpace();
275 }
276
277 String String::removeCharacters(CharacterMatchFunctionPtr findMatch) const
278 {
279 if (!m_impl)
280 return String();
281 return m_impl->removeCharacters(findMatch);
282 }
283
284 String String::foldCase() const
285 {
286 if (!m_impl)
287 return String();
288 return m_impl->foldCase();
289 }
290
291 bool String::percentage(int& result) const
292 {
293 if (!m_impl || !m_impl->length())
294 return false;
295
296 if ((*m_impl)[m_impl->length() - 1] != '%')
297 return false;
298
299 result = charactersToIntStrict(m_impl->characters(), m_impl->length() - 1);
300 return true;
301 }
302
303 const UChar* String::charactersWithNullTermination()
304 {
305 if (!m_impl)
306 return 0;
307 if (m_impl->hasTerminatingNullCharacter())
308 return m_impl->characters();
309 m_impl = StringImpl::createWithTerminatingNullCharacter(*m_impl);
310 return m_impl->characters();
311 }
312
313 String String::format(const char *format, ...)
314 {
315 #if PLATFORM(QT)
316 // Use QString::vsprintf to avoid the locale dependent formatting of vsnprintf.
317 // https://bugs.webkit.org/show_bug.cgi?id=18994
318 va_list args;
319 va_start(args, format);
320
321 QString buffer;
322 buffer.vsprintf(format, args);
323
324 va_end(args);
325
326 QByteArray ba = buffer.toUtf8();
327 return StringImpl::create(ba.constData(), ba.length());
328
329 #elif OS(WINCE)
330 va_list args;
331 va_start(args, format);
332
333 Vector<char, 256> buffer;
334
335 int bufferSize = 256;
336 buffer.resize(bufferSize);
337 for (;;) {
338 int written = vsnprintf(buffer.data(), bufferSize, format, args);
339 va_end(args);
340
341 if (written == 0)
342 return String("");
343 if (written > 0)
344 return StringImpl::create(buffer.data(), written);
345
346 bufferSize <<= 1;
347 buffer.resize(bufferSize);
348 va_start(args, format);
349 }
350
351 #else
352 va_list args;
353 va_start(args, format);
354
355 Vector<char, 256> buffer;
356
357 // Do the format once to get the length.
358 #if COMPILER(MSVC)
359 int result = _vscprintf(format, args);
360 #else
361 char ch;
362 int result = vsnprintf(&ch, 1, format, args);
363 // We need to call va_end() and then va_start() again here, as the
364 // contents of args is undefined after the call to vsnprintf
365 // according to http://man.cx/snprintf(3)
366 //
367 // Not calling va_end/va_start here happens to work on lots of
368 // systems, but fails e.g. on 64bit Linux.
369 va_end(args);
370 va_start(args, format);
371 #endif
372
373 if (result == 0)
374 return String("");
375 if (result < 0)
376 return String();
377 unsigned len = result;
378 buffer.grow(len + 1);
379
380 // Now do the formatting again, guaranteed to fit.
381 vsnprintf(buffer.data(), buffer.size(), format, args);
382
383 va_end(args);
384
385 return StringImpl::create(buffer.data(), len);
386 #endif
387 }
388
389 String String::number(short n)
390 {
391 return String::format("%hd", n);
392 }
393
394 String String::number(unsigned short n)
395 {
396 return String::format("%hu", n);
397 }
398
399 String String::number(int n)
400 {
401 return String::format("%d", n);
402 }
403
404 String String::number(unsigned n)
405 {
406 return String::format("%u", n);
407 }
408
409 String String::number(long n)
410 {
411 return String::format("%ld", n);
412 }
413
414 String String::number(unsigned long n)
415 {
416 return String::format("%lu", n);
417 }
418
419 String String::number(long long n)
420 {
421 #if OS(WINDOWS) && !PLATFORM(QT)
422 return String::format("%I64i", n);
423 #else
424 return String::format("%lli", n);
425 #endif
426 }
427
428 String String::number(unsigned long long n)
429 {
430 #if OS(WINDOWS) && !PLATFORM(QT)
431 return String::format("%I64u", n);
432 #else
433 return String::format("%llu", n);
434 #endif
435 }
436
437 String String::number(double n)
438 {
439 return String::format("%.6lg", n);
440 }
441
442 int String::toIntStrict(bool* ok, int base) const
443 {
444 if (!m_impl) {
445 if (ok)
446 *ok = false;
447 return 0;
448 }
449 return m_impl->toIntStrict(ok, base);
450 }
451
452 unsigned String::toUIntStrict(bool* ok, int base) const
453 {
454 if (!m_impl) {
455 if (ok)
456 *ok = false;
457 return 0;
458 }
459 return m_impl->toUIntStrict(ok, base);
460 }
461
462 int64_t String::toInt64Strict(bool* ok, int base) const
463 {
464 if (!m_impl) {
465 if (ok)
466 *ok = false;
467 return 0;
468 }
469 return m_impl->toInt64Strict(ok, base);
470 }
471
472 uint64_t String::toUInt64Strict(bool* ok, int base) const
473 {
474 if (!m_impl) {
475 if (ok)
476 *ok = false;
477 return 0;
478 }
479 return m_impl->toUInt64Strict(ok, base);
480 }
481
482 intptr_t String::toIntPtrStrict(bool* ok, int base) const
483 {
484 if (!m_impl) {
485 if (ok)
486 *ok = false;
487 return 0;
488 }
489 return m_impl->toIntPtrStrict(ok, base);
490 }
491
492
493 int String::toInt(bool* ok) const
494 {
495 if (!m_impl) {
496 if (ok)
497 *ok = false;
498 return 0;
499 }
500 return m_impl->toInt(ok);
501 }
502
503 unsigned String::toUInt(bool* ok) const
504 {
505 if (!m_impl) {
506 if (ok)
507 *ok = false;
508 return 0;
509 }
510 return m_impl->toUInt(ok);
511 }
512
513 int64_t String::toInt64(bool* ok) const
514 {
515 if (!m_impl) {
516 if (ok)
517 *ok = false;
518 return 0;
519 }
520 return m_impl->toInt64(ok);
521 }
522
523 uint64_t String::toUInt64(bool* ok) const
524 {
525 if (!m_impl) {
526 if (ok)
527 *ok = false;
528 return 0;
529 }
530 return m_impl->toUInt64(ok);
531 }
532
533 intptr_t String::toIntPtr(bool* ok) const
534 {
535 if (!m_impl) {
536 if (ok)
537 *ok = false;
538 return 0;
539 }
540 return m_impl->toIntPtr(ok);
541 }
542
543 double String::toDouble(bool* ok, bool* didReadNumber) const
544 {
545 if (!m_impl) {
546 if (ok)
547 *ok = false;
548 if (didReadNumber)
549 *didReadNumber = false;
550 return 0.0;
551 }
552 return m_impl->toDouble(ok, didReadNumber);
553 }
554
555 float String::toFloat(bool* ok, bool* didReadNumber) const
556 {
557 if (!m_impl) {
558 if (ok)
559 *ok = false;
560 if (didReadNumber)
561 *didReadNumber = false;
562 return 0.0f;
563 }
564 return m_impl->toFloat(ok, didReadNumber);
565 }
566
567 String String::threadsafeCopy() const
568 {
569 if (!m_impl)
570 return String();
571 return m_impl->threadsafeCopy();
572 }
573
574 String String::crossThreadString() const
575 {
576 if (!m_impl)
577 return String();
578 return m_impl->crossThreadString();
579 }
580
581 void String::split(const String& separator, bool allowEmptyEntries, Vector<String>& result) const
582 {
583 result.clear();
584
585 unsigned startPos = 0;
586 size_t endPos;
587 while ((endPos = find(separator, startPos)) != notFound) {
588 if (allowEmptyEntries || startPos != endPos)
589 result.append(substring(startPos, endPos - startPos));
590 startPos = endPos + separator.length();
591 }
592 if (allowEmptyEntries || startPos != length())
593 result.append(substring(startPos));
594 }
595
596 void String::split(const String& separator, Vector<String>& result) const
597 {
598 split(separator, false, result);
599 }
600
601 void String::split(UChar separator, bool allowEmptyEntries, Vector<String>& result) const
602 {
603 result.clear();
604
605 unsigned startPos = 0;
606 size_t endPos;
607 while ((endPos = find(separator, startPos)) != notFound) {
608 if (allowEmptyEntries || startPos != endPos)
609 result.append(substring(startPos, endPos - startPos));
610 startPos = endPos + 1;
611 }
612 if (allowEmptyEntries || startPos != length())
613 result.append(substring(startPos));
614 }
615
616 void String::split(UChar separator, Vector<String>& result) const
617 {
618 split(String(&separator, 1), false, result);
619 }
620
621 CString String::ascii() const
622 {
623 // Printable ASCII characters 32..127 and the null character are
624 // preserved, characters outside of this range are converted to '?'.
625
626 unsigned length = this->length();
627 const UChar* characters = this->characters();
628
629 char* characterBuffer;
630 CString result = CString::newUninitialized(length, characterBuffer);
631
632 for (unsigned i = 0; i < length; ++i) {
633 UChar ch = characters[i];
634 characterBuffer[i] = ch && (ch < 0x20 || ch > 0x7f) ? '?' : ch;
635 }
636
637 return result;
638 }
639
640 CString String::latin1() const
641 {
642 // Basic Latin1 (ISO) encoding - Unicode characters 0..255 are
643 // preserved, characters outside of this range are converted to '?'.
644
645 unsigned length = this->length();
646 const UChar* characters = this->characters();
647
648 char* characterBuffer;
649 CString result = CString::newUninitialized(length, characterBuffer);
650
651 for (unsigned i = 0; i < length; ++i) {
652 UChar ch = characters[i];
653 characterBuffer[i] = ch > 0xff ? '?' : ch;
654 }
655
656 return result;
657 }
658
659 // Helper to write a three-byte UTF-8 code point to the buffer, caller must check room is available.
660 static inline void putUTF8Triple(char*& buffer, UChar ch)
661 {
662 ASSERT(ch >= 0x0800);
663 *buffer++ = static_cast<char>(((ch >> 12) & 0x0F) | 0xE0);
664 *buffer++ = static_cast<char>(((ch >> 6) & 0x3F) | 0x80);
665 *buffer++ = static_cast<char>((ch & 0x3F) | 0x80);
666 }
667
668 CString String::utf8(bool strict) const
669 {
670 unsigned length = this->length();
671 const UChar* characters = this->characters();
672
673 // Allocate a buffer big enough to hold all the characters
674 // (an individual UTF-16 UChar can only expand to 3 UTF-8 bytes).
675 // Optimization ideas, if we find this function is hot:
676 // * We could speculatively create a CStringBuffer to contain 'length'
677 // characters, and resize if necessary (i.e. if the buffer contains
678 // non-ascii characters). (Alternatively, scan the buffer first for
679 // ascii characters, so we know this will be sufficient).
680 // * We could allocate a CStringBuffer with an appropriate size to
681 // have a good chance of being able to write the string into the
682 // buffer without reallocing (say, 1.5 x length).
683 if (length > numeric_limits<unsigned>::max() / 3)
684 return CString();
685 Vector<char, 1024> bufferVector(length * 3);
686
687 char* buffer = bufferVector.data();
688 ConversionResult result = convertUTF16ToUTF8(&characters, characters + length, &buffer, buffer + bufferVector.size(), strict);
689 ASSERT(result != targetExhausted); // (length * 3) should be sufficient for any conversion
690
691 // Only produced from strict conversion.
692 if (result == sourceIllegal)
693 return CString();
694
695 // Check for an unconverted high surrogate.
696 if (result == sourceExhausted) {
697 if (strict)
698 return CString();
699 // This should be one unpaired high surrogate. Treat it the same
700 // was as an unpaired high surrogate would have been handled in
701 // the middle of a string with non-strict conversion - which is
702 // to say, simply encode it to UTF-8.
703 ASSERT((characters + 1) == (this->characters() + length));
704 ASSERT((*characters >= 0xD800) && (*characters <= 0xDBFF));
705 // There should be room left, since one UChar hasn't been converted.
706 ASSERT((buffer + 3) <= (buffer + bufferVector.size()));
707 putUTF8Triple(buffer, *characters);
708 }
709
710 return CString(bufferVector.data(), buffer - bufferVector.data());
711 }
712
713 String String::fromUTF8(const char* stringStart, size_t length)
714 {
715 if (length > numeric_limits<unsigned>::max())
716 CRASH();
717
718 if (!stringStart)
719 return String();
720
721 // We'll use a StringImpl as a buffer; if the source string only contains ascii this should be
722 // the right length, if there are any multi-byte sequences this buffer will be too large.
723 UChar* buffer;
724 String stringBuffer(StringImpl::createUninitialized(length, buffer));
725 UChar* bufferEnd = buffer + length;
726
727 // Try converting into the buffer.
728 const char* stringCurrent = stringStart;
729 if (convertUTF8ToUTF16(&stringCurrent, stringStart + length, &buffer, bufferEnd) != conversionOK)
730 return String();
731
732 // stringBuffer is full (the input must have been all ascii) so just return it!
733 if (buffer == bufferEnd)
734 return stringBuffer;
735
736 // stringBuffer served its purpose as a buffer, copy the contents out into a new string.
737 unsigned utf16Length = buffer - stringBuffer.characters();
738 ASSERT(utf16Length < length);
739 return String(stringBuffer.characters(), utf16Length);
740 }
741
742 String String::fromUTF8(const char* string)
743 {
744 if (!string)
745 return String();
746 return fromUTF8(string, strlen(string));
747 }
748
749 String String::fromUTF8WithLatin1Fallback(const char* string, size_t size)
750 {
751 String utf8 = fromUTF8(string, size);
752 if (!utf8)
753 return String(string, size);
754 return utf8;
755 }
756
757 // String Operations
758
759 static bool isCharacterAllowedInBase(UChar c, int base)
760 {
761 if (c > 0x7F)
762 return false;
763 if (isASCIIDigit(c))
764 return c - '0' < base;
765 if (isASCIIAlpha(c)) {
766 if (base > 36)
767 base = 36;
768 return (c >= 'a' && c < 'a' + base - 10)
769 || (c >= 'A' && c < 'A' + base - 10);
770 }
771 return false;
772 }
773
774 template <typename IntegralType>
775 static inline IntegralType toIntegralType(const UChar* data, size_t length, bool* ok, int base)
776 {
777 static const IntegralType integralMax = numeric_limits<IntegralType>::max();
778 static const bool isSigned = numeric_limits<IntegralType>::is_signed;
779 const IntegralType maxMultiplier = integralMax / base;
780
781 IntegralType value = 0;
782 bool isOk = false;
783 bool isNegative = false;
784
785 if (!data)
786 goto bye;
787
788 // skip leading whitespace
789 while (length && isSpaceOrNewline(*data)) {
790 length--;
791 data++;
792 }
793
794 if (isSigned && length && *data == '-') {
795 length--;
796 data++;
797 isNegative = true;
798 } else if (length && *data == '+') {
799 length--;
800 data++;
801 }
802
803 if (!length || !isCharacterAllowedInBase(*data, base))
804 goto bye;
805
806 while (length && isCharacterAllowedInBase(*data, base)) {
807 length--;
808 IntegralType digitValue;
809 UChar c = *data;
810 if (isASCIIDigit(c))
811 digitValue = c - '0';
812 else if (c >= 'a')
813 digitValue = c - 'a' + 10;
814 else
815 digitValue = c - 'A' + 10;
816
817 if (value > maxMultiplier || (value == maxMultiplier && digitValue > (integralMax % base) + isNegative))
818 goto bye;
819
820 value = base * value + digitValue;
821 data++;
822 }
823
824 #if COMPILER(MSVC)
825 #pragma warning(push, 0)
826 #pragma warning(disable:4146)
827 #endif
828
829 if (isNegative)
830 value = -value;
831
832 #if COMPILER(MSVC)
833 #pragma warning(pop)
834 #endif
835
836 // skip trailing space
837 while (length && isSpaceOrNewline(*data)) {
838 length--;
839 data++;
840 }
841
842 if (!length)
843 isOk = true;
844 bye:
845 if (ok)
846 *ok = isOk;
847 return isOk ? value : 0;
848 }
849
850 static unsigned lengthOfCharactersAsInteger(const UChar* data, size_t length)
851 {
852 size_t i = 0;
853
854 // Allow leading spaces.
855 for (; i != length; ++i) {
856 if (!isSpaceOrNewline(data[i]))
857 break;
858 }
859
860 // Allow sign.
861 if (i != length && (data[i] == '+' || data[i] == '-'))
862 ++i;
863
864 // Allow digits.
865 for (; i != length; ++i) {
866 if (!isASCIIDigit(data[i]))
867 break;
868 }
869
870 return i;
871 }
872
873 int charactersToIntStrict(const UChar* data, size_t length, bool* ok, int base)
874 {
875 return toIntegralType<int>(data, length, ok, base);
876 }
877
878 unsigned charactersToUIntStrict(const UChar* data, size_t length, bool* ok, int base)
879 {
880 return toIntegralType<unsigned>(data, length, ok, base);
881 }
882
883 int64_t charactersToInt64Strict(const UChar* data, size_t length, bool* ok, int base)
884 {
885 return toIntegralType<int64_t>(data, length, ok, base);
886 }
887
888 uint64_t charactersToUInt64Strict(const UChar* data, size_t length, bool* ok, int base)
889 {
890 return toIntegralType<uint64_t>(data, length, ok, base);
891 }
892
893 intptr_t charactersToIntPtrStrict(const UChar* data, size_t length, bool* ok, int base)
894 {
895 return toIntegralType<intptr_t>(data, length, ok, base);
896 }
897
898 int charactersToInt(const UChar* data, size_t length, bool* ok)
899 {
900 return toIntegralType<int>(data, lengthOfCharactersAsInteger(data, length), ok, 10);
901 }
902
903 unsigned charactersToUInt(const UChar* data, size_t length, bool* ok)
904 {
905 return toIntegralType<unsigned>(data, lengthOfCharactersAsInteger(data, length), ok, 10);
906 }
907
908 int64_t charactersToInt64(const UChar* data, size_t length, bool* ok)
909 {
910 return toIntegralType<int64_t>(data, lengthOfCharactersAsInteger(data, length), ok, 10);
911 }
912
913 uint64_t charactersToUInt64(const UChar* data, size_t length, bool* ok)
914 {
915 return toIntegralType<uint64_t>(data, lengthOfCharactersAsInteger(data, length), ok, 10);
916 }
917
918 intptr_t charactersToIntPtr(const UChar* data, size_t length, bool* ok)
919 {
920 return toIntegralType<intptr_t>(data, lengthOfCharactersAsInteger(data, length), ok, 10);
921 }
922
923 double charactersToDouble(const UChar* data, size_t length, bool* ok, bool* didReadNumber)
924 {
925 if (!length) {
926 if (ok)
927 *ok = false;
928 if (didReadNumber)
929 *didReadNumber = false;
930 return 0.0;
931 }
932
933 Vector<char, 256> bytes(length + 1);
934 for (unsigned i = 0; i < length; ++i)
935 bytes[i] = data[i] < 0x7F ? data[i] : '?';
936 bytes[length] = '\0';
937 char* start = bytes.data();
938 char* end;
939 double val = WTF::strtod(start, &end);
940 if (ok)
941 *ok = (end == 0 || *end == '\0');
942 if (didReadNumber)
943 *didReadNumber = end - start;
944 return val;
945 }
946
947 float charactersToFloat(const UChar* data, size_t length, bool* ok, bool* didReadNumber)
948 {
949 // FIXME: This will return ok even when the string fits into a double but not a float.
950 return static_cast<float>(charactersToDouble(data, length, ok, didReadNumber));
951 }
952
953 const String& emptyString()
954 {
955 DEFINE_STATIC_LOCAL(String, emptyString, (StringImpl::empty()));
956 return emptyString;
957 }
958
959 } // namespace WTF
960
961 #ifndef NDEBUG
962 // For use in the debugger
963 String* string(const char*);
964 Vector<char> asciiDebug(StringImpl* impl);
965 Vector<char> asciiDebug(String& string);
966
967 String* string(const char* s)
968 {
969 // leaks memory!
970 return new String(s);
971 }
972
973 Vector<char> asciiDebug(StringImpl* impl)
974 {
975 if (!impl)
976 return asciiDebug(String("[null]").impl());
977
978 Vector<char> buffer;
979 unsigned length = impl->length();
980 const UChar* characters = impl->characters();
981
982 buffer.resize(length + 1);
983 for (unsigned i = 0; i < length; ++i) {
984 UChar ch = characters[i];
985 buffer[i] = ch && (ch < 0x20 || ch > 0x7f) ? '?' : ch;
986 }
987 buffer[length] = '\0';
988
989 return buffer;
990 }
991
992 Vector<char> asciiDebug(String& string)
993 {
994 return asciiDebug(string.impl());
995 }
996
997 #endif