1 | /*
|
---|
2 | * Copyright (C) 1999-2000 Harri Porten ([email protected])
|
---|
3 | * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
|
---|
4 | * Copyright (C) 2007 Cameron Zwarich ([email protected])
|
---|
5 | * Copyright (C) 2009 Google Inc. All rights reserved.
|
---|
6 | *
|
---|
7 | * This library is free software; you can redistribute it and/or
|
---|
8 | * modify it under the terms of the GNU Library General Public
|
---|
9 | * License as published by the Free Software Foundation; either
|
---|
10 | * version 2 of the License, or (at your option) any later version.
|
---|
11 | *
|
---|
12 | * This library is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
15 | * Library General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU Library General Public License
|
---|
18 | * along with this library; see the file COPYING.LIB. If not, write to
|
---|
19 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
---|
20 | * Boston, MA 02110-1301, USA.
|
---|
21 | *
|
---|
22 | */
|
---|
23 |
|
---|
24 | #include "config.h"
|
---|
25 | #include "UString.h"
|
---|
26 |
|
---|
27 | #include "JSGlobalObjectFunctions.h"
|
---|
28 | #include "Collector.h"
|
---|
29 | #include "dtoa.h"
|
---|
30 | #include "Identifier.h"
|
---|
31 | #include "Operations.h"
|
---|
32 | #include <ctype.h>
|
---|
33 | #include <float.h>
|
---|
34 | #include <limits.h>
|
---|
35 | #include <limits>
|
---|
36 | #include <math.h>
|
---|
37 | #include <stdio.h>
|
---|
38 | #include <stdlib.h>
|
---|
39 | #include <wtf/ASCIICType.h>
|
---|
40 | #include <wtf/Assertions.h>
|
---|
41 | #include <wtf/MathExtras.h>
|
---|
42 | #include <wtf/StringExtras.h>
|
---|
43 | #include <wtf/Vector.h>
|
---|
44 | #include <wtf/unicode/UTF8.h>
|
---|
45 | #include <wtf/StringExtras.h>
|
---|
46 |
|
---|
47 | #if HAVE(STRING_H)
|
---|
48 | #include <string.h>
|
---|
49 | #endif
|
---|
50 | #if HAVE(STRINGS_H)
|
---|
51 | #include <strings.h>
|
---|
52 | #endif
|
---|
53 |
|
---|
54 | using namespace WTF;
|
---|
55 | using namespace WTF::Unicode;
|
---|
56 | using namespace std;
|
---|
57 |
|
---|
58 | // This can be tuned differently per platform by putting platform #ifs right here.
|
---|
59 | // If you don't define this macro at all, then copyChars will just call directly
|
---|
60 | // to memcpy.
|
---|
61 | #define USTRING_COPY_CHARS_INLINE_CUTOFF 20
|
---|
62 |
|
---|
63 | namespace JSC {
|
---|
64 |
|
---|
65 | extern const double NaN;
|
---|
66 | extern const double Inf;
|
---|
67 |
|
---|
68 | // This number must be at least 2 to avoid sharing empty, null as well as 1 character strings from SmallStrings.
|
---|
69 | static const int minLengthToShare = 10;
|
---|
70 |
|
---|
71 | static inline size_t overflowIndicator() { return std::numeric_limits<size_t>::max(); }
|
---|
72 | static inline size_t maxUChars() { return std::numeric_limits<size_t>::max() / sizeof(UChar); }
|
---|
73 |
|
---|
74 | static inline PossiblyNull<UChar*> allocChars(size_t length)
|
---|
75 | {
|
---|
76 | ASSERT(length);
|
---|
77 | if (length > maxUChars())
|
---|
78 | return 0;
|
---|
79 | return tryFastMalloc(sizeof(UChar) * length);
|
---|
80 | }
|
---|
81 |
|
---|
82 | static inline PossiblyNull<UChar*> reallocChars(UChar* buffer, size_t length)
|
---|
83 | {
|
---|
84 | ASSERT(length);
|
---|
85 | if (length > maxUChars())
|
---|
86 | return 0;
|
---|
87 | return tryFastRealloc(buffer, sizeof(UChar) * length);
|
---|
88 | }
|
---|
89 |
|
---|
90 | static inline void copyChars(UChar* destination, const UChar* source, unsigned numCharacters)
|
---|
91 | {
|
---|
92 | #ifdef USTRING_COPY_CHARS_INLINE_CUTOFF
|
---|
93 | if (numCharacters <= USTRING_COPY_CHARS_INLINE_CUTOFF) {
|
---|
94 | for (unsigned i = 0; i < numCharacters; ++i)
|
---|
95 | destination[i] = source[i];
|
---|
96 | return;
|
---|
97 | }
|
---|
98 | #endif
|
---|
99 | memcpy(destination, source, numCharacters * sizeof(UChar));
|
---|
100 | }
|
---|
101 |
|
---|
102 | COMPILE_ASSERT(sizeof(UChar) == 2, uchar_is_2_bytes);
|
---|
103 |
|
---|
104 | CString::CString(const char* c)
|
---|
105 | : m_length(strlen(c))
|
---|
106 | , m_data(new char[m_length + 1])
|
---|
107 | {
|
---|
108 | memcpy(m_data, c, m_length + 1);
|
---|
109 | }
|
---|
110 |
|
---|
111 | CString::CString(const char* c, size_t length)
|
---|
112 | : m_length(length)
|
---|
113 | , m_data(new char[length + 1])
|
---|
114 | {
|
---|
115 | memcpy(m_data, c, m_length);
|
---|
116 | m_data[m_length] = 0;
|
---|
117 | }
|
---|
118 |
|
---|
119 | CString::CString(const CString& b)
|
---|
120 | {
|
---|
121 | m_length = b.m_length;
|
---|
122 | if (b.m_data) {
|
---|
123 | m_data = new char[m_length + 1];
|
---|
124 | memcpy(m_data, b.m_data, m_length + 1);
|
---|
125 | } else
|
---|
126 | m_data = 0;
|
---|
127 | }
|
---|
128 |
|
---|
129 | CString::~CString()
|
---|
130 | {
|
---|
131 | delete [] m_data;
|
---|
132 | }
|
---|
133 |
|
---|
134 | CString CString::adopt(char* c, size_t length)
|
---|
135 | {
|
---|
136 | CString s;
|
---|
137 | s.m_data = c;
|
---|
138 | s.m_length = length;
|
---|
139 | return s;
|
---|
140 | }
|
---|
141 |
|
---|
142 | CString& CString::append(const CString& t)
|
---|
143 | {
|
---|
144 | char* n;
|
---|
145 | n = new char[m_length + t.m_length + 1];
|
---|
146 | if (m_length)
|
---|
147 | memcpy(n, m_data, m_length);
|
---|
148 | if (t.m_length)
|
---|
149 | memcpy(n + m_length, t.m_data, t.m_length);
|
---|
150 | m_length += t.m_length;
|
---|
151 | n[m_length] = 0;
|
---|
152 |
|
---|
153 | delete [] m_data;
|
---|
154 | m_data = n;
|
---|
155 |
|
---|
156 | return *this;
|
---|
157 | }
|
---|
158 |
|
---|
159 | CString& CString::operator=(const char* c)
|
---|
160 | {
|
---|
161 | if (m_data)
|
---|
162 | delete [] m_data;
|
---|
163 | m_length = strlen(c);
|
---|
164 | m_data = new char[m_length + 1];
|
---|
165 | memcpy(m_data, c, m_length + 1);
|
---|
166 |
|
---|
167 | return *this;
|
---|
168 | }
|
---|
169 |
|
---|
170 | CString& CString::operator=(const CString& str)
|
---|
171 | {
|
---|
172 | if (this == &str)
|
---|
173 | return *this;
|
---|
174 |
|
---|
175 | if (m_data)
|
---|
176 | delete [] m_data;
|
---|
177 | m_length = str.m_length;
|
---|
178 | if (str.m_data) {
|
---|
179 | m_data = new char[m_length + 1];
|
---|
180 | memcpy(m_data, str.m_data, m_length + 1);
|
---|
181 | } else
|
---|
182 | m_data = 0;
|
---|
183 |
|
---|
184 | return *this;
|
---|
185 | }
|
---|
186 |
|
---|
187 | bool operator==(const CString& c1, const CString& c2)
|
---|
188 | {
|
---|
189 | size_t len = c1.size();
|
---|
190 | return len == c2.size() && (len == 0 || memcmp(c1.c_str(), c2.c_str(), len) == 0);
|
---|
191 | }
|
---|
192 |
|
---|
193 | // These static strings are immutable, except for rc, whose initial value is chosen to
|
---|
194 | // reduce the possibility of it becoming zero due to ref/deref not being thread-safe.
|
---|
195 | static UChar sharedEmptyChar;
|
---|
196 | UString::BaseString* UString::Rep::nullBaseString;
|
---|
197 | UString::BaseString* UString::Rep::emptyBaseString;
|
---|
198 | UString* UString::nullUString;
|
---|
199 |
|
---|
200 | static void initializeStaticBaseString(UString::BaseString& base)
|
---|
201 | {
|
---|
202 | base.rc = INT_MAX / 2;
|
---|
203 | base.m_identifierTableAndFlags.setFlag(UString::Rep::StaticFlag);
|
---|
204 | base.checkConsistency();
|
---|
205 | }
|
---|
206 |
|
---|
207 | void initializeUString()
|
---|
208 | {
|
---|
209 | UString::Rep::nullBaseString = new UString::BaseString(0, 0);
|
---|
210 | initializeStaticBaseString(*UString::Rep::nullBaseString);
|
---|
211 |
|
---|
212 | UString::Rep::emptyBaseString = new UString::BaseString(&sharedEmptyChar, 0);
|
---|
213 | initializeStaticBaseString(*UString::Rep::emptyBaseString);
|
---|
214 |
|
---|
215 | UString::nullUString = new UString;
|
---|
216 | }
|
---|
217 |
|
---|
218 | static char* statBuffer = 0; // Only used for debugging via UString::ascii().
|
---|
219 |
|
---|
220 | PassRefPtr<UString::Rep> UString::Rep::createCopying(const UChar* d, int l)
|
---|
221 | {
|
---|
222 | UChar* copyD = static_cast<UChar*>(fastMalloc(l * sizeof(UChar)));
|
---|
223 | copyChars(copyD, d, l);
|
---|
224 | return create(copyD, l);
|
---|
225 | }
|
---|
226 |
|
---|
227 | PassRefPtr<UString::Rep> UString::Rep::createFromUTF8(const char* string)
|
---|
228 | {
|
---|
229 | if (!string)
|
---|
230 | return &UString::Rep::null();
|
---|
231 |
|
---|
232 | size_t length = strlen(string);
|
---|
233 | Vector<UChar, 1024> buffer(length);
|
---|
234 | UChar* p = buffer.data();
|
---|
235 | if (conversionOK != convertUTF8ToUTF16(&string, string + length, &p, p + length))
|
---|
236 | return &UString::Rep::null();
|
---|
237 |
|
---|
238 | return UString::Rep::createCopying(buffer.data(), p - buffer.data());
|
---|
239 | }
|
---|
240 |
|
---|
241 | PassRefPtr<UString::Rep> UString::Rep::create(UChar* string, int length, PassRefPtr<UString::SharedUChar> sharedBuffer)
|
---|
242 | {
|
---|
243 | PassRefPtr<UString::Rep> rep = create(string, length);
|
---|
244 | rep->baseString()->setSharedBuffer(sharedBuffer);
|
---|
245 | rep->checkConsistency();
|
---|
246 | return rep;
|
---|
247 | }
|
---|
248 |
|
---|
249 | UString::SharedUChar* UString::Rep::sharedBuffer()
|
---|
250 | {
|
---|
251 | UString::BaseString* base = baseString();
|
---|
252 | if (len < minLengthToShare)
|
---|
253 | return 0;
|
---|
254 |
|
---|
255 | return base->sharedBuffer();
|
---|
256 | }
|
---|
257 |
|
---|
258 | void UString::Rep::destroy()
|
---|
259 | {
|
---|
260 | checkConsistency();
|
---|
261 |
|
---|
262 | // Static null and empty strings can never be destroyed, but we cannot rely on
|
---|
263 | // reference counting, because ref/deref are not thread-safe.
|
---|
264 | if (!isStatic()) {
|
---|
265 | if (identifierTable())
|
---|
266 | Identifier::remove(this);
|
---|
267 |
|
---|
268 | UString::BaseString* base = baseString();
|
---|
269 | if (base == this) {
|
---|
270 | if (m_sharedBuffer)
|
---|
271 | m_sharedBuffer->deref();
|
---|
272 | else
|
---|
273 | fastFree(base->buf);
|
---|
274 | } else
|
---|
275 | base->deref();
|
---|
276 |
|
---|
277 | delete this;
|
---|
278 | }
|
---|
279 | }
|
---|
280 |
|
---|
281 | // Golden ratio - arbitrary start value to avoid mapping all 0's to all 0's
|
---|
282 | // or anything like that.
|
---|
283 | const unsigned PHI = 0x9e3779b9U;
|
---|
284 |
|
---|
285 | // Paul Hsieh's SuperFastHash
|
---|
286 | // http://www.azillionmonkeys.com/qed/hash.html
|
---|
287 | unsigned UString::Rep::computeHash(const UChar* s, int len)
|
---|
288 | {
|
---|
289 | unsigned l = len;
|
---|
290 | uint32_t hash = PHI;
|
---|
291 | uint32_t tmp;
|
---|
292 |
|
---|
293 | int rem = l & 1;
|
---|
294 | l >>= 1;
|
---|
295 |
|
---|
296 | // Main loop
|
---|
297 | for (; l > 0; l--) {
|
---|
298 | hash += s[0];
|
---|
299 | tmp = (s[1] << 11) ^ hash;
|
---|
300 | hash = (hash << 16) ^ tmp;
|
---|
301 | s += 2;
|
---|
302 | hash += hash >> 11;
|
---|
303 | }
|
---|
304 |
|
---|
305 | // Handle end case
|
---|
306 | if (rem) {
|
---|
307 | hash += s[0];
|
---|
308 | hash ^= hash << 11;
|
---|
309 | hash += hash >> 17;
|
---|
310 | }
|
---|
311 |
|
---|
312 | // Force "avalanching" of final 127 bits
|
---|
313 | hash ^= hash << 3;
|
---|
314 | hash += hash >> 5;
|
---|
315 | hash ^= hash << 2;
|
---|
316 | hash += hash >> 15;
|
---|
317 | hash ^= hash << 10;
|
---|
318 |
|
---|
319 | // this avoids ever returning a hash code of 0, since that is used to
|
---|
320 | // signal "hash not computed yet", using a value that is likely to be
|
---|
321 | // effectively the same as 0 when the low bits are masked
|
---|
322 | if (hash == 0)
|
---|
323 | hash = 0x80000000;
|
---|
324 |
|
---|
325 | return hash;
|
---|
326 | }
|
---|
327 |
|
---|
328 | // Paul Hsieh's SuperFastHash
|
---|
329 | // http://www.azillionmonkeys.com/qed/hash.html
|
---|
330 | unsigned UString::Rep::computeHash(const char* s, int l)
|
---|
331 | {
|
---|
332 | // This hash is designed to work on 16-bit chunks at a time. But since the normal case
|
---|
333 | // (above) is to hash UTF-16 characters, we just treat the 8-bit chars as if they
|
---|
334 | // were 16-bit chunks, which should give matching results
|
---|
335 |
|
---|
336 | uint32_t hash = PHI;
|
---|
337 | uint32_t tmp;
|
---|
338 |
|
---|
339 | size_t rem = l & 1;
|
---|
340 | l >>= 1;
|
---|
341 |
|
---|
342 | // Main loop
|
---|
343 | for (; l > 0; l--) {
|
---|
344 | hash += static_cast<unsigned char>(s[0]);
|
---|
345 | tmp = (static_cast<unsigned char>(s[1]) << 11) ^ hash;
|
---|
346 | hash = (hash << 16) ^ tmp;
|
---|
347 | s += 2;
|
---|
348 | hash += hash >> 11;
|
---|
349 | }
|
---|
350 |
|
---|
351 | // Handle end case
|
---|
352 | if (rem) {
|
---|
353 | hash += static_cast<unsigned char>(s[0]);
|
---|
354 | hash ^= hash << 11;
|
---|
355 | hash += hash >> 17;
|
---|
356 | }
|
---|
357 |
|
---|
358 | // Force "avalanching" of final 127 bits
|
---|
359 | hash ^= hash << 3;
|
---|
360 | hash += hash >> 5;
|
---|
361 | hash ^= hash << 2;
|
---|
362 | hash += hash >> 15;
|
---|
363 | hash ^= hash << 10;
|
---|
364 |
|
---|
365 | // this avoids ever returning a hash code of 0, since that is used to
|
---|
366 | // signal "hash not computed yet", using a value that is likely to be
|
---|
367 | // effectively the same as 0 when the low bits are masked
|
---|
368 | if (hash == 0)
|
---|
369 | hash = 0x80000000;
|
---|
370 |
|
---|
371 | return hash;
|
---|
372 | }
|
---|
373 |
|
---|
374 | #ifndef NDEBUG
|
---|
375 | void UString::Rep::checkConsistency() const
|
---|
376 | {
|
---|
377 | const UString::BaseString* base = baseString();
|
---|
378 |
|
---|
379 | // There is no recursion for base strings.
|
---|
380 | ASSERT(base == base->baseString());
|
---|
381 |
|
---|
382 | if (isStatic()) {
|
---|
383 | // There are only two static strings: null and empty.
|
---|
384 | ASSERT(!len);
|
---|
385 |
|
---|
386 | // Static strings cannot get in identifier tables, because they are globally shared.
|
---|
387 | ASSERT(!identifierTable());
|
---|
388 | }
|
---|
389 |
|
---|
390 | // The string fits in buffer.
|
---|
391 | ASSERT(base->usedPreCapacity <= base->preCapacity);
|
---|
392 | ASSERT(base->usedCapacity <= base->capacity);
|
---|
393 | ASSERT(-offset <= base->usedPreCapacity);
|
---|
394 | ASSERT(offset + len <= base->usedCapacity);
|
---|
395 | }
|
---|
396 | #endif
|
---|
397 |
|
---|
398 | UString::SharedUChar* UString::BaseString::sharedBuffer()
|
---|
399 | {
|
---|
400 | if (!m_sharedBuffer)
|
---|
401 | setSharedBuffer(SharedUChar::create(new OwnFastMallocPtr<UChar>(buf)));
|
---|
402 | return m_sharedBuffer;
|
---|
403 | }
|
---|
404 |
|
---|
405 | void UString::BaseString::setSharedBuffer(PassRefPtr<UString::SharedUChar> sharedBuffer)
|
---|
406 | {
|
---|
407 | // The manual steps below are because m_sharedBuffer can't be a RefPtr. m_sharedBuffer
|
---|
408 | // is in a union with another variable to avoid making BaseString any larger.
|
---|
409 | if (m_sharedBuffer)
|
---|
410 | m_sharedBuffer->deref();
|
---|
411 | m_sharedBuffer = sharedBuffer.releaseRef();
|
---|
412 | }
|
---|
413 |
|
---|
414 | bool UString::BaseString::slowIsBufferReadOnly()
|
---|
415 | {
|
---|
416 | // The buffer may not be modified as soon as the underlying data has been shared with another class.
|
---|
417 | if (m_sharedBuffer->isShared())
|
---|
418 | return true;
|
---|
419 |
|
---|
420 | // At this point, we know it that the underlying buffer isn't shared outside of this base class,
|
---|
421 | // so get rid of m_sharedBuffer.
|
---|
422 | OwnPtr<OwnFastMallocPtr<UChar> > mallocPtr(m_sharedBuffer->release());
|
---|
423 | UChar* unsharedBuf = const_cast<UChar*>(mallocPtr->release());
|
---|
424 | setSharedBuffer(0);
|
---|
425 | preCapacity += (buf - unsharedBuf);
|
---|
426 | buf = unsharedBuf;
|
---|
427 | return false;
|
---|
428 | }
|
---|
429 |
|
---|
430 | // Put these early so they can be inlined.
|
---|
431 | static inline size_t expandedSize(size_t capacitySize, size_t precapacitySize)
|
---|
432 | {
|
---|
433 | // Combine capacitySize & precapacitySize to produce a single size to allocate,
|
---|
434 | // check that doing so does not result in overflow.
|
---|
435 | size_t size = capacitySize + precapacitySize;
|
---|
436 | if (size < capacitySize)
|
---|
437 | return overflowIndicator();
|
---|
438 |
|
---|
439 | // Small Strings (up to 4 pages):
|
---|
440 | // Expand the allocation size to 112.5% of the amount requested. This is largely sicking
|
---|
441 | // to our previous policy, however 112.5% is cheaper to calculate.
|
---|
442 | if (size < 0x4000) {
|
---|
443 | size_t expandedSize = ((size + (size >> 3)) | 15) + 1;
|
---|
444 | // Given the limited range within which we calculate the expansion in this
|
---|
445 | // fashion the above calculation should never overflow.
|
---|
446 | ASSERT(expandedSize >= size);
|
---|
447 | ASSERT(expandedSize < maxUChars());
|
---|
448 | return expandedSize;
|
---|
449 | }
|
---|
450 |
|
---|
451 | // Medium Strings (up to 128 pages):
|
---|
452 | // For pages covering multiple pages over-allocation is less of a concern - any unused
|
---|
453 | // space will not be paged in if it is not used, so this is purely a VM overhead. For
|
---|
454 | // these strings allocate 2x the requested size.
|
---|
455 | if (size < 0x80000) {
|
---|
456 | size_t expandedSize = ((size + size) | 0xfff) + 1;
|
---|
457 | // Given the limited range within which we calculate the expansion in this
|
---|
458 | // fashion the above calculation should never overflow.
|
---|
459 | ASSERT(expandedSize >= size);
|
---|
460 | ASSERT(expandedSize < maxUChars());
|
---|
461 | return expandedSize;
|
---|
462 | }
|
---|
463 |
|
---|
464 | // Large Strings (to infinity and beyond!):
|
---|
465 | // Revert to our 112.5% policy - probably best to limit the amount of unused VM we allow
|
---|
466 | // any individual string be responsible for.
|
---|
467 | size_t expandedSize = ((size + (size >> 3)) | 0xfff) + 1;
|
---|
468 |
|
---|
469 | // Check for overflow - any result that is at least as large as requested (but
|
---|
470 | // still below the limit) is okay.
|
---|
471 | if ((expandedSize >= size) && (expandedSize < maxUChars()))
|
---|
472 | return expandedSize;
|
---|
473 | return overflowIndicator();
|
---|
474 | }
|
---|
475 |
|
---|
476 | static inline bool expandCapacity(UString::Rep* rep, int requiredLength)
|
---|
477 | {
|
---|
478 | rep->checkConsistency();
|
---|
479 | ASSERT(!rep->baseString()->isBufferReadOnly());
|
---|
480 |
|
---|
481 | UString::BaseString* base = rep->baseString();
|
---|
482 |
|
---|
483 | if (requiredLength > base->capacity) {
|
---|
484 | size_t newCapacity = expandedSize(requiredLength, base->preCapacity);
|
---|
485 | UChar* oldBuf = base->buf;
|
---|
486 | if (!reallocChars(base->buf, newCapacity).getValue(base->buf)) {
|
---|
487 | base->buf = oldBuf;
|
---|
488 | return false;
|
---|
489 | }
|
---|
490 | base->capacity = newCapacity - base->preCapacity;
|
---|
491 | }
|
---|
492 | if (requiredLength > base->usedCapacity)
|
---|
493 | base->usedCapacity = requiredLength;
|
---|
494 |
|
---|
495 | rep->checkConsistency();
|
---|
496 | return true;
|
---|
497 | }
|
---|
498 |
|
---|
499 | bool UString::Rep::reserveCapacity(int capacity)
|
---|
500 | {
|
---|
501 | // If this is an empty string there is no point 'growing' it - just allocate a new one.
|
---|
502 | // If the BaseString is shared with another string that is using more capacity than this
|
---|
503 | // string is, then growing the buffer won't help.
|
---|
504 | // If the BaseString's buffer is readonly, then it isn't allowed to grow.
|
---|
505 | UString::BaseString* base = baseString();
|
---|
506 | if (!base->buf || !base->capacity || (offset + len) != base->usedCapacity || base->isBufferReadOnly())
|
---|
507 | return false;
|
---|
508 |
|
---|
509 | // If there is already sufficient capacity, no need to grow!
|
---|
510 | if (capacity <= base->capacity)
|
---|
511 | return true;
|
---|
512 |
|
---|
513 | checkConsistency();
|
---|
514 |
|
---|
515 | size_t newCapacity = expandedSize(capacity, base->preCapacity);
|
---|
516 | UChar* oldBuf = base->buf;
|
---|
517 | if (!reallocChars(base->buf, newCapacity).getValue(base->buf)) {
|
---|
518 | base->buf = oldBuf;
|
---|
519 | return false;
|
---|
520 | }
|
---|
521 | base->capacity = newCapacity - base->preCapacity;
|
---|
522 |
|
---|
523 | checkConsistency();
|
---|
524 | return true;
|
---|
525 | }
|
---|
526 |
|
---|
527 | void UString::expandCapacity(int requiredLength)
|
---|
528 | {
|
---|
529 | if (!JSC::expandCapacity(m_rep.get(), requiredLength))
|
---|
530 | makeNull();
|
---|
531 | }
|
---|
532 |
|
---|
533 | void UString::expandPreCapacity(int requiredPreCap)
|
---|
534 | {
|
---|
535 | m_rep->checkConsistency();
|
---|
536 | ASSERT(!m_rep->baseString()->isBufferReadOnly());
|
---|
537 |
|
---|
538 | BaseString* base = m_rep->baseString();
|
---|
539 |
|
---|
540 | if (requiredPreCap > base->preCapacity) {
|
---|
541 | size_t newCapacity = expandedSize(requiredPreCap, base->capacity);
|
---|
542 | int delta = newCapacity - base->capacity - base->preCapacity;
|
---|
543 |
|
---|
544 | UChar* newBuf;
|
---|
545 | if (!allocChars(newCapacity).getValue(newBuf)) {
|
---|
546 | makeNull();
|
---|
547 | return;
|
---|
548 | }
|
---|
549 | copyChars(newBuf + delta, base->buf, base->capacity + base->preCapacity);
|
---|
550 | fastFree(base->buf);
|
---|
551 | base->buf = newBuf;
|
---|
552 |
|
---|
553 | base->preCapacity = newCapacity - base->capacity;
|
---|
554 | }
|
---|
555 | if (requiredPreCap > base->usedPreCapacity)
|
---|
556 | base->usedPreCapacity = requiredPreCap;
|
---|
557 |
|
---|
558 | m_rep->checkConsistency();
|
---|
559 | }
|
---|
560 |
|
---|
561 | static PassRefPtr<UString::Rep> createRep(const char* c)
|
---|
562 | {
|
---|
563 | if (!c)
|
---|
564 | return &UString::Rep::null();
|
---|
565 |
|
---|
566 | if (!c[0])
|
---|
567 | return &UString::Rep::empty();
|
---|
568 |
|
---|
569 | size_t length = strlen(c);
|
---|
570 | UChar* d;
|
---|
571 | if (!allocChars(length).getValue(d))
|
---|
572 | return &UString::Rep::null();
|
---|
573 | else {
|
---|
574 | for (size_t i = 0; i < length; i++)
|
---|
575 | d[i] = static_cast<unsigned char>(c[i]); // use unsigned char to zero-extend instead of sign-extend
|
---|
576 | return UString::Rep::create(d, static_cast<int>(length));
|
---|
577 | }
|
---|
578 |
|
---|
579 | }
|
---|
580 |
|
---|
581 | UString::UString(const char* c)
|
---|
582 | : m_rep(createRep(c))
|
---|
583 | {
|
---|
584 | }
|
---|
585 |
|
---|
586 | UString::UString(const UChar* c, int length)
|
---|
587 | {
|
---|
588 | if (length == 0)
|
---|
589 | m_rep = &Rep::empty();
|
---|
590 | else
|
---|
591 | m_rep = Rep::createCopying(c, length);
|
---|
592 | }
|
---|
593 |
|
---|
594 | UString::UString(UChar* c, int length, bool copy)
|
---|
595 | {
|
---|
596 | if (length == 0)
|
---|
597 | m_rep = &Rep::empty();
|
---|
598 | else if (copy)
|
---|
599 | m_rep = Rep::createCopying(c, length);
|
---|
600 | else
|
---|
601 | m_rep = Rep::create(c, length);
|
---|
602 | }
|
---|
603 |
|
---|
604 | UString::UString(const Vector<UChar>& buffer)
|
---|
605 | {
|
---|
606 | if (!buffer.size())
|
---|
607 | m_rep = &Rep::empty();
|
---|
608 | else
|
---|
609 | m_rep = Rep::createCopying(buffer.data(), buffer.size());
|
---|
610 | }
|
---|
611 |
|
---|
612 | static ALWAYS_INLINE int newCapacityWithOverflowCheck(const int currentCapacity, const int extendLength, const bool plusOne = false)
|
---|
613 | {
|
---|
614 | ASSERT_WITH_MESSAGE(extendLength >= 0, "extendedLength = %d", extendLength);
|
---|
615 |
|
---|
616 | const int plusLength = plusOne ? 1 : 0;
|
---|
617 | if (currentCapacity > std::numeric_limits<int>::max() - extendLength - plusLength)
|
---|
618 | CRASH();
|
---|
619 |
|
---|
620 | return currentCapacity + extendLength + plusLength;
|
---|
621 | }
|
---|
622 |
|
---|
623 | static ALWAYS_INLINE PassRefPtr<UString::Rep> concatenate(PassRefPtr<UString::Rep> r, const UChar* tData, int tSize)
|
---|
624 | {
|
---|
625 | RefPtr<UString::Rep> rep = r;
|
---|
626 |
|
---|
627 | rep->checkConsistency();
|
---|
628 |
|
---|
629 | int thisSize = rep->size();
|
---|
630 | int thisOffset = rep->offset;
|
---|
631 | int length = thisSize + tSize;
|
---|
632 | UString::BaseString* base = rep->baseString();
|
---|
633 |
|
---|
634 | // possible cases:
|
---|
635 | if (tSize == 0) {
|
---|
636 | // t is empty
|
---|
637 | } else if (thisSize == 0) {
|
---|
638 | // this is empty
|
---|
639 | rep = UString::Rep::createCopying(tData, tSize);
|
---|
640 | } else if (rep == base && !base->isShared()) {
|
---|
641 | // this is direct and has refcount of 1 (so we can just alter it directly)
|
---|
642 | if (!expandCapacity(rep.get(), newCapacityWithOverflowCheck(thisOffset, length)))
|
---|
643 | rep = &UString::Rep::null();
|
---|
644 | if (rep->data()) {
|
---|
645 | copyChars(rep->data() + thisSize, tData, tSize);
|
---|
646 | rep->len = length;
|
---|
647 | rep->_hash = 0;
|
---|
648 | }
|
---|
649 | } else if (thisOffset + thisSize == base->usedCapacity && thisSize >= minShareSize && !base->isBufferReadOnly()) {
|
---|
650 | // this reaches the end of the buffer - extend it if it's long enough to append to
|
---|
651 | if (!expandCapacity(rep.get(), newCapacityWithOverflowCheck(thisOffset, length)))
|
---|
652 | rep = &UString::Rep::null();
|
---|
653 | if (rep->data()) {
|
---|
654 | copyChars(rep->data() + thisSize, tData, tSize);
|
---|
655 | rep = UString::Rep::create(rep, 0, length);
|
---|
656 | }
|
---|
657 | } else {
|
---|
658 | // This is shared in some way that prevents us from modifying base, so we must make a whole new string.
|
---|
659 | size_t newCapacity = expandedSize(length, 0);
|
---|
660 | UChar* d;
|
---|
661 | if (!allocChars(newCapacity).getValue(d))
|
---|
662 | rep = &UString::Rep::null();
|
---|
663 | else {
|
---|
664 | copyChars(d, rep->data(), thisSize);
|
---|
665 | copyChars(d + thisSize, tData, tSize);
|
---|
666 | rep = UString::Rep::create(d, length);
|
---|
667 | rep->baseString()->capacity = newCapacity;
|
---|
668 | }
|
---|
669 | }
|
---|
670 |
|
---|
671 | rep->checkConsistency();
|
---|
672 |
|
---|
673 | return rep.release();
|
---|
674 | }
|
---|
675 |
|
---|
676 | static ALWAYS_INLINE PassRefPtr<UString::Rep> concatenate(PassRefPtr<UString::Rep> r, const char* t)
|
---|
677 | {
|
---|
678 | RefPtr<UString::Rep> rep = r;
|
---|
679 |
|
---|
680 | rep->checkConsistency();
|
---|
681 |
|
---|
682 | int thisSize = rep->size();
|
---|
683 | int thisOffset = rep->offset;
|
---|
684 | int tSize = static_cast<int>(strlen(t));
|
---|
685 | int length = thisSize + tSize;
|
---|
686 | UString::BaseString* base = rep->baseString();
|
---|
687 |
|
---|
688 | // possible cases:
|
---|
689 | if (thisSize == 0) {
|
---|
690 | // this is empty
|
---|
691 | rep = createRep(t);
|
---|
692 | } else if (tSize == 0) {
|
---|
693 | // t is empty, we'll just return *this below.
|
---|
694 | } else if (rep == base && !base->isShared()) {
|
---|
695 | // this is direct and has refcount of 1 (so we can just alter it directly)
|
---|
696 | expandCapacity(rep.get(), newCapacityWithOverflowCheck(thisOffset, length));
|
---|
697 | UChar* d = rep->data();
|
---|
698 | if (d) {
|
---|
699 | for (int i = 0; i < tSize; ++i)
|
---|
700 | d[thisSize + i] = static_cast<unsigned char>(t[i]); // use unsigned char to zero-extend instead of sign-extend
|
---|
701 | rep->len = length;
|
---|
702 | rep->_hash = 0;
|
---|
703 | }
|
---|
704 | } else if (thisOffset + thisSize == base->usedCapacity && thisSize >= minShareSize && !base->isBufferReadOnly()) {
|
---|
705 | // this string reaches the end of the buffer - extend it
|
---|
706 | expandCapacity(rep.get(), newCapacityWithOverflowCheck(thisOffset, length));
|
---|
707 | UChar* d = rep->data();
|
---|
708 | if (d) {
|
---|
709 | for (int i = 0; i < tSize; ++i)
|
---|
710 | d[thisSize + i] = static_cast<unsigned char>(t[i]); // use unsigned char to zero-extend instead of sign-extend
|
---|
711 | rep = UString::Rep::create(rep, 0, length);
|
---|
712 | }
|
---|
713 | } else {
|
---|
714 | // This is shared in some way that prevents us from modifying base, so we must make a whole new string.
|
---|
715 | size_t newCapacity = expandedSize(length, 0);
|
---|
716 | UChar* d;
|
---|
717 | if (!allocChars(newCapacity).getValue(d))
|
---|
718 | rep = &UString::Rep::null();
|
---|
719 | else {
|
---|
720 | copyChars(d, rep->data(), thisSize);
|
---|
721 | for (int i = 0; i < tSize; ++i)
|
---|
722 | d[thisSize + i] = static_cast<unsigned char>(t[i]); // use unsigned char to zero-extend instead of sign-extend
|
---|
723 | rep = UString::Rep::create(d, length);
|
---|
724 | rep->baseString()->capacity = newCapacity;
|
---|
725 | }
|
---|
726 | }
|
---|
727 |
|
---|
728 | rep->checkConsistency();
|
---|
729 |
|
---|
730 | return rep.release();
|
---|
731 | }
|
---|
732 |
|
---|
733 | PassRefPtr<UString::Rep> concatenate(UString::Rep* a, UString::Rep* b)
|
---|
734 | {
|
---|
735 | a->checkConsistency();
|
---|
736 | b->checkConsistency();
|
---|
737 |
|
---|
738 | int aSize = a->size();
|
---|
739 | int bSize = b->size();
|
---|
740 | int aOffset = a->offset;
|
---|
741 |
|
---|
742 | // possible cases:
|
---|
743 |
|
---|
744 | UString::BaseString* aBase = a->baseString();
|
---|
745 | if (bSize == 1 && aOffset + aSize == aBase->usedCapacity && aOffset + aSize < aBase->capacity && !aBase->isBufferReadOnly()) {
|
---|
746 | // b is a single character (common fast case)
|
---|
747 | ++aBase->usedCapacity;
|
---|
748 | a->data()[aSize] = b->data()[0];
|
---|
749 | return UString::Rep::create(a, 0, aSize + 1);
|
---|
750 | }
|
---|
751 |
|
---|
752 | // a is empty
|
---|
753 | if (aSize == 0)
|
---|
754 | return b;
|
---|
755 | // b is empty
|
---|
756 | if (bSize == 0)
|
---|
757 | return a;
|
---|
758 |
|
---|
759 | int bOffset = b->offset;
|
---|
760 | int length = aSize + bSize;
|
---|
761 |
|
---|
762 | UString::BaseString* bBase = b->baseString();
|
---|
763 | if (aOffset + aSize == aBase->usedCapacity && aSize >= minShareSize && 4 * aSize >= bSize
|
---|
764 | && (-bOffset != bBase->usedPreCapacity || aSize >= bSize) && !aBase->isBufferReadOnly()) {
|
---|
765 | // - a reaches the end of its buffer so it qualifies for shared append
|
---|
766 | // - also, it's at least a quarter the length of b - appending to a much shorter
|
---|
767 | // string does more harm than good
|
---|
768 | // - however, if b qualifies for prepend and is longer than a, we'd rather prepend
|
---|
769 |
|
---|
770 | UString x(a);
|
---|
771 | x.expandCapacity(newCapacityWithOverflowCheck(aOffset, length));
|
---|
772 | if (!a->data() || !x.data())
|
---|
773 | return 0;
|
---|
774 | copyChars(a->data() + aSize, b->data(), bSize);
|
---|
775 | PassRefPtr<UString::Rep> result = UString::Rep::create(a, 0, length);
|
---|
776 |
|
---|
777 | a->checkConsistency();
|
---|
778 | b->checkConsistency();
|
---|
779 | result->checkConsistency();
|
---|
780 |
|
---|
781 | return result;
|
---|
782 | }
|
---|
783 |
|
---|
784 | if (-bOffset == bBase->usedPreCapacity && bSize >= minShareSize && 4 * bSize >= aSize && !bBase->isBufferReadOnly()) {
|
---|
785 | // - b reaches the beginning of its buffer so it qualifies for shared prepend
|
---|
786 | // - also, it's at least a quarter the length of a - prepending to a much shorter
|
---|
787 | // string does more harm than good
|
---|
788 | UString y(b);
|
---|
789 | y.expandPreCapacity(-bOffset + aSize);
|
---|
790 | if (!b->data() || !y.data())
|
---|
791 | return 0;
|
---|
792 | copyChars(b->data() - aSize, a->data(), aSize);
|
---|
793 | PassRefPtr<UString::Rep> result = UString::Rep::create(b, -aSize, length);
|
---|
794 |
|
---|
795 | a->checkConsistency();
|
---|
796 | b->checkConsistency();
|
---|
797 | result->checkConsistency();
|
---|
798 |
|
---|
799 | return result;
|
---|
800 | }
|
---|
801 |
|
---|
802 | // a does not qualify for append, and b does not qualify for prepend, gotta make a whole new string
|
---|
803 | size_t newCapacity = expandedSize(length, 0);
|
---|
804 | UChar* d;
|
---|
805 | if (!allocChars(newCapacity).getValue(d))
|
---|
806 | return 0;
|
---|
807 | copyChars(d, a->data(), aSize);
|
---|
808 | copyChars(d + aSize, b->data(), bSize);
|
---|
809 | PassRefPtr<UString::Rep> result = UString::Rep::create(d, length);
|
---|
810 | result->baseString()->capacity = newCapacity;
|
---|
811 |
|
---|
812 | a->checkConsistency();
|
---|
813 | b->checkConsistency();
|
---|
814 | result->checkConsistency();
|
---|
815 |
|
---|
816 | return result;
|
---|
817 | }
|
---|
818 |
|
---|
819 | PassRefPtr<UString::Rep> concatenate(UString::Rep* rep, int i)
|
---|
820 | {
|
---|
821 | UChar buf[1 + sizeof(i) * 3];
|
---|
822 | UChar* end = buf + sizeof(buf) / sizeof(UChar);
|
---|
823 | UChar* p = end;
|
---|
824 |
|
---|
825 | if (i == 0)
|
---|
826 | *--p = '0';
|
---|
827 | else if (i == INT_MIN) {
|
---|
828 | char minBuf[1 + sizeof(i) * 3];
|
---|
829 | sprintf(minBuf, "%d", INT_MIN);
|
---|
830 | return concatenate(rep, minBuf);
|
---|
831 | } else {
|
---|
832 | bool negative = false;
|
---|
833 | if (i < 0) {
|
---|
834 | negative = true;
|
---|
835 | i = -i;
|
---|
836 | }
|
---|
837 | while (i) {
|
---|
838 | *--p = static_cast<unsigned short>((i % 10) + '0');
|
---|
839 | i /= 10;
|
---|
840 | }
|
---|
841 | if (negative)
|
---|
842 | *--p = '-';
|
---|
843 | }
|
---|
844 |
|
---|
845 | return concatenate(rep, p, static_cast<int>(end - p));
|
---|
846 |
|
---|
847 | }
|
---|
848 |
|
---|
849 | PassRefPtr<UString::Rep> concatenate(UString::Rep* rep, double d)
|
---|
850 | {
|
---|
851 | // avoid ever printing -NaN, in JS conceptually there is only one NaN value
|
---|
852 | if (isnan(d))
|
---|
853 | return concatenate(rep, "NaN");
|
---|
854 |
|
---|
855 | if (d == 0.0) // stringify -0 as 0
|
---|
856 | d = 0.0;
|
---|
857 |
|
---|
858 | char buf[80];
|
---|
859 | int decimalPoint;
|
---|
860 | int sign;
|
---|
861 |
|
---|
862 | char result[80];
|
---|
863 | WTF::dtoa(result, d, 0, &decimalPoint, &sign, NULL);
|
---|
864 | int length = static_cast<int>(strlen(result));
|
---|
865 |
|
---|
866 | int i = 0;
|
---|
867 | if (sign)
|
---|
868 | buf[i++] = '-';
|
---|
869 |
|
---|
870 | if (decimalPoint <= 0 && decimalPoint > -6) {
|
---|
871 | buf[i++] = '0';
|
---|
872 | buf[i++] = '.';
|
---|
873 | for (int j = decimalPoint; j < 0; j++)
|
---|
874 | buf[i++] = '0';
|
---|
875 | strcpy(buf + i, result);
|
---|
876 | } else if (decimalPoint <= 21 && decimalPoint > 0) {
|
---|
877 | if (length <= decimalPoint) {
|
---|
878 | strcpy(buf + i, result);
|
---|
879 | i += length;
|
---|
880 | for (int j = 0; j < decimalPoint - length; j++)
|
---|
881 | buf[i++] = '0';
|
---|
882 | buf[i] = '\0';
|
---|
883 | } else {
|
---|
884 | strncpy(buf + i, result, decimalPoint);
|
---|
885 | i += decimalPoint;
|
---|
886 | buf[i++] = '.';
|
---|
887 | strcpy(buf + i, result + decimalPoint);
|
---|
888 | }
|
---|
889 | } else if (result[0] < '0' || result[0] > '9')
|
---|
890 | strcpy(buf + i, result);
|
---|
891 | else {
|
---|
892 | buf[i++] = result[0];
|
---|
893 | if (length > 1) {
|
---|
894 | buf[i++] = '.';
|
---|
895 | strcpy(buf + i, result + 1);
|
---|
896 | i += length - 1;
|
---|
897 | }
|
---|
898 |
|
---|
899 | buf[i++] = 'e';
|
---|
900 | buf[i++] = (decimalPoint >= 0) ? '+' : '-';
|
---|
901 | // decimalPoint can't be more than 3 digits decimal given the
|
---|
902 | // nature of float representation
|
---|
903 | int exponential = decimalPoint - 1;
|
---|
904 | if (exponential < 0)
|
---|
905 | exponential = -exponential;
|
---|
906 | if (exponential >= 100)
|
---|
907 | buf[i++] = static_cast<char>('0' + exponential / 100);
|
---|
908 | if (exponential >= 10)
|
---|
909 | buf[i++] = static_cast<char>('0' + (exponential % 100) / 10);
|
---|
910 | buf[i++] = static_cast<char>('0' + exponential % 10);
|
---|
911 | buf[i++] = '\0';
|
---|
912 | }
|
---|
913 |
|
---|
914 | return concatenate(rep, buf);
|
---|
915 | }
|
---|
916 |
|
---|
917 | UString UString::from(int i)
|
---|
918 | {
|
---|
919 | UChar buf[1 + sizeof(i) * 3];
|
---|
920 | UChar* end = buf + sizeof(buf) / sizeof(UChar);
|
---|
921 | UChar* p = end;
|
---|
922 |
|
---|
923 | if (i == 0)
|
---|
924 | *--p = '0';
|
---|
925 | else if (i == INT_MIN) {
|
---|
926 | char minBuf[1 + sizeof(i) * 3];
|
---|
927 | sprintf(minBuf, "%d", INT_MIN);
|
---|
928 | return UString(minBuf);
|
---|
929 | } else {
|
---|
930 | bool negative = false;
|
---|
931 | if (i < 0) {
|
---|
932 | negative = true;
|
---|
933 | i = -i;
|
---|
934 | }
|
---|
935 | while (i) {
|
---|
936 | *--p = static_cast<unsigned short>((i % 10) + '0');
|
---|
937 | i /= 10;
|
---|
938 | }
|
---|
939 | if (negative)
|
---|
940 | *--p = '-';
|
---|
941 | }
|
---|
942 |
|
---|
943 | return UString(p, static_cast<int>(end - p));
|
---|
944 | }
|
---|
945 |
|
---|
946 | UString UString::from(long long i)
|
---|
947 | {
|
---|
948 | UChar buf[1 + sizeof(i) * 3];
|
---|
949 | UChar* end = buf + sizeof(buf) / sizeof(UChar);
|
---|
950 | UChar* p = end;
|
---|
951 |
|
---|
952 | if (i == 0)
|
---|
953 | *--p = '0';
|
---|
954 | else if (i == std::numeric_limits<long long>::min()) {
|
---|
955 | char minBuf[1 + sizeof(i) * 3];
|
---|
956 | #if PLATFORM(WIN_OS)
|
---|
957 | snprintf(minBuf, sizeof(minBuf) - 1, "%I64d", std::numeric_limits<long long>::min());
|
---|
958 | #else
|
---|
959 | snprintf(minBuf, sizeof(minBuf) - 1, "%lld", std::numeric_limits<long long>::min());
|
---|
960 | #endif
|
---|
961 | return UString(minBuf);
|
---|
962 | } else {
|
---|
963 | bool negative = false;
|
---|
964 | if (i < 0) {
|
---|
965 | negative = true;
|
---|
966 | i = -i;
|
---|
967 | }
|
---|
968 | while (i) {
|
---|
969 | *--p = static_cast<unsigned short>((i % 10) + '0');
|
---|
970 | i /= 10;
|
---|
971 | }
|
---|
972 | if (negative)
|
---|
973 | *--p = '-';
|
---|
974 | }
|
---|
975 |
|
---|
976 | return UString(p, static_cast<int>(end - p));
|
---|
977 | }
|
---|
978 |
|
---|
979 | UString UString::from(unsigned int u)
|
---|
980 | {
|
---|
981 | UChar buf[sizeof(u) * 3];
|
---|
982 | UChar* end = buf + sizeof(buf) / sizeof(UChar);
|
---|
983 | UChar* p = end;
|
---|
984 |
|
---|
985 | if (u == 0)
|
---|
986 | *--p = '0';
|
---|
987 | else {
|
---|
988 | while (u) {
|
---|
989 | *--p = static_cast<unsigned short>((u % 10) + '0');
|
---|
990 | u /= 10;
|
---|
991 | }
|
---|
992 | }
|
---|
993 |
|
---|
994 | return UString(p, static_cast<int>(end - p));
|
---|
995 | }
|
---|
996 |
|
---|
997 | UString UString::from(long l)
|
---|
998 | {
|
---|
999 | UChar buf[1 + sizeof(l) * 3];
|
---|
1000 | UChar* end = buf + sizeof(buf) / sizeof(UChar);
|
---|
1001 | UChar* p = end;
|
---|
1002 |
|
---|
1003 | if (l == 0)
|
---|
1004 | *--p = '0';
|
---|
1005 | else if (l == LONG_MIN) {
|
---|
1006 | char minBuf[1 + sizeof(l) * 3];
|
---|
1007 | sprintf(minBuf, "%ld", LONG_MIN);
|
---|
1008 | return UString(minBuf);
|
---|
1009 | } else {
|
---|
1010 | bool negative = false;
|
---|
1011 | if (l < 0) {
|
---|
1012 | negative = true;
|
---|
1013 | l = -l;
|
---|
1014 | }
|
---|
1015 | while (l) {
|
---|
1016 | *--p = static_cast<unsigned short>((l % 10) + '0');
|
---|
1017 | l /= 10;
|
---|
1018 | }
|
---|
1019 | if (negative)
|
---|
1020 | *--p = '-';
|
---|
1021 | }
|
---|
1022 |
|
---|
1023 | return UString(p, static_cast<int>(end - p));
|
---|
1024 | }
|
---|
1025 |
|
---|
1026 | UString UString::from(double d)
|
---|
1027 | {
|
---|
1028 | // avoid ever printing -NaN, in JS conceptually there is only one NaN value
|
---|
1029 | if (isnan(d))
|
---|
1030 | return "NaN";
|
---|
1031 | if (!d)
|
---|
1032 | return "0"; // -0 -> "0"
|
---|
1033 |
|
---|
1034 | char buf[80];
|
---|
1035 | int decimalPoint;
|
---|
1036 | int sign;
|
---|
1037 |
|
---|
1038 | char result[80];
|
---|
1039 | WTF::dtoa(result, d, 0, &decimalPoint, &sign, NULL);
|
---|
1040 | int length = static_cast<int>(strlen(result));
|
---|
1041 |
|
---|
1042 | int i = 0;
|
---|
1043 | if (sign)
|
---|
1044 | buf[i++] = '-';
|
---|
1045 |
|
---|
1046 | if (decimalPoint <= 0 && decimalPoint > -6) {
|
---|
1047 | buf[i++] = '0';
|
---|
1048 | buf[i++] = '.';
|
---|
1049 | for (int j = decimalPoint; j < 0; j++)
|
---|
1050 | buf[i++] = '0';
|
---|
1051 | strcpy(buf + i, result);
|
---|
1052 | } else if (decimalPoint <= 21 && decimalPoint > 0) {
|
---|
1053 | if (length <= decimalPoint) {
|
---|
1054 | strcpy(buf + i, result);
|
---|
1055 | i += length;
|
---|
1056 | for (int j = 0; j < decimalPoint - length; j++)
|
---|
1057 | buf[i++] = '0';
|
---|
1058 | buf[i] = '\0';
|
---|
1059 | } else {
|
---|
1060 | strncpy(buf + i, result, decimalPoint);
|
---|
1061 | i += decimalPoint;
|
---|
1062 | buf[i++] = '.';
|
---|
1063 | strcpy(buf + i, result + decimalPoint);
|
---|
1064 | }
|
---|
1065 | } else if (result[0] < '0' || result[0] > '9')
|
---|
1066 | strcpy(buf + i, result);
|
---|
1067 | else {
|
---|
1068 | buf[i++] = result[0];
|
---|
1069 | if (length > 1) {
|
---|
1070 | buf[i++] = '.';
|
---|
1071 | strcpy(buf + i, result + 1);
|
---|
1072 | i += length - 1;
|
---|
1073 | }
|
---|
1074 |
|
---|
1075 | buf[i++] = 'e';
|
---|
1076 | buf[i++] = (decimalPoint >= 0) ? '+' : '-';
|
---|
1077 | // decimalPoint can't be more than 3 digits decimal given the
|
---|
1078 | // nature of float representation
|
---|
1079 | int exponential = decimalPoint - 1;
|
---|
1080 | if (exponential < 0)
|
---|
1081 | exponential = -exponential;
|
---|
1082 | if (exponential >= 100)
|
---|
1083 | buf[i++] = static_cast<char>('0' + exponential / 100);
|
---|
1084 | if (exponential >= 10)
|
---|
1085 | buf[i++] = static_cast<char>('0' + (exponential % 100) / 10);
|
---|
1086 | buf[i++] = static_cast<char>('0' + exponential % 10);
|
---|
1087 | buf[i++] = '\0';
|
---|
1088 | }
|
---|
1089 |
|
---|
1090 | return UString(buf);
|
---|
1091 | }
|
---|
1092 |
|
---|
1093 | UString UString::spliceSubstringsWithSeparators(const Range* substringRanges, int rangeCount, const UString* separators, int separatorCount) const
|
---|
1094 | {
|
---|
1095 | m_rep->checkConsistency();
|
---|
1096 |
|
---|
1097 | if (rangeCount == 1 && separatorCount == 0) {
|
---|
1098 | int thisSize = size();
|
---|
1099 | int position = substringRanges[0].position;
|
---|
1100 | int length = substringRanges[0].length;
|
---|
1101 | if (position <= 0 && length >= thisSize)
|
---|
1102 | return *this;
|
---|
1103 | return UString::Rep::create(m_rep, max(0, position), min(thisSize, length));
|
---|
1104 | }
|
---|
1105 |
|
---|
1106 | int totalLength = 0;
|
---|
1107 | for (int i = 0; i < rangeCount; i++)
|
---|
1108 | totalLength += substringRanges[i].length;
|
---|
1109 | for (int i = 0; i < separatorCount; i++)
|
---|
1110 | totalLength += separators[i].size();
|
---|
1111 |
|
---|
1112 | if (totalLength == 0)
|
---|
1113 | return "";
|
---|
1114 |
|
---|
1115 | UChar* buffer;
|
---|
1116 | if (!allocChars(totalLength).getValue(buffer))
|
---|
1117 | return null();
|
---|
1118 |
|
---|
1119 | int maxCount = max(rangeCount, separatorCount);
|
---|
1120 | int bufferPos = 0;
|
---|
1121 | for (int i = 0; i < maxCount; i++) {
|
---|
1122 | if (i < rangeCount) {
|
---|
1123 | copyChars(buffer + bufferPos, data() + substringRanges[i].position, substringRanges[i].length);
|
---|
1124 | bufferPos += substringRanges[i].length;
|
---|
1125 | }
|
---|
1126 | if (i < separatorCount) {
|
---|
1127 | copyChars(buffer + bufferPos, separators[i].data(), separators[i].size());
|
---|
1128 | bufferPos += separators[i].size();
|
---|
1129 | }
|
---|
1130 | }
|
---|
1131 |
|
---|
1132 | return UString::Rep::create(buffer, totalLength);
|
---|
1133 | }
|
---|
1134 |
|
---|
1135 | UString UString::replaceRange(int rangeStart, int rangeLength, const UString& replacement) const
|
---|
1136 | {
|
---|
1137 | m_rep->checkConsistency();
|
---|
1138 |
|
---|
1139 | int replacementLength = replacement.size();
|
---|
1140 | int totalLength = size() - rangeLength + replacementLength;
|
---|
1141 | if (totalLength == 0)
|
---|
1142 | return "";
|
---|
1143 |
|
---|
1144 | UChar* buffer;
|
---|
1145 | if (!allocChars(totalLength).getValue(buffer))
|
---|
1146 | return null();
|
---|
1147 |
|
---|
1148 | copyChars(buffer, data(), rangeStart);
|
---|
1149 | copyChars(buffer + rangeStart, replacement.data(), replacementLength);
|
---|
1150 | int rangeEnd = rangeStart + rangeLength;
|
---|
1151 | copyChars(buffer + rangeStart + replacementLength, data() + rangeEnd, size() - rangeEnd);
|
---|
1152 |
|
---|
1153 | return UString::Rep::create(buffer, totalLength);
|
---|
1154 | }
|
---|
1155 |
|
---|
1156 |
|
---|
1157 | UString& UString::append(const UString &t)
|
---|
1158 | {
|
---|
1159 | m_rep->checkConsistency();
|
---|
1160 | t.rep()->checkConsistency();
|
---|
1161 |
|
---|
1162 | int thisSize = size();
|
---|
1163 | int thisOffset = m_rep->offset;
|
---|
1164 | int tSize = t.size();
|
---|
1165 | int length = thisSize + tSize;
|
---|
1166 | BaseString* base = m_rep->baseString();
|
---|
1167 |
|
---|
1168 | // possible cases:
|
---|
1169 | if (thisSize == 0) {
|
---|
1170 | // this is empty
|
---|
1171 | *this = t;
|
---|
1172 | } else if (tSize == 0) {
|
---|
1173 | // t is empty
|
---|
1174 | } else if (m_rep == base && !base->isShared()) {
|
---|
1175 | // this is direct and has refcount of 1 (so we can just alter it directly)
|
---|
1176 | expandCapacity(newCapacityWithOverflowCheck(thisOffset, length));
|
---|
1177 | if (data()) {
|
---|
1178 | copyChars(m_rep->data() + thisSize, t.data(), tSize);
|
---|
1179 | m_rep->len = length;
|
---|
1180 | m_rep->_hash = 0;
|
---|
1181 | }
|
---|
1182 | } else if (thisOffset + thisSize == base->usedCapacity && thisSize >= minShareSize && !base->isBufferReadOnly()) {
|
---|
1183 | // this reaches the end of the buffer - extend it if it's long enough to append to
|
---|
1184 | expandCapacity(newCapacityWithOverflowCheck(thisOffset, length));
|
---|
1185 | if (data()) {
|
---|
1186 | copyChars(m_rep->data() + thisSize, t.data(), tSize);
|
---|
1187 | m_rep = Rep::create(m_rep, 0, length);
|
---|
1188 | }
|
---|
1189 | } else {
|
---|
1190 | // This is shared in some way that prevents us from modifying base, so we must make a whole new string.
|
---|
1191 | size_t newCapacity = expandedSize(length, 0);
|
---|
1192 | UChar* d;
|
---|
1193 | if (!allocChars(newCapacity).getValue(d))
|
---|
1194 | makeNull();
|
---|
1195 | else {
|
---|
1196 | copyChars(d, data(), thisSize);
|
---|
1197 | copyChars(d + thisSize, t.data(), tSize);
|
---|
1198 | m_rep = Rep::create(d, length);
|
---|
1199 | m_rep->baseString()->capacity = newCapacity;
|
---|
1200 | }
|
---|
1201 | }
|
---|
1202 |
|
---|
1203 | m_rep->checkConsistency();
|
---|
1204 | t.rep()->checkConsistency();
|
---|
1205 |
|
---|
1206 | return *this;
|
---|
1207 | }
|
---|
1208 |
|
---|
1209 | UString& UString::append(const UChar* tData, int tSize)
|
---|
1210 | {
|
---|
1211 | m_rep = concatenate(m_rep.release(), tData, tSize);
|
---|
1212 | return *this;
|
---|
1213 | }
|
---|
1214 |
|
---|
1215 | UString& UString::append(const char* t)
|
---|
1216 | {
|
---|
1217 | m_rep = concatenate(m_rep.release(), t);
|
---|
1218 | return *this;
|
---|
1219 | }
|
---|
1220 |
|
---|
1221 | UString& UString::append(UChar c)
|
---|
1222 | {
|
---|
1223 | m_rep->checkConsistency();
|
---|
1224 |
|
---|
1225 | int thisOffset = m_rep->offset;
|
---|
1226 | int length = size();
|
---|
1227 | BaseString* base = m_rep->baseString();
|
---|
1228 |
|
---|
1229 | // possible cases:
|
---|
1230 | if (length == 0) {
|
---|
1231 | // this is empty - must make a new m_rep because we don't want to pollute the shared empty one
|
---|
1232 | size_t newCapacity = expandedSize(1, 0);
|
---|
1233 | UChar* d;
|
---|
1234 | if (!allocChars(newCapacity).getValue(d))
|
---|
1235 | makeNull();
|
---|
1236 | else {
|
---|
1237 | d[0] = c;
|
---|
1238 | m_rep = Rep::create(d, 1);
|
---|
1239 | m_rep->baseString()->capacity = newCapacity;
|
---|
1240 | }
|
---|
1241 | } else if (m_rep == base && !base->isShared()) {
|
---|
1242 | // this is direct and has refcount of 1 (so we can just alter it directly)
|
---|
1243 | expandCapacity(newCapacityWithOverflowCheck(thisOffset, length, true));
|
---|
1244 | UChar* d = m_rep->data();
|
---|
1245 | if (d) {
|
---|
1246 | d[length] = c;
|
---|
1247 | m_rep->len = length + 1;
|
---|
1248 | m_rep->_hash = 0;
|
---|
1249 | }
|
---|
1250 | } else if (thisOffset + length == base->usedCapacity && length >= minShareSize && !base->isBufferReadOnly()) {
|
---|
1251 | // this reaches the end of the string - extend it and share
|
---|
1252 | expandCapacity(newCapacityWithOverflowCheck(thisOffset, length, true));
|
---|
1253 | UChar* d = m_rep->data();
|
---|
1254 | if (d) {
|
---|
1255 | d[length] = c;
|
---|
1256 | m_rep = Rep::create(m_rep, 0, length + 1);
|
---|
1257 | }
|
---|
1258 | } else {
|
---|
1259 | // This is shared in some way that prevents us from modifying base, so we must make a whole new string.
|
---|
1260 | size_t newCapacity = expandedSize(length + 1, 0);
|
---|
1261 | UChar* d;
|
---|
1262 | if (!allocChars(newCapacity).getValue(d))
|
---|
1263 | makeNull();
|
---|
1264 | else {
|
---|
1265 | copyChars(d, data(), length);
|
---|
1266 | d[length] = c;
|
---|
1267 | m_rep = Rep::create(d, length + 1);
|
---|
1268 | m_rep->baseString()->capacity = newCapacity;
|
---|
1269 | }
|
---|
1270 | }
|
---|
1271 |
|
---|
1272 | m_rep->checkConsistency();
|
---|
1273 |
|
---|
1274 | return *this;
|
---|
1275 | }
|
---|
1276 |
|
---|
1277 | bool UString::getCString(CStringBuffer& buffer) const
|
---|
1278 | {
|
---|
1279 | int length = size();
|
---|
1280 | int neededSize = length + 1;
|
---|
1281 | buffer.resize(neededSize);
|
---|
1282 | char* buf = buffer.data();
|
---|
1283 |
|
---|
1284 | UChar ored = 0;
|
---|
1285 | const UChar* p = data();
|
---|
1286 | char* q = buf;
|
---|
1287 | const UChar* limit = p + length;
|
---|
1288 | while (p != limit) {
|
---|
1289 | UChar c = p[0];
|
---|
1290 | ored |= c;
|
---|
1291 | *q = static_cast<char>(c);
|
---|
1292 | ++p;
|
---|
1293 | ++q;
|
---|
1294 | }
|
---|
1295 | *q = '\0';
|
---|
1296 |
|
---|
1297 | return !(ored & 0xFF00);
|
---|
1298 | }
|
---|
1299 |
|
---|
1300 | char* UString::ascii() const
|
---|
1301 | {
|
---|
1302 | int length = size();
|
---|
1303 | int neededSize = length + 1;
|
---|
1304 | delete[] statBuffer;
|
---|
1305 | statBuffer = new char[neededSize];
|
---|
1306 |
|
---|
1307 | const UChar* p = data();
|
---|
1308 | char* q = statBuffer;
|
---|
1309 | const UChar* limit = p + length;
|
---|
1310 | while (p != limit) {
|
---|
1311 | *q = static_cast<char>(p[0]);
|
---|
1312 | ++p;
|
---|
1313 | ++q;
|
---|
1314 | }
|
---|
1315 | *q = '\0';
|
---|
1316 |
|
---|
1317 | return statBuffer;
|
---|
1318 | }
|
---|
1319 |
|
---|
1320 | UString& UString::operator=(const char* c)
|
---|
1321 | {
|
---|
1322 | if (!c) {
|
---|
1323 | m_rep = &Rep::null();
|
---|
1324 | return *this;
|
---|
1325 | }
|
---|
1326 |
|
---|
1327 | if (!c[0]) {
|
---|
1328 | m_rep = &Rep::empty();
|
---|
1329 | return *this;
|
---|
1330 | }
|
---|
1331 |
|
---|
1332 | int l = static_cast<int>(strlen(c));
|
---|
1333 | UChar* d;
|
---|
1334 | BaseString* base = m_rep->baseString();
|
---|
1335 | if (!base->isShared() && l <= base->capacity && m_rep == base && m_rep->offset == 0 && base->preCapacity == 0) {
|
---|
1336 | d = base->buf;
|
---|
1337 | m_rep->_hash = 0;
|
---|
1338 | m_rep->len = l;
|
---|
1339 | } else {
|
---|
1340 | if (!allocChars(l).getValue(d)) {
|
---|
1341 | makeNull();
|
---|
1342 | return *this;
|
---|
1343 | }
|
---|
1344 | m_rep = Rep::create(d, l);
|
---|
1345 | }
|
---|
1346 | for (int i = 0; i < l; i++)
|
---|
1347 | d[i] = static_cast<unsigned char>(c[i]); // use unsigned char to zero-extend instead of sign-extend
|
---|
1348 |
|
---|
1349 | return *this;
|
---|
1350 | }
|
---|
1351 |
|
---|
1352 | bool UString::is8Bit() const
|
---|
1353 | {
|
---|
1354 | const UChar* u = data();
|
---|
1355 | const UChar* limit = u + size();
|
---|
1356 | while (u < limit) {
|
---|
1357 | if (u[0] > 0xFF)
|
---|
1358 | return false;
|
---|
1359 | ++u;
|
---|
1360 | }
|
---|
1361 |
|
---|
1362 | return true;
|
---|
1363 | }
|
---|
1364 |
|
---|
1365 | UChar UString::operator[](int pos) const
|
---|
1366 | {
|
---|
1367 | if (pos >= size())
|
---|
1368 | return '\0';
|
---|
1369 | return data()[pos];
|
---|
1370 | }
|
---|
1371 |
|
---|
1372 | double UString::toDouble(bool tolerateTrailingJunk, bool tolerateEmptyString) const
|
---|
1373 | {
|
---|
1374 | if (size() == 1) {
|
---|
1375 | UChar c = data()[0];
|
---|
1376 | if (isASCIIDigit(c))
|
---|
1377 | return c - '0';
|
---|
1378 | if (isASCIISpace(c) && tolerateEmptyString)
|
---|
1379 | return 0;
|
---|
1380 | return NaN;
|
---|
1381 | }
|
---|
1382 |
|
---|
1383 | // FIXME: If tolerateTrailingJunk is true, then we want to tolerate non-8-bit junk
|
---|
1384 | // after the number, so this is too strict a check.
|
---|
1385 | CStringBuffer s;
|
---|
1386 | if (!getCString(s))
|
---|
1387 | return NaN;
|
---|
1388 | const char* c = s.data();
|
---|
1389 |
|
---|
1390 | // skip leading white space
|
---|
1391 | while (isASCIISpace(*c))
|
---|
1392 | c++;
|
---|
1393 |
|
---|
1394 | // empty string ?
|
---|
1395 | if (*c == '\0')
|
---|
1396 | return tolerateEmptyString ? 0.0 : NaN;
|
---|
1397 |
|
---|
1398 | double d;
|
---|
1399 |
|
---|
1400 | // hex number ?
|
---|
1401 | if (*c == '0' && (*(c + 1) == 'x' || *(c + 1) == 'X')) {
|
---|
1402 | const char* firstDigitPosition = c + 2;
|
---|
1403 | c++;
|
---|
1404 | d = 0.0;
|
---|
1405 | while (*(++c)) {
|
---|
1406 | if (*c >= '0' && *c <= '9')
|
---|
1407 | d = d * 16.0 + *c - '0';
|
---|
1408 | else if ((*c >= 'A' && *c <= 'F') || (*c >= 'a' && *c <= 'f'))
|
---|
1409 | d = d * 16.0 + (*c & 0xdf) - 'A' + 10.0;
|
---|
1410 | else
|
---|
1411 | break;
|
---|
1412 | }
|
---|
1413 |
|
---|
1414 | if (d >= mantissaOverflowLowerBound)
|
---|
1415 | d = parseIntOverflow(firstDigitPosition, c - firstDigitPosition, 16);
|
---|
1416 | } else {
|
---|
1417 | // regular number ?
|
---|
1418 | char* end;
|
---|
1419 | d = WTF::strtod(c, &end);
|
---|
1420 | if ((d != 0.0 || end != c) && d != Inf && d != -Inf) {
|
---|
1421 | c = end;
|
---|
1422 | } else {
|
---|
1423 | double sign = 1.0;
|
---|
1424 |
|
---|
1425 | if (*c == '+')
|
---|
1426 | c++;
|
---|
1427 | else if (*c == '-') {
|
---|
1428 | sign = -1.0;
|
---|
1429 | c++;
|
---|
1430 | }
|
---|
1431 |
|
---|
1432 | // We used strtod() to do the conversion. However, strtod() handles
|
---|
1433 | // infinite values slightly differently than JavaScript in that it
|
---|
1434 | // converts the string "inf" with any capitalization to infinity,
|
---|
1435 | // whereas the ECMA spec requires that it be converted to NaN.
|
---|
1436 |
|
---|
1437 | if (c[0] == 'I' && c[1] == 'n' && c[2] == 'f' && c[3] == 'i' && c[4] == 'n' && c[5] == 'i' && c[6] == 't' && c[7] == 'y') {
|
---|
1438 | d = sign * Inf;
|
---|
1439 | c += 8;
|
---|
1440 | } else if ((d == Inf || d == -Inf) && *c != 'I' && *c != 'i')
|
---|
1441 | c = end;
|
---|
1442 | else
|
---|
1443 | return NaN;
|
---|
1444 | }
|
---|
1445 | }
|
---|
1446 |
|
---|
1447 | // allow trailing white space
|
---|
1448 | while (isASCIISpace(*c))
|
---|
1449 | c++;
|
---|
1450 | // don't allow anything after - unless tolerant=true
|
---|
1451 | if (!tolerateTrailingJunk && *c != '\0')
|
---|
1452 | d = NaN;
|
---|
1453 |
|
---|
1454 | return d;
|
---|
1455 | }
|
---|
1456 |
|
---|
1457 | double UString::toDouble(bool tolerateTrailingJunk) const
|
---|
1458 | {
|
---|
1459 | return toDouble(tolerateTrailingJunk, true);
|
---|
1460 | }
|
---|
1461 |
|
---|
1462 | double UString::toDouble() const
|
---|
1463 | {
|
---|
1464 | return toDouble(false, true);
|
---|
1465 | }
|
---|
1466 |
|
---|
1467 | uint32_t UString::toUInt32(bool* ok) const
|
---|
1468 | {
|
---|
1469 | double d = toDouble();
|
---|
1470 | bool b = true;
|
---|
1471 |
|
---|
1472 | if (d != static_cast<uint32_t>(d)) {
|
---|
1473 | b = false;
|
---|
1474 | d = 0;
|
---|
1475 | }
|
---|
1476 |
|
---|
1477 | if (ok)
|
---|
1478 | *ok = b;
|
---|
1479 |
|
---|
1480 | return static_cast<uint32_t>(d);
|
---|
1481 | }
|
---|
1482 |
|
---|
1483 | uint32_t UString::toUInt32(bool* ok, bool tolerateEmptyString) const
|
---|
1484 | {
|
---|
1485 | double d = toDouble(false, tolerateEmptyString);
|
---|
1486 | bool b = true;
|
---|
1487 |
|
---|
1488 | if (d != static_cast<uint32_t>(d)) {
|
---|
1489 | b = false;
|
---|
1490 | d = 0;
|
---|
1491 | }
|
---|
1492 |
|
---|
1493 | if (ok)
|
---|
1494 | *ok = b;
|
---|
1495 |
|
---|
1496 | return static_cast<uint32_t>(d);
|
---|
1497 | }
|
---|
1498 |
|
---|
1499 | uint32_t UString::toStrictUInt32(bool* ok) const
|
---|
1500 | {
|
---|
1501 | if (ok)
|
---|
1502 | *ok = false;
|
---|
1503 |
|
---|
1504 | // Empty string is not OK.
|
---|
1505 | int len = m_rep->len;
|
---|
1506 | if (len == 0)
|
---|
1507 | return 0;
|
---|
1508 | const UChar* p = m_rep->data();
|
---|
1509 | unsigned short c = p[0];
|
---|
1510 |
|
---|
1511 | // If the first digit is 0, only 0 itself is OK.
|
---|
1512 | if (c == '0') {
|
---|
1513 | if (len == 1 && ok)
|
---|
1514 | *ok = true;
|
---|
1515 | return 0;
|
---|
1516 | }
|
---|
1517 |
|
---|
1518 | // Convert to UInt32, checking for overflow.
|
---|
1519 | uint32_t i = 0;
|
---|
1520 | while (1) {
|
---|
1521 | // Process character, turning it into a digit.
|
---|
1522 | if (c < '0' || c > '9')
|
---|
1523 | return 0;
|
---|
1524 | const unsigned d = c - '0';
|
---|
1525 |
|
---|
1526 | // Multiply by 10, checking for overflow out of 32 bits.
|
---|
1527 | if (i > 0xFFFFFFFFU / 10)
|
---|
1528 | return 0;
|
---|
1529 | i *= 10;
|
---|
1530 |
|
---|
1531 | // Add in the digit, checking for overflow out of 32 bits.
|
---|
1532 | const unsigned max = 0xFFFFFFFFU - d;
|
---|
1533 | if (i > max)
|
---|
1534 | return 0;
|
---|
1535 | i += d;
|
---|
1536 |
|
---|
1537 | // Handle end of string.
|
---|
1538 | if (--len == 0) {
|
---|
1539 | if (ok)
|
---|
1540 | *ok = true;
|
---|
1541 | return i;
|
---|
1542 | }
|
---|
1543 |
|
---|
1544 | // Get next character.
|
---|
1545 | c = *(++p);
|
---|
1546 | }
|
---|
1547 | }
|
---|
1548 |
|
---|
1549 | int UString::find(const UString& f, int pos) const
|
---|
1550 | {
|
---|
1551 | int fsz = f.size();
|
---|
1552 |
|
---|
1553 | if (pos < 0)
|
---|
1554 | pos = 0;
|
---|
1555 |
|
---|
1556 | if (fsz == 1) {
|
---|
1557 | UChar ch = f[0];
|
---|
1558 | const UChar* end = data() + size();
|
---|
1559 | for (const UChar* c = data() + pos; c < end; c++) {
|
---|
1560 | if (*c == ch)
|
---|
1561 | return static_cast<int>(c - data());
|
---|
1562 | }
|
---|
1563 | return -1;
|
---|
1564 | }
|
---|
1565 |
|
---|
1566 | int sz = size();
|
---|
1567 | if (sz < fsz)
|
---|
1568 | return -1;
|
---|
1569 | if (fsz == 0)
|
---|
1570 | return pos;
|
---|
1571 | const UChar* end = data() + sz - fsz;
|
---|
1572 | int fsizeminusone = (fsz - 1) * sizeof(UChar);
|
---|
1573 | const UChar* fdata = f.data();
|
---|
1574 | unsigned short fchar = fdata[0];
|
---|
1575 | ++fdata;
|
---|
1576 | for (const UChar* c = data() + pos; c <= end; c++) {
|
---|
1577 | if (c[0] == fchar && !memcmp(c + 1, fdata, fsizeminusone))
|
---|
1578 | return static_cast<int>(c - data());
|
---|
1579 | }
|
---|
1580 |
|
---|
1581 | return -1;
|
---|
1582 | }
|
---|
1583 |
|
---|
1584 | int UString::find(UChar ch, int pos) const
|
---|
1585 | {
|
---|
1586 | if (pos < 0)
|
---|
1587 | pos = 0;
|
---|
1588 | const UChar* end = data() + size();
|
---|
1589 | for (const UChar* c = data() + pos; c < end; c++) {
|
---|
1590 | if (*c == ch)
|
---|
1591 | return static_cast<int>(c - data());
|
---|
1592 | }
|
---|
1593 |
|
---|
1594 | return -1;
|
---|
1595 | }
|
---|
1596 |
|
---|
1597 | int UString::rfind(const UString& f, int pos) const
|
---|
1598 | {
|
---|
1599 | int sz = size();
|
---|
1600 | int fsz = f.size();
|
---|
1601 | if (sz < fsz)
|
---|
1602 | return -1;
|
---|
1603 | if (pos < 0)
|
---|
1604 | pos = 0;
|
---|
1605 | if (pos > sz - fsz)
|
---|
1606 | pos = sz - fsz;
|
---|
1607 | if (fsz == 0)
|
---|
1608 | return pos;
|
---|
1609 | int fsizeminusone = (fsz - 1) * sizeof(UChar);
|
---|
1610 | const UChar* fdata = f.data();
|
---|
1611 | for (const UChar* c = data() + pos; c >= data(); c--) {
|
---|
1612 | if (*c == *fdata && !memcmp(c + 1, fdata + 1, fsizeminusone))
|
---|
1613 | return static_cast<int>(c - data());
|
---|
1614 | }
|
---|
1615 |
|
---|
1616 | return -1;
|
---|
1617 | }
|
---|
1618 |
|
---|
1619 | int UString::rfind(UChar ch, int pos) const
|
---|
1620 | {
|
---|
1621 | if (isEmpty())
|
---|
1622 | return -1;
|
---|
1623 | if (pos + 1 >= size())
|
---|
1624 | pos = size() - 1;
|
---|
1625 | for (const UChar* c = data() + pos; c >= data(); c--) {
|
---|
1626 | if (*c == ch)
|
---|
1627 | return static_cast<int>(c - data());
|
---|
1628 | }
|
---|
1629 |
|
---|
1630 | return -1;
|
---|
1631 | }
|
---|
1632 |
|
---|
1633 | UString UString::substr(int pos, int len) const
|
---|
1634 | {
|
---|
1635 | int s = size();
|
---|
1636 |
|
---|
1637 | if (pos < 0)
|
---|
1638 | pos = 0;
|
---|
1639 | else if (pos >= s)
|
---|
1640 | pos = s;
|
---|
1641 | if (len < 0)
|
---|
1642 | len = s;
|
---|
1643 | if (pos + len >= s)
|
---|
1644 | len = s - pos;
|
---|
1645 |
|
---|
1646 | if (pos == 0 && len == s)
|
---|
1647 | return *this;
|
---|
1648 |
|
---|
1649 | return UString(Rep::create(m_rep, pos, len));
|
---|
1650 | }
|
---|
1651 |
|
---|
1652 | bool operator==(const UString& s1, const char *s2)
|
---|
1653 | {
|
---|
1654 | if (s2 == 0)
|
---|
1655 | return s1.isEmpty();
|
---|
1656 |
|
---|
1657 | const UChar* u = s1.data();
|
---|
1658 | const UChar* uend = u + s1.size();
|
---|
1659 | while (u != uend && *s2) {
|
---|
1660 | if (u[0] != (unsigned char)*s2)
|
---|
1661 | return false;
|
---|
1662 | s2++;
|
---|
1663 | u++;
|
---|
1664 | }
|
---|
1665 |
|
---|
1666 | return u == uend && *s2 == 0;
|
---|
1667 | }
|
---|
1668 |
|
---|
1669 | bool operator<(const UString& s1, const UString& s2)
|
---|
1670 | {
|
---|
1671 | const int l1 = s1.size();
|
---|
1672 | const int l2 = s2.size();
|
---|
1673 | const int lmin = l1 < l2 ? l1 : l2;
|
---|
1674 | const UChar* c1 = s1.data();
|
---|
1675 | const UChar* c2 = s2.data();
|
---|
1676 | int l = 0;
|
---|
1677 | while (l < lmin && *c1 == *c2) {
|
---|
1678 | c1++;
|
---|
1679 | c2++;
|
---|
1680 | l++;
|
---|
1681 | }
|
---|
1682 | if (l < lmin)
|
---|
1683 | return (c1[0] < c2[0]);
|
---|
1684 |
|
---|
1685 | return (l1 < l2);
|
---|
1686 | }
|
---|
1687 |
|
---|
1688 | bool operator>(const UString& s1, const UString& s2)
|
---|
1689 | {
|
---|
1690 | const int l1 = s1.size();
|
---|
1691 | const int l2 = s2.size();
|
---|
1692 | const int lmin = l1 < l2 ? l1 : l2;
|
---|
1693 | const UChar* c1 = s1.data();
|
---|
1694 | const UChar* c2 = s2.data();
|
---|
1695 | int l = 0;
|
---|
1696 | while (l < lmin && *c1 == *c2) {
|
---|
1697 | c1++;
|
---|
1698 | c2++;
|
---|
1699 | l++;
|
---|
1700 | }
|
---|
1701 | if (l < lmin)
|
---|
1702 | return (c1[0] > c2[0]);
|
---|
1703 |
|
---|
1704 | return (l1 > l2);
|
---|
1705 | }
|
---|
1706 |
|
---|
1707 | int compare(const UString& s1, const UString& s2)
|
---|
1708 | {
|
---|
1709 | const int l1 = s1.size();
|
---|
1710 | const int l2 = s2.size();
|
---|
1711 | const int lmin = l1 < l2 ? l1 : l2;
|
---|
1712 | const UChar* c1 = s1.data();
|
---|
1713 | const UChar* c2 = s2.data();
|
---|
1714 | int l = 0;
|
---|
1715 | while (l < lmin && *c1 == *c2) {
|
---|
1716 | c1++;
|
---|
1717 | c2++;
|
---|
1718 | l++;
|
---|
1719 | }
|
---|
1720 |
|
---|
1721 | if (l < lmin)
|
---|
1722 | return (c1[0] > c2[0]) ? 1 : -1;
|
---|
1723 |
|
---|
1724 | if (l1 == l2)
|
---|
1725 | return 0;
|
---|
1726 |
|
---|
1727 | return (l1 > l2) ? 1 : -1;
|
---|
1728 | }
|
---|
1729 |
|
---|
1730 | bool equal(const UString::Rep* r, const UString::Rep* b)
|
---|
1731 | {
|
---|
1732 | int length = r->len;
|
---|
1733 | if (length != b->len)
|
---|
1734 | return false;
|
---|
1735 | const UChar* d = r->data();
|
---|
1736 | const UChar* s = b->data();
|
---|
1737 | for (int i = 0; i != length; ++i) {
|
---|
1738 | if (d[i] != s[i])
|
---|
1739 | return false;
|
---|
1740 | }
|
---|
1741 | return true;
|
---|
1742 | }
|
---|
1743 |
|
---|
1744 | CString UString::UTF8String(bool strict) const
|
---|
1745 | {
|
---|
1746 | // Allocate a buffer big enough to hold all the characters.
|
---|
1747 | const int length = size();
|
---|
1748 | Vector<char, 1024> buffer(length * 3);
|
---|
1749 |
|
---|
1750 | // Convert to runs of 8-bit characters.
|
---|
1751 | char* p = buffer.data();
|
---|
1752 | const UChar* d = reinterpret_cast<const UChar*>(&data()[0]);
|
---|
1753 | ConversionResult result = convertUTF16ToUTF8(&d, d + length, &p, p + buffer.size(), strict);
|
---|
1754 | if (result != conversionOK)
|
---|
1755 | return CString();
|
---|
1756 |
|
---|
1757 | return CString(buffer.data(), p - buffer.data());
|
---|
1758 | }
|
---|
1759 |
|
---|
1760 | // For use in error handling code paths -- having this not be inlined helps avoid PIC branches to fetch the global on Mac OS X.
|
---|
1761 | NEVER_INLINE void UString::makeNull()
|
---|
1762 | {
|
---|
1763 | m_rep = &Rep::null();
|
---|
1764 | }
|
---|
1765 |
|
---|
1766 | // For use in error handling code paths -- having this not be inlined helps avoid PIC branches to fetch the global on Mac OS X.
|
---|
1767 | NEVER_INLINE UString::Rep* UString::nullRep()
|
---|
1768 | {
|
---|
1769 | return &Rep::null();
|
---|
1770 | }
|
---|
1771 |
|
---|
1772 | } // namespace JSC
|
---|