summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/webkit/JavaScriptCore/runtime/UString.cpp
blob: 118751e35d00eeed327a936c55374b9f2dc13c17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
/*
 *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
 *  Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
 *  Copyright (C) 2007 Cameron Zwarich (cwzwarich@uwaterloo.ca)
 *  Copyright (C) 2009 Google Inc. All rights reserved.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 *
 */

#include "config.h"
#include "UString.h"

#include "JSGlobalObjectFunctions.h"
#include "Collector.h"
#include "dtoa.h"
#include "Identifier.h"
#include "Operations.h"
#include <ctype.h>
#include <float.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <wtf/ASCIICType.h>
#include <wtf/Assertions.h>
#include <wtf/MathExtras.h>
#include <wtf/Vector.h>
#include <wtf/unicode/UTF8.h>

#if HAVE(STRING_H)
#include <string.h>
#endif
#if HAVE(STRINGS_H)
#include <strings.h>
#endif

using namespace WTF;
using namespace WTF::Unicode;
using namespace std;

// This can be tuned differently per platform by putting platform #ifs right here.
// If you don't define this macro at all, then copyChars will just call directly
// to memcpy.
#define USTRING_COPY_CHARS_INLINE_CUTOFF 20

namespace JSC {
 
extern const double NaN;
extern const double Inf;

// This number must be at least 2 to avoid sharing empty, null as well as 1 character strings from SmallStrings.
static const int minLengthToShare = 10;

static inline size_t overflowIndicator() { return std::numeric_limits<size_t>::max(); }
static inline size_t maxUChars() { return std::numeric_limits<size_t>::max() / sizeof(UChar); }

static inline UChar* allocChars(size_t length)
{
    ASSERT(length);
    if (length > maxUChars())
        return 0;
    return static_cast<UChar*>(tryFastMalloc(sizeof(UChar) * length));
}

static inline UChar* reallocChars(UChar* buffer, size_t length)
{
    ASSERT(length);
    if (length > maxUChars())
        return 0;
    return static_cast<UChar*>(tryFastRealloc(buffer, sizeof(UChar) * length));
}

static inline void copyChars(UChar* destination, const UChar* source, unsigned numCharacters)
{
#ifdef USTRING_COPY_CHARS_INLINE_CUTOFF
    if (numCharacters <= USTRING_COPY_CHARS_INLINE_CUTOFF) {
        for (unsigned i = 0; i < numCharacters; ++i)
            destination[i] = source[i];
        return;
    }
#endif
    memcpy(destination, source, numCharacters * sizeof(UChar));
}

COMPILE_ASSERT(sizeof(UChar) == 2, uchar_is_2_bytes);

CString::CString(const char* c)
    : m_length(strlen(c))
    , m_data(new char[m_length + 1])
{
    memcpy(m_data, c, m_length + 1);
}

CString::CString(const char* c, size_t length)
    : m_length(length)
    , m_data(new char[length + 1])
{
    memcpy(m_data, c, m_length);
    m_data[m_length] = 0;
}

CString::CString(const CString& b)
{
    m_length = b.m_length;
    if (b.m_data) {
        m_data = new char[m_length + 1];
        memcpy(m_data, b.m_data, m_length + 1);
    } else
        m_data = 0;
}

CString::~CString()
{
    delete [] m_data;
}

CString CString::adopt(char* c, size_t length)
{
    CString s;
    s.m_data = c;
    s.m_length = length;
    return s;
}

CString& CString::append(const CString& t)
{
    char* n;
    n = new char[m_length + t.m_length + 1];
    if (m_length)
        memcpy(n, m_data, m_length);
    if (t.m_length)
        memcpy(n + m_length, t.m_data, t.m_length);
    m_length += t.m_length;
    n[m_length] = 0;

    delete [] m_data;
    m_data = n;

    return *this;
}

CString& CString::operator=(const char* c)
{
    if (m_data)
        delete [] m_data;
    m_length = strlen(c);
    m_data = new char[m_length + 1];
    memcpy(m_data, c, m_length + 1);

    return *this;
}

CString& CString::operator=(const CString& str)
{
    if (this == &str)
        return *this;

    if (m_data)
        delete [] m_data;
    m_length = str.m_length;
    if (str.m_data) {
        m_data = new char[m_length + 1];
        memcpy(m_data, str.m_data, m_length + 1);
    } else
        m_data = 0;

    return *this;
}

bool operator==(const CString& c1, const CString& c2)
{
    size_t len = c1.size();
    return len == c2.size() && (len == 0 || memcmp(c1.c_str(), c2.c_str(), len) == 0);
}

// These static strings are immutable, except for rc, whose initial value is chosen to 
// reduce the possibility of it becoming zero due to ref/deref not being thread-safe.
static UChar sharedEmptyChar;
UString::BaseString* UString::Rep::nullBaseString;
UString::BaseString* UString::Rep::emptyBaseString;
UString* UString::nullUString;

static void initializeStaticBaseString(UString::BaseString& base)
{
    base.rc = INT_MAX / 2;
    base.m_identifierTableAndFlags.setFlag(UString::Rep::StaticFlag);
    base.checkConsistency();
}

void initializeUString()
{
    UString::Rep::nullBaseString = new UString::BaseString(0, 0);
    initializeStaticBaseString(*UString::Rep::nullBaseString);

    UString::Rep::emptyBaseString = new UString::BaseString(&sharedEmptyChar, 0);
    initializeStaticBaseString(*UString::Rep::emptyBaseString);

    UString::nullUString = new UString;
}

static char* statBuffer = 0; // Only used for debugging via UString::ascii().

PassRefPtr<UString::Rep> UString::Rep::createCopying(const UChar* d, int l)
{
    UChar* copyD = static_cast<UChar*>(fastMalloc(l * sizeof(UChar)));
    copyChars(copyD, d, l);
    return create(copyD, l);
}

PassRefPtr<UString::Rep> UString::Rep::createFromUTF8(const char* string)
{
    if (!string)
        return &UString::Rep::null();

    size_t length = strlen(string);
    Vector<UChar, 1024> buffer(length);
    UChar* p = buffer.data();
    if (conversionOK != convertUTF8ToUTF16(&string, string + length, &p, p + length))
        return &UString::Rep::null();

    return UString::Rep::createCopying(buffer.data(), p - buffer.data());
}

PassRefPtr<UString::Rep> UString::Rep::create(UChar* string, int length, PassRefPtr<UString::SharedUChar> sharedBuffer)
{
    PassRefPtr<UString::Rep> rep = create(string, length);
    rep->baseString()->setSharedBuffer(sharedBuffer);
    rep->checkConsistency();
    return rep;
}

UString::SharedUChar* UString::Rep::sharedBuffer()
{
    UString::BaseString* base = baseString();
    if (len < minLengthToShare)
        return 0;

    return base->sharedBuffer();
}

void UString::Rep::destroy()
{
    checkConsistency();

    // Static null and empty strings can never be destroyed, but we cannot rely on 
    // reference counting, because ref/deref are not thread-safe.
    if (!isStatic()) {
        if (identifierTable())
            Identifier::remove(this);

        UString::BaseString* base = baseString();
        if (base == this) {
            if (m_sharedBuffer)
                m_sharedBuffer->deref();
            else
                fastFree(base->buf);
        } else
            base->deref();

        delete this;
    }
}

// Golden ratio - arbitrary start value to avoid mapping all 0's to all 0's
// or anything like that.
const unsigned PHI = 0x9e3779b9U;

// Paul Hsieh's SuperFastHash
// http://www.azillionmonkeys.com/qed/hash.html
unsigned UString::Rep::computeHash(const UChar* s, int len)
{
    unsigned l = len;
    uint32_t hash = PHI;
    uint32_t tmp;

    int rem = l & 1;
    l >>= 1;

    // Main loop
    for (; l > 0; l--) {
        hash += s[0];
        tmp = (s[1] << 11) ^ hash;
        hash = (hash << 16) ^ tmp;
        s += 2;
        hash += hash >> 11;
    }

    // Handle end case
    if (rem) {
        hash += s[0];
        hash ^= hash << 11;
        hash += hash >> 17;
    }

    // Force "avalanching" of final 127 bits
    hash ^= hash << 3;
    hash += hash >> 5;
    hash ^= hash << 2;
    hash += hash >> 15;
    hash ^= hash << 10;

    // this avoids ever returning a hash code of 0, since that is used to
    // signal "hash not computed yet", using a value that is likely to be
    // effectively the same as 0 when the low bits are masked
    if (hash == 0)
        hash = 0x80000000;

    return hash;
}

// Paul Hsieh's SuperFastHash
// http://www.azillionmonkeys.com/qed/hash.html
unsigned UString::Rep::computeHash(const char* s, int l)
{
    // This hash is designed to work on 16-bit chunks at a time. But since the normal case
    // (above) is to hash UTF-16 characters, we just treat the 8-bit chars as if they
    // were 16-bit chunks, which should give matching results

    uint32_t hash = PHI;
    uint32_t tmp;

    size_t rem = l & 1;
    l >>= 1;

    // Main loop
    for (; l > 0; l--) {
        hash += static_cast<unsigned char>(s[0]);
        tmp = (static_cast<unsigned char>(s[1]) << 11) ^ hash;
        hash = (hash << 16) ^ tmp;
        s += 2;
        hash += hash >> 11;
    }

    // Handle end case
    if (rem) {
        hash += static_cast<unsigned char>(s[0]);
        hash ^= hash << 11;
        hash += hash >> 17;
    }

    // Force "avalanching" of final 127 bits
    hash ^= hash << 3;
    hash += hash >> 5;
    hash ^= hash << 2;
    hash += hash >> 15;
    hash ^= hash << 10;

    // this avoids ever returning a hash code of 0, since that is used to
    // signal "hash not computed yet", using a value that is likely to be
    // effectively the same as 0 when the low bits are masked
    if (hash == 0)
        hash = 0x80000000;

    return hash;
}

#ifndef NDEBUG
void UString::Rep::checkConsistency() const
{
    const UString::BaseString* base = baseString();

    // There is no recursion for base strings.
    ASSERT(base == base->baseString());

    if (isStatic()) {
        // There are only two static strings: null and empty.
        ASSERT(!len);

        // Static strings cannot get in identifier tables, because they are globally shared.
        ASSERT(!identifierTable());
    }

    // The string fits in buffer.
    ASSERT(base->usedPreCapacity <= base->preCapacity);
    ASSERT(base->usedCapacity <= base->capacity);
    ASSERT(-offset <= base->usedPreCapacity);
    ASSERT(offset + len <= base->usedCapacity);
}
#endif

UString::SharedUChar* UString::BaseString::sharedBuffer()
{
    if (!m_sharedBuffer)
        setSharedBuffer(SharedUChar::create(new OwnFastMallocPtr<UChar>(buf)));
    return m_sharedBuffer;
}

void UString::BaseString::setSharedBuffer(PassRefPtr<UString::SharedUChar> sharedBuffer)
{
    // The manual steps below are because m_sharedBuffer can't be a RefPtr. m_sharedBuffer
    // is in a union with another variable to avoid making BaseString any larger.
    if (m_sharedBuffer)
        m_sharedBuffer->deref();
    m_sharedBuffer = sharedBuffer.releaseRef();
}

bool UString::BaseString::slowIsBufferReadOnly()
{
    // The buffer may not be modified as soon as the underlying data has been shared with another class.
    if (m_sharedBuffer->isShared())
        return true;

    // At this point, we know it that the underlying buffer isn't shared outside of this base class,
    // so get rid of m_sharedBuffer.
    OwnPtr<OwnFastMallocPtr<UChar> > mallocPtr(m_sharedBuffer->release());
    UChar* unsharedBuf = const_cast<UChar*>(mallocPtr->release());
    setSharedBuffer(0);
    preCapacity += (buf - unsharedBuf);
    buf = unsharedBuf;
    return false;
}

// Put these early so they can be inlined.
static inline size_t expandedSize(size_t capacitySize, size_t precapacitySize)
{
    // Combine capacitySize & precapacitySize to produce a single size to allocate,
    // check that doing so does not result in overflow.
    size_t size = capacitySize + precapacitySize;
    if (size < capacitySize)
        return overflowIndicator();

    // Small Strings (up to 4 pages):
    // Expand the allocation size to 112.5% of the amount requested.  This is largely sicking
    // to our previous policy, however 112.5% is cheaper to calculate.
    if (size < 0x4000) {
        size_t expandedSize = ((size + (size >> 3)) | 15) + 1;
        // Given the limited range within which we calculate the expansion in this
        // fashion the above calculation should never overflow.
        ASSERT(expandedSize >= size);
        ASSERT(expandedSize < maxUChars());
        return expandedSize;
    }

    // Medium Strings (up to 128 pages):
    // For pages covering multiple pages over-allocation is less of a concern - any unused
    // space will not be paged in if it is not used, so this is purely a VM overhead.  For
    // these strings allocate 2x the requested size.
    if (size < 0x80000) {
        size_t expandedSize = ((size + size) | 0xfff) + 1;
        // Given the limited range within which we calculate the expansion in this
        // fashion the above calculation should never overflow.
        ASSERT(expandedSize >= size);
        ASSERT(expandedSize < maxUChars());
        return expandedSize;
    }

    // Large Strings (to infinity and beyond!):
    // Revert to our 112.5% policy - probably best to limit the amount of unused VM we allow
    // any individual string be responsible for.
    size_t expandedSize = ((size + (size >> 3)) | 0xfff) + 1;

    // Check for overflow - any result that is at least as large as requested (but
    // still below the limit) is okay.
    if ((expandedSize >= size) && (expandedSize < maxUChars()))
        return expandedSize;
    return overflowIndicator();
}

static inline bool expandCapacity(UString::Rep* rep, int requiredLength)
{
    rep->checkConsistency();
    ASSERT(!rep->baseString()->isBufferReadOnly());

    UString::BaseString* base = rep->baseString();

    if (requiredLength > base->capacity) {
        size_t newCapacity = expandedSize(requiredLength, base->preCapacity);
        UChar* oldBuf = base->buf;
        base->buf = reallocChars(base->buf, newCapacity);
        if (!base->buf) {
            base->buf = oldBuf;
            return false;
        }
        base->capacity = newCapacity - base->preCapacity;
    }
    if (requiredLength > base->usedCapacity)
        base->usedCapacity = requiredLength;

    rep->checkConsistency();
    return true;
}

bool UString::Rep::reserveCapacity(int capacity)
{
    // If this is an empty string there is no point 'growing' it - just allocate a new one.
    // If the BaseString is shared with another string that is using more capacity than this
    // string is, then growing the buffer won't help.
    // If the BaseString's buffer is readonly, then it isn't allowed to grow.
    UString::BaseString* base = baseString();
    if (!base->buf || !base->capacity || (offset + len) != base->usedCapacity || base->isBufferReadOnly())
        return false;
    
    // If there is already sufficient capacity, no need to grow!
    if (capacity <= base->capacity)
        return true;

    checkConsistency();

    size_t newCapacity = expandedSize(capacity, base->preCapacity);
    UChar* oldBuf = base->buf;
    base->buf = reallocChars(base->buf, newCapacity);
    if (!base->buf) {
        base->buf = oldBuf;
        return false;
    }
    base->capacity = newCapacity - base->preCapacity;

    checkConsistency();
    return true;
}

void UString::expandCapacity(int requiredLength)
{
    if (!JSC::expandCapacity(m_rep.get(), requiredLength))
        makeNull();
}

void UString::expandPreCapacity(int requiredPreCap)
{
    m_rep->checkConsistency();
    ASSERT(!m_rep->baseString()->isBufferReadOnly());

    BaseString* base = m_rep->baseString();

    if (requiredPreCap > base->preCapacity) {
        size_t newCapacity = expandedSize(requiredPreCap, base->capacity);
        int delta = newCapacity - base->capacity - base->preCapacity;

        UChar* newBuf = allocChars(newCapacity);
        if (!newBuf) {
            makeNull();
            return;
        }
        copyChars(newBuf + delta, base->buf, base->capacity + base->preCapacity);
        fastFree(base->buf);
        base->buf = newBuf;

        base->preCapacity = newCapacity - base->capacity;
    }
    if (requiredPreCap > base->usedPreCapacity)
        base->usedPreCapacity = requiredPreCap;

    m_rep->checkConsistency();
}

static PassRefPtr<UString::Rep> createRep(const char* c)
{
    if (!c)
        return &UString::Rep::null();

    if (!c[0])
        return &UString::Rep::empty();

    size_t length = strlen(c);
    UChar* d = allocChars(length);
    if (!d)
        return &UString::Rep::null();
    else {
        for (size_t i = 0; i < length; i++)
            d[i] = static_cast<unsigned char>(c[i]); // use unsigned char to zero-extend instead of sign-extend
        return UString::Rep::create(d, static_cast<int>(length));
    }

}

UString::UString(const char* c)
    : m_rep(createRep(c))
{
}

UString::UString(const UChar* c, int length)
{
    if (length == 0) 
        m_rep = &Rep::empty();
    else
        m_rep = Rep::createCopying(c, length);
}

UString::UString(UChar* c, int length, bool copy)
{
    if (length == 0)
        m_rep = &Rep::empty();
    else if (copy)
        m_rep = Rep::createCopying(c, length);
    else
        m_rep = Rep::create(c, length);
}

UString::UString(const Vector<UChar>& buffer)
{
    if (!buffer.size())
        m_rep = &Rep::empty();
    else
        m_rep = Rep::createCopying(buffer.data(), buffer.size());
}

static ALWAYS_INLINE int newCapacityWithOverflowCheck(const int currentCapacity, const int extendLength, const bool plusOne = false)
{
    ASSERT_WITH_MESSAGE(extendLength >= 0, "extendedLength = %d", extendLength);

    const int plusLength = plusOne ? 1 : 0;
    if (currentCapacity > std::numeric_limits<int>::max() - extendLength - plusLength)
        CRASH();

    return currentCapacity + extendLength + plusLength;
}

static ALWAYS_INLINE PassRefPtr<UString::Rep> concatenate(PassRefPtr<UString::Rep> r, const UChar* tData, int tSize)
{
    RefPtr<UString::Rep> rep = r;

    rep->checkConsistency();

    int thisSize = rep->size();
    int thisOffset = rep->offset;
    int length = thisSize + tSize;
    UString::BaseString* base = rep->baseString();

    // possible cases:
    if (tSize == 0) {
        // t is empty
    } else if (thisSize == 0) {
        // this is empty
        rep = UString::Rep::createCopying(tData, tSize);
    } else if (rep == base && !base->isShared()) {
        // this is direct and has refcount of 1 (so we can just alter it directly)
        if (!expandCapacity(rep.get(), newCapacityWithOverflowCheck(thisOffset, length)))
            rep = &UString::Rep::null();
        if (rep->data()) {
            copyChars(rep->data() + thisSize, tData, tSize);
            rep->len = length;
            rep->_hash = 0;
        }
    } else if (thisOffset + thisSize == base->usedCapacity && thisSize >= minShareSize && !base->isBufferReadOnly()) {
        // this reaches the end of the buffer - extend it if it's long enough to append to
        if (!expandCapacity(rep.get(), newCapacityWithOverflowCheck(thisOffset, length)))
            rep = &UString::Rep::null();
        if (rep->data()) {
            copyChars(rep->data() + thisSize, tData, tSize);
            rep = UString::Rep::create(rep, 0, length);
        }
    } else {
        // This is shared in some way that prevents us from modifying base, so we must make a whole new string.
        size_t newCapacity = expandedSize(length, 0);
        UChar* d = allocChars(newCapacity);
        if (!d)
            rep = &UString::Rep::null();
        else {
            copyChars(d, rep->data(), thisSize);
            copyChars(d + thisSize, tData, tSize);
            rep = UString::Rep::create(d, length);
            rep->baseString()->capacity = newCapacity;
        }
    }

    rep->checkConsistency();

    return rep.release();
}

static ALWAYS_INLINE PassRefPtr<UString::Rep> concatenate(PassRefPtr<UString::Rep> r, const char* t)
{
    RefPtr<UString::Rep> rep = r;

    rep->checkConsistency();

    int thisSize = rep->size();
    int thisOffset = rep->offset;
    int tSize = static_cast<int>(strlen(t));
    int length = thisSize + tSize;
    UString::BaseString* base = rep->baseString();

    // possible cases:
    if (thisSize == 0) {
        // this is empty
        rep = createRep(t);
    } else if (tSize == 0) {
        // t is empty, we'll just return *this below.
    } else if (rep == base && !base->isShared()) {
        // this is direct and has refcount of 1 (so we can just alter it directly)
        expandCapacity(rep.get(), newCapacityWithOverflowCheck(thisOffset, length));
        UChar* d = rep->data();
        if (d) {
            for (int i = 0; i < tSize; ++i)
                d[thisSize + i] = static_cast<unsigned char>(t[i]); // use unsigned char to zero-extend instead of sign-extend
            rep->len = length;
            rep->_hash = 0;
        }
    } else if (thisOffset + thisSize == base->usedCapacity && thisSize >= minShareSize && !base->isBufferReadOnly()) {
        // this string reaches the end of the buffer - extend it
        expandCapacity(rep.get(), newCapacityWithOverflowCheck(thisOffset, length));
        UChar* d = rep->data();
        if (d) {
            for (int i = 0; i < tSize; ++i)
                d[thisSize + i] = static_cast<unsigned char>(t[i]); // use unsigned char to zero-extend instead of sign-extend
            rep = UString::Rep::create(rep, 0, length);
        }
    } else {
        // This is shared in some way that prevents us from modifying base, so we must make a whole new string.
        size_t newCapacity = expandedSize(length, 0);
        UChar* d = allocChars(newCapacity);
        if (!d)
            rep = &UString::Rep::null();
        else {
            copyChars(d, rep->data(), thisSize);
            for (int i = 0; i < tSize; ++i)
                d[thisSize + i] = static_cast<unsigned char>(t[i]); // use unsigned char to zero-extend instead of sign-extend
            rep = UString::Rep::create(d, length);
            rep->baseString()->capacity = newCapacity;
        }
    }

    rep->checkConsistency();

    return rep.release();
}

PassRefPtr<UString::Rep> concatenate(UString::Rep* a, UString::Rep* b)
{
    a->checkConsistency();
    b->checkConsistency();

    int aSize = a->size();
    int bSize = b->size();
    int aOffset = a->offset;

    // possible cases:

    UString::BaseString* aBase = a->baseString();
    if (bSize == 1 && aOffset + aSize == aBase->usedCapacity && aOffset + aSize < aBase->capacity && !aBase->isBufferReadOnly()) {
        // b is a single character (common fast case)
        ++aBase->usedCapacity;
        a->data()[aSize] = b->data()[0];
        return UString::Rep::create(a, 0, aSize + 1);
    }

    // a is empty
    if (aSize == 0)
        return b;
    // b is empty
    if (bSize == 0)
        return a;

    int bOffset = b->offset;
    int length = aSize + bSize;

    UString::BaseString* bBase = b->baseString();
    if (aOffset + aSize == aBase->usedCapacity && aSize >= minShareSize && 4 * aSize >= bSize
        && (-bOffset != bBase->usedPreCapacity || aSize >= bSize) && !aBase->isBufferReadOnly()) {
        // - a reaches the end of its buffer so it qualifies for shared append
        // - also, it's at least a quarter the length of b - appending to a much shorter
        //   string does more harm than good
        // - however, if b qualifies for prepend and is longer than a, we'd rather prepend
        
        UString x(a);
        x.expandCapacity(newCapacityWithOverflowCheck(aOffset, length));
        if (!a->data() || !x.data())
            return 0;
        copyChars(a->data() + aSize, b->data(), bSize);
        PassRefPtr<UString::Rep> result = UString::Rep::create(a, 0, length);

        a->checkConsistency();
        b->checkConsistency();
        result->checkConsistency();

        return result;
    }

    if (-bOffset == bBase->usedPreCapacity && bSize >= minShareSize && 4 * bSize >= aSize && !bBase->isBufferReadOnly()) {
        // - b reaches the beginning of its buffer so it qualifies for shared prepend
        // - also, it's at least a quarter the length of a - prepending to a much shorter
        //   string does more harm than good
        UString y(b);
        y.expandPreCapacity(-bOffset + aSize);
        if (!b->data() || !y.data())
            return 0;
        copyChars(b->data() - aSize, a->data(), aSize);
        PassRefPtr<UString::Rep> result = UString::Rep::create(b, -aSize, length);

        a->checkConsistency();
        b->checkConsistency();
        result->checkConsistency();

        return result;
    }

    // a does not qualify for append, and b does not qualify for prepend, gotta make a whole new string
    size_t newCapacity = expandedSize(length, 0);
    UChar* d = allocChars(newCapacity);
    if (!d)
        return 0;
    copyChars(d, a->data(), aSize);
    copyChars(d + aSize, b->data(), bSize);
    PassRefPtr<UString::Rep> result = UString::Rep::create(d, length);
    result->baseString()->capacity = newCapacity;

    a->checkConsistency();
    b->checkConsistency();
    result->checkConsistency();

    return result;
}

PassRefPtr<UString::Rep> concatenate(UString::Rep* rep, int i)
{
    UChar buf[1 + sizeof(i) * 3];
    UChar* end = buf + sizeof(buf) / sizeof(UChar);
    UChar* p = end;
  
    if (i == 0)
        *--p = '0';
    else if (i == INT_MIN) {
        char minBuf[1 + sizeof(i) * 3];
        sprintf(minBuf, "%d", INT_MIN);
        return concatenate(rep, minBuf);
    } else {
        bool negative = false;
        if (i < 0) {
            negative = true;
            i = -i;
        }
        while (i) {
            *--p = static_cast<unsigned short>((i % 10) + '0');
            i /= 10;
        }
        if (negative)
            *--p = '-';
    }

    return concatenate(rep, p, static_cast<int>(end - p));

}

PassRefPtr<UString::Rep> concatenate(UString::Rep* rep, double d)
{
    // avoid ever printing -NaN, in JS conceptually there is only one NaN value
    if (isnan(d))
        return concatenate(rep, "NaN");

    if (d == 0.0) // stringify -0 as 0
        d = 0.0;

    char buf[80];
    int decimalPoint;
    int sign;

    char result[80];
    WTF::dtoa(result, d, 0, &decimalPoint, &sign, NULL);
    int length = static_cast<int>(strlen(result));
  
    int i = 0;
    if (sign)
        buf[i++] = '-';
  
    if (decimalPoint <= 0 && decimalPoint > -6) {
        buf[i++] = '0';
        buf[i++] = '.';
        for (int j = decimalPoint; j < 0; j++)
            buf[i++] = '0';
        strcpy(buf + i, result);
    } else if (decimalPoint <= 21 && decimalPoint > 0) {
        if (length <= decimalPoint) {
            strcpy(buf + i, result);
            i += length;
            for (int j = 0; j < decimalPoint - length; j++)
                buf[i++] = '0';
            buf[i] = '\0';
        } else {
            strncpy(buf + i, result, decimalPoint);
            i += decimalPoint;
            buf[i++] = '.';
            strcpy(buf + i, result + decimalPoint);
        }
    } else if (result[0] < '0' || result[0] > '9')
        strcpy(buf + i, result);
    else {
        buf[i++] = result[0];
        if (length > 1) {
            buf[i++] = '.';
            strcpy(buf + i, result + 1);
            i += length - 1;
        }
        
        buf[i++] = 'e';
        buf[i++] = (decimalPoint >= 0) ? '+' : '-';
        // decimalPoint can't be more than 3 digits decimal given the
        // nature of float representation
        int exponential = decimalPoint - 1;
        if (exponential < 0)
            exponential = -exponential;
        if (exponential >= 100)
            buf[i++] = static_cast<char>('0' + exponential / 100);
        if (exponential >= 10)
            buf[i++] = static_cast<char>('0' + (exponential % 100) / 10);
        buf[i++] = static_cast<char>('0' + exponential % 10);
        buf[i++] = '\0';
    }
    
    return concatenate(rep, buf);
}

UString UString::from(int i)
{
    UChar buf[1 + sizeof(i) * 3];
    UChar* end = buf + sizeof(buf) / sizeof(UChar);
    UChar* p = end;
  
    if (i == 0)
        *--p = '0';
    else if (i == INT_MIN) {
        char minBuf[1 + sizeof(i) * 3];
        sprintf(minBuf, "%d", INT_MIN);
        return UString(minBuf);
    } else {
        bool negative = false;
        if (i < 0) {
            negative = true;
            i = -i;
        }
        while (i) {
            *--p = static_cast<unsigned short>((i % 10) + '0');
            i /= 10;
        }
        if (negative)
            *--p = '-';
    }

    return UString(p, static_cast<int>(end - p));
}

UString UString::from(unsigned int u)
{
    UChar buf[sizeof(u) * 3];
    UChar* end = buf + sizeof(buf) / sizeof(UChar);
    UChar* p = end;
    
    if (u == 0)
        *--p = '0';
    else {
        while (u) {
            *--p = static_cast<unsigned short>((u % 10) + '0');
            u /= 10;
        }
    }
    
    return UString(p, static_cast<int>(end - p));
}

UString UString::from(long l)
{
    UChar buf[1 + sizeof(l) * 3];
    UChar* end = buf + sizeof(buf) / sizeof(UChar);
    UChar* p = end;

    if (l == 0)
        *--p = '0';
    else if (l == LONG_MIN) {
        char minBuf[1 + sizeof(l) * 3];
        sprintf(minBuf, "%ld", LONG_MIN);
        return UString(minBuf);
    } else {
        bool negative = false;
        if (l < 0) {
            negative = true;
            l = -l;
        }
        while (l) {
            *--p = static_cast<unsigned short>((l % 10) + '0');
            l /= 10;
        }
        if (negative)
            *--p = '-';
    }

    return UString(p, static_cast<int>(end - p));
}

UString UString::from(double d)
{
    // avoid ever printing -NaN, in JS conceptually there is only one NaN value
    if (isnan(d))
        return "NaN";

    char buf[80];
    int decimalPoint;
    int sign;
    
    char result[80];
    WTF::dtoa(result, d, 0, &decimalPoint, &sign, NULL);
    int length = static_cast<int>(strlen(result));
  
    int i = 0;
    if (sign)
        buf[i++] = '-';
  
    if (decimalPoint <= 0 && decimalPoint > -6) {
        buf[i++] = '0';
        buf[i++] = '.';
        for (int j = decimalPoint; j < 0; j++)
            buf[i++] = '0';
        strcpy(buf + i, result);
    } else if (decimalPoint <= 21 && decimalPoint > 0) {
        if (length <= decimalPoint) {
            strcpy(buf + i, result);
            i += length;
            for (int j = 0; j < decimalPoint - length; j++)
                buf[i++] = '0';
            buf[i] = '\0';
        } else {
            strncpy(buf + i, result, decimalPoint);
            i += decimalPoint;
            buf[i++] = '.';
            strcpy(buf + i, result + decimalPoint);
        }
    } else if (result[0] < '0' || result[0] > '9')
        strcpy(buf + i, result);
    else {
        buf[i++] = result[0];
        if (length > 1) {
            buf[i++] = '.';
            strcpy(buf + i, result + 1);
            i += length - 1;
        }
        
        buf[i++] = 'e';
        buf[i++] = (decimalPoint >= 0) ? '+' : '-';
        // decimalPoint can't be more than 3 digits decimal given the
        // nature of float representation
        int exponential = decimalPoint - 1;
        if (exponential < 0)
            exponential = -exponential;
        if (exponential >= 100)
            buf[i++] = static_cast<char>('0' + exponential / 100);
        if (exponential >= 10)
            buf[i++] = static_cast<char>('0' + (exponential % 100) / 10);
        buf[i++] = static_cast<char>('0' + exponential % 10);
        buf[i++] = '\0';
    }
    
    return UString(buf);
}

UString UString::spliceSubstringsWithSeparators(const Range* substringRanges, int rangeCount, const UString* separators, int separatorCount) const
{
    m_rep->checkConsistency();

    if (rangeCount == 1 && separatorCount == 0) {
        int thisSize = size();
        int position = substringRanges[0].position;
        int length = substringRanges[0].length;
        if (position <= 0 && length >= thisSize)
            return *this;
        return UString::Rep::create(m_rep, max(0, position), min(thisSize, length));
    }

    int totalLength = 0;
    for (int i = 0; i < rangeCount; i++)
        totalLength += substringRanges[i].length;
    for (int i = 0; i < separatorCount; i++)
        totalLength += separators[i].size();

    if (totalLength == 0)
        return "";

    UChar* buffer = allocChars(totalLength);
    if (!buffer)
        return null();

    int maxCount = max(rangeCount, separatorCount);
    int bufferPos = 0;
    for (int i = 0; i < maxCount; i++) {
        if (i < rangeCount) {
            copyChars(buffer + bufferPos, data() + substringRanges[i].position, substringRanges[i].length);
            bufferPos += substringRanges[i].length;
        }
        if (i < separatorCount) {
            copyChars(buffer + bufferPos, separators[i].data(), separators[i].size());
            bufferPos += separators[i].size();
        }
    }

    return UString::Rep::create(buffer, totalLength);
}

UString UString::replaceRange(int rangeStart, int rangeLength, const UString& replacement) const
{
    m_rep->checkConsistency();

    int replacementLength = replacement.size();
    int totalLength = size() - rangeLength + replacementLength;
    if (totalLength == 0)
        return "";

    UChar* buffer = allocChars(totalLength);
    if (!buffer)
        return null();

    copyChars(buffer, data(), rangeStart);
    copyChars(buffer + rangeStart, replacement.data(), replacementLength);
    int rangeEnd = rangeStart + rangeLength;
    copyChars(buffer + rangeStart + replacementLength, data() + rangeEnd, size() - rangeEnd);

    return UString::Rep::create(buffer, totalLength);
}


UString& UString::append(const UString &t)
{
    m_rep->checkConsistency();
    t.rep()->checkConsistency();

    int thisSize = size();
    int thisOffset = m_rep->offset;
    int tSize = t.size();
    int length = thisSize + tSize;
    BaseString* base = m_rep->baseString();

    // possible cases:
    if (thisSize == 0) {
        // this is empty
        *this = t;
    } else if (tSize == 0) {
        // t is empty
    } else if (m_rep == base && !base->isShared()) {
        // this is direct and has refcount of 1 (so we can just alter it directly)
        expandCapacity(newCapacityWithOverflowCheck(thisOffset, length));
        if (data()) {
            copyChars(m_rep->data() + thisSize, t.data(), tSize);
            m_rep->len = length;
            m_rep->_hash = 0;
        }
    } else if (thisOffset + thisSize == base->usedCapacity && thisSize >= minShareSize && !base->isBufferReadOnly()) {
        // this reaches the end of the buffer - extend it if it's long enough to append to
        expandCapacity(newCapacityWithOverflowCheck(thisOffset, length));
        if (data()) {
            copyChars(m_rep->data() + thisSize, t.data(), tSize);
            m_rep = Rep::create(m_rep, 0, length);
        }
    } else {
        // This is shared in some way that prevents us from modifying base, so we must make a whole new string.
        size_t newCapacity = expandedSize(length, 0);
        UChar* d = allocChars(newCapacity);
        if (!d)
            makeNull();
        else {
            copyChars(d, data(), thisSize);
            copyChars(d + thisSize, t.data(), tSize);
            m_rep = Rep::create(d, length);
            m_rep->baseString()->capacity = newCapacity;
        }
    }

    m_rep->checkConsistency();
    t.rep()->checkConsistency();

    return *this;
}

UString& UString::append(const UChar* tData, int tSize)
{
    m_rep = concatenate(m_rep.release(), tData, tSize);
    return *this;
}

UString& UString::appendNumeric(int i)
{
    m_rep = concatenate(rep(), i);
    return *this;
}

UString& UString::appendNumeric(double d)
{
    m_rep = concatenate(rep(), d);
    return *this;
}

UString& UString::append(const char* t)
{
    m_rep = concatenate(m_rep.release(), t);
    return *this;
}

UString& UString::append(UChar c)
{
    m_rep->checkConsistency();

    int thisOffset = m_rep->offset;
    int length = size();
    BaseString* base = m_rep->baseString();

    // possible cases:
    if (length == 0) {
        // this is empty - must make a new m_rep because we don't want to pollute the shared empty one 
        size_t newCapacity = expandedSize(1, 0);
        UChar* d = allocChars(newCapacity);
        if (!d)
            makeNull();
        else {
            d[0] = c;
            m_rep = Rep::create(d, 1);
            m_rep->baseString()->capacity = newCapacity;
        }
    } else if (m_rep == base && !base->isShared()) {
        // this is direct and has refcount of 1 (so we can just alter it directly)
        expandCapacity(newCapacityWithOverflowCheck(thisOffset, length, true));
        UChar* d = m_rep->data();
        if (d) {
            d[length] = c;
            m_rep->len = length + 1;
            m_rep->_hash = 0;
        }
    } else if (thisOffset + length == base->usedCapacity && length >= minShareSize && !base->isBufferReadOnly()) {
        // this reaches the end of the string - extend it and share
        expandCapacity(newCapacityWithOverflowCheck(thisOffset, length, true));
        UChar* d = m_rep->data();
        if (d) {
            d[length] = c;
            m_rep = Rep::create(m_rep, 0, length + 1);
        }
    } else {
        // This is shared in some way that prevents us from modifying base, so we must make a whole new string.
        size_t newCapacity = expandedSize(length + 1, 0);
        UChar* d = allocChars(newCapacity);
        if (!d)
            makeNull();
        else {
            copyChars(d, data(), length);
            d[length] = c;
            m_rep = Rep::create(d, length + 1);
            m_rep->baseString()->capacity = newCapacity;
        }
    }

    m_rep->checkConsistency();

    return *this;
}

bool UString::getCString(CStringBuffer& buffer) const
{
    int length = size();
    int neededSize = length + 1;
    buffer.resize(neededSize);
    char* buf = buffer.data();

    UChar ored = 0;
    const UChar* p = data();
    char* q = buf;
    const UChar* limit = p + length;
    while (p != limit) {
        UChar c = p[0];
        ored |= c;
        *q = static_cast<char>(c);
        ++p;
        ++q;
    }
    *q = '\0';

    return !(ored & 0xFF00);
}

char* UString::ascii() const
{
    int length = size();
    int neededSize = length + 1;
    delete[] statBuffer;
    statBuffer = new char[neededSize];

    const UChar* p = data();
    char* q = statBuffer;
    const UChar* limit = p + length;
    while (p != limit) {
        *q = static_cast<char>(p[0]);
        ++p;
        ++q;
    }
    *q = '\0';

    return statBuffer;
}

UString& UString::operator=(const char* c)
{
    if (!c) {
        m_rep = &Rep::null();
        return *this;
    }

    if (!c[0]) {
        m_rep = &Rep::empty();
        return *this;
    }

    int l = static_cast<int>(strlen(c));
    UChar* d;
    BaseString* base = m_rep->baseString();
    if (!base->isShared() && l <= base->capacity && m_rep == base && m_rep->offset == 0 && base->preCapacity == 0) {
        d = base->buf;
        m_rep->_hash = 0;
        m_rep->len = l;
    } else {
        d = allocChars(l);
        if (!d) {
            makeNull();
            return *this;
        }
        m_rep = Rep::create(d, l);
    }
    for (int i = 0; i < l; i++)
        d[i] = static_cast<unsigned char>(c[i]); // use unsigned char to zero-extend instead of sign-extend

    return *this;
}

bool UString::is8Bit() const
{
    const UChar* u = data();
    const UChar* limit = u + size();
    while (u < limit) {
        if (u[0] > 0xFF)
            return false;
        ++u;
    }

    return true;
}

UChar UString::operator[](int pos) const
{
    if (pos >= size())
        return '\0';
    return data()[pos];
}

double UString::toDouble(bool tolerateTrailingJunk, bool tolerateEmptyString) const
{
    if (size() == 1) {
        UChar c = data()[0];
        if (isASCIIDigit(c))
            return c - '0';
        if (isASCIISpace(c) && tolerateEmptyString)
            return 0;
        return NaN;
    }

    // FIXME: If tolerateTrailingJunk is true, then we want to tolerate non-8-bit junk
    // after the number, so this is too strict a check.
    CStringBuffer s;
    if (!getCString(s))
        return NaN;
    const char* c = s.data();

    // skip leading white space
    while (isASCIISpace(*c))
        c++;

    // empty string ?
    if (*c == '\0')
        return tolerateEmptyString ? 0.0 : NaN;

    double d;

    // hex number ?
    if (*c == '0' && (*(c + 1) == 'x' || *(c + 1) == 'X')) {
        const char* firstDigitPosition = c + 2;
        c++;
        d = 0.0;
        while (*(++c)) {
            if (*c >= '0' && *c <= '9')
                d = d * 16.0 + *c - '0';
            else if ((*c >= 'A' && *c <= 'F') || (*c >= 'a' && *c <= 'f'))
                d = d * 16.0 + (*c & 0xdf) - 'A' + 10.0;
            else
                break;
        }

        if (d >= mantissaOverflowLowerBound)
            d = parseIntOverflow(firstDigitPosition, c - firstDigitPosition, 16);
    } else {
        // regular number ?
        char* end;
        d = WTF::strtod(c, &end);
        if ((d != 0.0 || end != c) && d != Inf && d != -Inf) {
            c = end;
        } else {
            double sign = 1.0;

            if (*c == '+')
                c++;
            else if (*c == '-') {
                sign = -1.0;
                c++;
            }

            // We used strtod() to do the conversion. However, strtod() handles
            // infinite values slightly differently than JavaScript in that it
            // converts the string "inf" with any capitalization to infinity,
            // whereas the ECMA spec requires that it be converted to NaN.

            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') {
                d = sign * Inf;
                c += 8;
            } else if ((d == Inf || d == -Inf) && *c != 'I' && *c != 'i')
                c = end;
            else
                return NaN;
        }
    }

    // allow trailing white space
    while (isASCIISpace(*c))
        c++;
    // don't allow anything after - unless tolerant=true
    if (!tolerateTrailingJunk && *c != '\0')
        d = NaN;

    return d;
}

double UString::toDouble(bool tolerateTrailingJunk) const
{
    return toDouble(tolerateTrailingJunk, true);
}

double UString::toDouble() const
{
    return toDouble(false, true);
}

uint32_t UString::toUInt32(bool* ok) const
{
    double d = toDouble();
    bool b = true;

    if (d != static_cast<uint32_t>(d)) {
        b = false;
        d = 0;
    }

    if (ok)
        *ok = b;

    return static_cast<uint32_t>(d);
}

uint32_t UString::toUInt32(bool* ok, bool tolerateEmptyString) const
{
    double d = toDouble(false, tolerateEmptyString);
    bool b = true;

    if (d != static_cast<uint32_t>(d)) {
        b = false;
        d = 0;
    }

    if (ok)
        *ok = b;

    return static_cast<uint32_t>(d);
}

uint32_t UString::toStrictUInt32(bool* ok) const
{
    if (ok)
        *ok = false;

    // Empty string is not OK.
    int len = m_rep->len;
    if (len == 0)
        return 0;
    const UChar* p = m_rep->data();
    unsigned short c = p[0];

    // If the first digit is 0, only 0 itself is OK.
    if (c == '0') {
        if (len == 1 && ok)
            *ok = true;
        return 0;
    }

    // Convert to UInt32, checking for overflow.
    uint32_t i = 0;
    while (1) {
        // Process character, turning it into a digit.
        if (c < '0' || c > '9')
            return 0;
        const unsigned d = c - '0';

        // Multiply by 10, checking for overflow out of 32 bits.
        if (i > 0xFFFFFFFFU / 10)
            return 0;
        i *= 10;

        // Add in the digit, checking for overflow out of 32 bits.
        const unsigned max = 0xFFFFFFFFU - d;
        if (i > max)
            return 0;
        i += d;

        // Handle end of string.
        if (--len == 0) {
            if (ok)
                *ok = true;
            return i;
        }

        // Get next character.
        c = *(++p);
    }
}

int UString::find(const UString& f, int pos) const
{
    int fsz = f.size();

    if (pos < 0)
        pos = 0;

    if (fsz == 1) {
        UChar ch = f[0];
        const UChar* end = data() + size();
        for (const UChar* c = data() + pos; c < end; c++) {
            if (*c == ch)
                return static_cast<int>(c - data());
        }
        return -1;
    }

    int sz = size();
    if (sz < fsz)
        return -1;
    if (fsz == 0)
        return pos;
    const UChar* end = data() + sz - fsz;
    int fsizeminusone = (fsz - 1) * sizeof(UChar);
    const UChar* fdata = f.data();
    unsigned short fchar = fdata[0];
    ++fdata;
    for (const UChar* c = data() + pos; c <= end; c++) {
        if (c[0] == fchar && !memcmp(c + 1, fdata, fsizeminusone))
            return static_cast<int>(c - data());
    }

    return -1;
}

int UString::find(UChar ch, int pos) const
{
    if (pos < 0)
        pos = 0;
    const UChar* end = data() + size();
    for (const UChar* c = data() + pos; c < end; c++) {
        if (*c == ch)
            return static_cast<int>(c - data());
    }
    
    return -1;
}

int UString::rfind(const UString& f, int pos) const
{
    int sz = size();
    int fsz = f.size();
    if (sz < fsz)
        return -1;
    if (pos < 0)
        pos = 0;
    if (pos > sz - fsz)
        pos = sz - fsz;
    if (fsz == 0)
        return pos;
    int fsizeminusone = (fsz - 1) * sizeof(UChar);
    const UChar* fdata = f.data();
    for (const UChar* c = data() + pos; c >= data(); c--) {
        if (*c == *fdata && !memcmp(c + 1, fdata + 1, fsizeminusone))
            return static_cast<int>(c - data());
    }

    return -1;
}

int UString::rfind(UChar ch, int pos) const
{
    if (isEmpty())
        return -1;
    if (pos + 1 >= size())
        pos = size() - 1;
    for (const UChar* c = data() + pos; c >= data(); c--) {
        if (*c == ch)
            return static_cast<int>(c - data());
    }

    return -1;
}

UString UString::substr(int pos, int len) const
{
    int s = size();

    if (pos < 0)
        pos = 0;
    else if (pos >= s)
        pos = s;
    if (len < 0)
        len = s;
    if (pos + len >= s)
        len = s - pos;

    if (pos == 0 && len == s)
        return *this;

    return UString(Rep::create(m_rep, pos, len));
}

bool operator==(const UString& s1, const char *s2)
{
    if (s2 == 0)
        return s1.isEmpty();

    const UChar* u = s1.data();
    const UChar* uend = u + s1.size();
    while (u != uend && *s2) {
        if (u[0] != (unsigned char)*s2)
            return false;
        s2++;
        u++;
    }

    return u == uend && *s2 == 0;
}

bool operator<(const UString& s1, const UString& s2)
{
    const int l1 = s1.size();
    const int l2 = s2.size();
    const int lmin = l1 < l2 ? l1 : l2;
    const UChar* c1 = s1.data();
    const UChar* c2 = s2.data();
    int l = 0;
    while (l < lmin && *c1 == *c2) {
        c1++;
        c2++;
        l++;
    }
    if (l < lmin)
        return (c1[0] < c2[0]);

    return (l1 < l2);
}

bool operator>(const UString& s1, const UString& s2)
{
    const int l1 = s1.size();
    const int l2 = s2.size();
    const int lmin = l1 < l2 ? l1 : l2;
    const UChar* c1 = s1.data();
    const UChar* c2 = s2.data();
    int l = 0;
    while (l < lmin && *c1 == *c2) {
        c1++;
        c2++;
        l++;
    }
    if (l < lmin)
        return (c1[0] > c2[0]);

    return (l1 > l2);
}

int compare(const UString& s1, const UString& s2)
{
    const int l1 = s1.size();
    const int l2 = s2.size();
    const int lmin = l1 < l2 ? l1 : l2;
    const UChar* c1 = s1.data();
    const UChar* c2 = s2.data();
    int l = 0;
    while (l < lmin && *c1 == *c2) {
        c1++;
        c2++;
        l++;
    }

    if (l < lmin)
        return (c1[0] > c2[0]) ? 1 : -1;

    if (l1 == l2)
        return 0;

    return (l1 > l2) ? 1 : -1;
}

bool equal(const UString::Rep* r, const UString::Rep* b)
{
    int length = r->len;
    if (length != b->len)
        return false;
    const UChar* d = r->data();
    const UChar* s = b->data();
    for (int i = 0; i != length; ++i) {
        if (d[i] != s[i])
            return false;
    }
    return true;
}

CString UString::UTF8String(bool strict) const
{
    // Allocate a buffer big enough to hold all the characters.
    const int length = size();
    Vector<char, 1024> buffer(length * 3);

    // Convert to runs of 8-bit characters.
    char* p = buffer.data();
    const UChar* d = reinterpret_cast<const UChar*>(&data()[0]);
    ConversionResult result = convertUTF16ToUTF8(&d, d + length, &p, p + buffer.size(), strict);
    if (result != conversionOK)
        return CString();

    return CString(buffer.data(), p - buffer.data());
}

// For use in error handling code paths -- having this not be inlined helps avoid PIC branches to fetch the global on Mac OS X.
NEVER_INLINE void UString::makeNull()
{
    m_rep = &Rep::null();
}

// For use in error handling code paths -- having this not be inlined helps avoid PIC branches to fetch the global on Mac OS X.
NEVER_INLINE UString::Rep* UString::nullRep()
{
    return &Rep::null();
}

} // namespace JSC