author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 06 Jul 2010 15:10:48 +0300 | |
changeset 30 | 5dc02b23752f |
parent 0 | 1918ee327afb |
permissions | -rw-r--r-- |
0 | 1 |
/* |
2 |
* Copyright (C) 1999-2000 Harri Porten (porten@kde.org) |
|
3 |
* Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved. |
|
4 |
* Copyright (C) 2007 Cameron Zwarich (cwzwarich@uwaterloo.ca) |
|
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 <limits.h> |
|
34 |
#include <limits> |
|
35 |
#include <math.h> |
|
36 |
#include <stdio.h> |
|
37 |
#include <stdlib.h> |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
38 |
#include <string.h> |
0 | 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(STRINGS_H) |
|
48 |
#include <strings.h> |
|
49 |
#endif |
|
50 |
||
51 |
using namespace WTF; |
|
52 |
using namespace WTF::Unicode; |
|
53 |
using namespace std; |
|
54 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
55 |
namespace JSC { |
0 | 56 |
|
57 |
extern const double NaN; |
|
58 |
extern const double Inf; |
|
59 |
||
60 |
CString::CString(const char* c) |
|
61 |
: m_length(strlen(c)) |
|
62 |
, m_data(new char[m_length + 1]) |
|
63 |
{ |
|
64 |
memcpy(m_data, c, m_length + 1); |
|
65 |
} |
|
66 |
||
67 |
CString::CString(const char* c, size_t length) |
|
68 |
: m_length(length) |
|
69 |
, m_data(new char[length + 1]) |
|
70 |
{ |
|
71 |
memcpy(m_data, c, m_length); |
|
72 |
m_data[m_length] = 0; |
|
73 |
} |
|
74 |
||
75 |
CString::CString(const CString& b) |
|
76 |
{ |
|
77 |
m_length = b.m_length; |
|
78 |
if (b.m_data) { |
|
79 |
m_data = new char[m_length + 1]; |
|
80 |
memcpy(m_data, b.m_data, m_length + 1); |
|
81 |
} else |
|
82 |
m_data = 0; |
|
83 |
} |
|
84 |
||
85 |
CString::~CString() |
|
86 |
{ |
|
87 |
delete [] m_data; |
|
88 |
} |
|
89 |
||
90 |
CString CString::adopt(char* c, size_t length) |
|
91 |
{ |
|
92 |
CString s; |
|
93 |
s.m_data = c; |
|
94 |
s.m_length = length; |
|
95 |
return s; |
|
96 |
} |
|
97 |
||
98 |
CString& CString::append(const CString& t) |
|
99 |
{ |
|
100 |
char* n; |
|
101 |
n = new char[m_length + t.m_length + 1]; |
|
102 |
if (m_length) |
|
103 |
memcpy(n, m_data, m_length); |
|
104 |
if (t.m_length) |
|
105 |
memcpy(n + m_length, t.m_data, t.m_length); |
|
106 |
m_length += t.m_length; |
|
107 |
n[m_length] = 0; |
|
108 |
||
109 |
delete [] m_data; |
|
110 |
m_data = n; |
|
111 |
||
112 |
return *this; |
|
113 |
} |
|
114 |
||
115 |
CString& CString::operator=(const char* c) |
|
116 |
{ |
|
117 |
if (m_data) |
|
118 |
delete [] m_data; |
|
119 |
m_length = strlen(c); |
|
120 |
m_data = new char[m_length + 1]; |
|
121 |
memcpy(m_data, c, m_length + 1); |
|
122 |
||
123 |
return *this; |
|
124 |
} |
|
125 |
||
126 |
CString& CString::operator=(const CString& str) |
|
127 |
{ |
|
128 |
if (this == &str) |
|
129 |
return *this; |
|
130 |
||
131 |
if (m_data) |
|
132 |
delete [] m_data; |
|
133 |
m_length = str.m_length; |
|
134 |
if (str.m_data) { |
|
135 |
m_data = new char[m_length + 1]; |
|
136 |
memcpy(m_data, str.m_data, m_length + 1); |
|
137 |
} else |
|
138 |
m_data = 0; |
|
139 |
||
140 |
return *this; |
|
141 |
} |
|
142 |
||
143 |
bool operator==(const CString& c1, const CString& c2) |
|
144 |
{ |
|
145 |
size_t len = c1.size(); |
|
146 |
return len == c2.size() && (len == 0 || memcmp(c1.c_str(), c2.c_str(), len) == 0); |
|
147 |
} |
|
148 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
149 |
// The null string is immutable, except for refCount. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
150 |
UString::Rep* UString::s_nullRep; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
151 |
UString* UString::s_nullUString; |
0 | 152 |
|
153 |
void initializeUString() |
|
154 |
{ |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
155 |
// UStringImpl::empty() does not construct its static string in a threadsafe fashion, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
156 |
// so ensure it has been initialized from here. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
157 |
UStringImpl::empty(); |
0 | 158 |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
159 |
UString::s_nullRep = new UStringImpl(0, 0, UStringImpl::ConstructStaticString); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
160 |
UString::s_nullUString = new UString; |
0 | 161 |
} |
162 |
||
163 |
UString::UString(const char* c) |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
164 |
: m_rep(Rep::create(c)) |
0 | 165 |
{ |
166 |
} |
|
167 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
168 |
UString::UString(const char* c, unsigned length) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
169 |
: m_rep(Rep::create(c, length)) |
0 | 170 |
{ |
171 |
} |
|
172 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
173 |
UString::UString(const UChar* c, unsigned length) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
174 |
: m_rep(Rep::create(c, length)) |
0 | 175 |
{ |
176 |
} |
|
177 |
||
178 |
UString UString::from(int i) |
|
179 |
{ |
|
180 |
UChar buf[1 + sizeof(i) * 3]; |
|
181 |
UChar* end = buf + sizeof(buf) / sizeof(UChar); |
|
182 |
UChar* p = end; |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
183 |
|
0 | 184 |
if (i == 0) |
185 |
*--p = '0'; |
|
186 |
else if (i == INT_MIN) { |
|
187 |
char minBuf[1 + sizeof(i) * 3]; |
|
188 |
sprintf(minBuf, "%d", INT_MIN); |
|
189 |
return UString(minBuf); |
|
190 |
} else { |
|
191 |
bool negative = false; |
|
192 |
if (i < 0) { |
|
193 |
negative = true; |
|
194 |
i = -i; |
|
195 |
} |
|
196 |
while (i) { |
|
197 |
*--p = static_cast<unsigned short>((i % 10) + '0'); |
|
198 |
i /= 10; |
|
199 |
} |
|
200 |
if (negative) |
|
201 |
*--p = '-'; |
|
202 |
} |
|
203 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
204 |
return UString(p, static_cast<unsigned>(end - p)); |
0 | 205 |
} |
206 |
||
207 |
UString UString::from(long long i) |
|
208 |
{ |
|
209 |
UChar buf[1 + sizeof(i) * 3]; |
|
210 |
UChar* end = buf + sizeof(buf) / sizeof(UChar); |
|
211 |
UChar* p = end; |
|
212 |
||
213 |
if (i == 0) |
|
214 |
*--p = '0'; |
|
215 |
else if (i == std::numeric_limits<long long>::min()) { |
|
216 |
char minBuf[1 + sizeof(i) * 3]; |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
217 |
#if OS(WINDOWS) |
0 | 218 |
snprintf(minBuf, sizeof(minBuf) - 1, "%I64d", std::numeric_limits<long long>::min()); |
219 |
#else |
|
220 |
snprintf(minBuf, sizeof(minBuf) - 1, "%lld", std::numeric_limits<long long>::min()); |
|
221 |
#endif |
|
222 |
return UString(minBuf); |
|
223 |
} else { |
|
224 |
bool negative = false; |
|
225 |
if (i < 0) { |
|
226 |
negative = true; |
|
227 |
i = -i; |
|
228 |
} |
|
229 |
while (i) { |
|
230 |
*--p = static_cast<unsigned short>((i % 10) + '0'); |
|
231 |
i /= 10; |
|
232 |
} |
|
233 |
if (negative) |
|
234 |
*--p = '-'; |
|
235 |
} |
|
236 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
237 |
return UString(p, static_cast<unsigned>(end - p)); |
0 | 238 |
} |
239 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
240 |
UString UString::from(unsigned u) |
0 | 241 |
{ |
242 |
UChar buf[sizeof(u) * 3]; |
|
243 |
UChar* end = buf + sizeof(buf) / sizeof(UChar); |
|
244 |
UChar* p = end; |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
245 |
|
0 | 246 |
if (u == 0) |
247 |
*--p = '0'; |
|
248 |
else { |
|
249 |
while (u) { |
|
250 |
*--p = static_cast<unsigned short>((u % 10) + '0'); |
|
251 |
u /= 10; |
|
252 |
} |
|
253 |
} |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
254 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
255 |
return UString(p, static_cast<unsigned>(end - p)); |
0 | 256 |
} |
257 |
||
258 |
UString UString::from(long l) |
|
259 |
{ |
|
260 |
UChar buf[1 + sizeof(l) * 3]; |
|
261 |
UChar* end = buf + sizeof(buf) / sizeof(UChar); |
|
262 |
UChar* p = end; |
|
263 |
||
264 |
if (l == 0) |
|
265 |
*--p = '0'; |
|
266 |
else if (l == LONG_MIN) { |
|
267 |
char minBuf[1 + sizeof(l) * 3]; |
|
268 |
sprintf(minBuf, "%ld", LONG_MIN); |
|
269 |
return UString(minBuf); |
|
270 |
} else { |
|
271 |
bool negative = false; |
|
272 |
if (l < 0) { |
|
273 |
negative = true; |
|
274 |
l = -l; |
|
275 |
} |
|
276 |
while (l) { |
|
277 |
*--p = static_cast<unsigned short>((l % 10) + '0'); |
|
278 |
l /= 10; |
|
279 |
} |
|
280 |
if (negative) |
|
281 |
*--p = '-'; |
|
282 |
} |
|
283 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
284 |
return UString(p, end - p); |
0 | 285 |
} |
286 |
||
287 |
UString UString::from(double d) |
|
288 |
{ |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
289 |
DtoaBuffer buffer; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
290 |
unsigned length; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
291 |
doubleToStringInJavaScriptFormat(d, buffer, &length); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
292 |
return UString(buffer, length); |
0 | 293 |
} |
294 |
||
295 |
bool UString::getCString(CStringBuffer& buffer) const |
|
296 |
{ |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
297 |
unsigned length = size(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
298 |
unsigned neededSize = length + 1; |
0 | 299 |
buffer.resize(neededSize); |
300 |
char* buf = buffer.data(); |
|
301 |
||
302 |
UChar ored = 0; |
|
303 |
const UChar* p = data(); |
|
304 |
char* q = buf; |
|
305 |
const UChar* limit = p + length; |
|
306 |
while (p != limit) { |
|
307 |
UChar c = p[0]; |
|
308 |
ored |= c; |
|
309 |
*q = static_cast<char>(c); |
|
310 |
++p; |
|
311 |
++q; |
|
312 |
} |
|
313 |
*q = '\0'; |
|
314 |
||
315 |
return !(ored & 0xFF00); |
|
316 |
} |
|
317 |
||
318 |
char* UString::ascii() const |
|
319 |
{ |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
320 |
static char* asciiBuffer = 0; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
321 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
322 |
unsigned length = size(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
323 |
unsigned neededSize = length + 1; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
324 |
delete[] asciiBuffer; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
325 |
asciiBuffer = new char[neededSize]; |
0 | 326 |
|
327 |
const UChar* p = data(); |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
328 |
char* q = asciiBuffer; |
0 | 329 |
const UChar* limit = p + length; |
330 |
while (p != limit) { |
|
331 |
*q = static_cast<char>(p[0]); |
|
332 |
++p; |
|
333 |
++q; |
|
334 |
} |
|
335 |
*q = '\0'; |
|
336 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
337 |
return asciiBuffer; |
0 | 338 |
} |
339 |
||
340 |
bool UString::is8Bit() const |
|
341 |
{ |
|
342 |
const UChar* u = data(); |
|
343 |
const UChar* limit = u + size(); |
|
344 |
while (u < limit) { |
|
345 |
if (u[0] > 0xFF) |
|
346 |
return false; |
|
347 |
++u; |
|
348 |
} |
|
349 |
||
350 |
return true; |
|
351 |
} |
|
352 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
353 |
UChar UString::operator[](unsigned pos) const |
0 | 354 |
{ |
355 |
if (pos >= size()) |
|
356 |
return '\0'; |
|
357 |
return data()[pos]; |
|
358 |
} |
|
359 |
||
360 |
double UString::toDouble(bool tolerateTrailingJunk, bool tolerateEmptyString) const |
|
361 |
{ |
|
362 |
if (size() == 1) { |
|
363 |
UChar c = data()[0]; |
|
364 |
if (isASCIIDigit(c)) |
|
365 |
return c - '0'; |
|
366 |
if (isASCIISpace(c) && tolerateEmptyString) |
|
367 |
return 0; |
|
368 |
return NaN; |
|
369 |
} |
|
370 |
||
371 |
// FIXME: If tolerateTrailingJunk is true, then we want to tolerate non-8-bit junk |
|
372 |
// after the number, so this is too strict a check. |
|
373 |
CStringBuffer s; |
|
374 |
if (!getCString(s)) |
|
375 |
return NaN; |
|
376 |
const char* c = s.data(); |
|
377 |
||
378 |
// skip leading white space |
|
379 |
while (isASCIISpace(*c)) |
|
380 |
c++; |
|
381 |
||
382 |
// empty string ? |
|
383 |
if (*c == '\0') |
|
384 |
return tolerateEmptyString ? 0.0 : NaN; |
|
385 |
||
386 |
double d; |
|
387 |
||
388 |
// hex number ? |
|
389 |
if (*c == '0' && (*(c + 1) == 'x' || *(c + 1) == 'X')) { |
|
390 |
const char* firstDigitPosition = c + 2; |
|
391 |
c++; |
|
392 |
d = 0.0; |
|
393 |
while (*(++c)) { |
|
394 |
if (*c >= '0' && *c <= '9') |
|
395 |
d = d * 16.0 + *c - '0'; |
|
396 |
else if ((*c >= 'A' && *c <= 'F') || (*c >= 'a' && *c <= 'f')) |
|
397 |
d = d * 16.0 + (*c & 0xdf) - 'A' + 10.0; |
|
398 |
else |
|
399 |
break; |
|
400 |
} |
|
401 |
||
402 |
if (d >= mantissaOverflowLowerBound) |
|
403 |
d = parseIntOverflow(firstDigitPosition, c - firstDigitPosition, 16); |
|
404 |
} else { |
|
405 |
// regular number ? |
|
406 |
char* end; |
|
407 |
d = WTF::strtod(c, &end); |
|
408 |
if ((d != 0.0 || end != c) && d != Inf && d != -Inf) { |
|
409 |
c = end; |
|
410 |
} else { |
|
411 |
double sign = 1.0; |
|
412 |
||
413 |
if (*c == '+') |
|
414 |
c++; |
|
415 |
else if (*c == '-') { |
|
416 |
sign = -1.0; |
|
417 |
c++; |
|
418 |
} |
|
419 |
||
420 |
// We used strtod() to do the conversion. However, strtod() handles |
|
421 |
// infinite values slightly differently than JavaScript in that it |
|
422 |
// converts the string "inf" with any capitalization to infinity, |
|
423 |
// whereas the ECMA spec requires that it be converted to NaN. |
|
424 |
||
425 |
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') { |
|
426 |
d = sign * Inf; |
|
427 |
c += 8; |
|
428 |
} else if ((d == Inf || d == -Inf) && *c != 'I' && *c != 'i') |
|
429 |
c = end; |
|
430 |
else |
|
431 |
return NaN; |
|
432 |
} |
|
433 |
} |
|
434 |
||
435 |
// allow trailing white space |
|
436 |
while (isASCIISpace(*c)) |
|
437 |
c++; |
|
438 |
// don't allow anything after - unless tolerant=true |
|
439 |
if (!tolerateTrailingJunk && *c != '\0') |
|
440 |
d = NaN; |
|
441 |
||
442 |
return d; |
|
443 |
} |
|
444 |
||
445 |
double UString::toDouble(bool tolerateTrailingJunk) const |
|
446 |
{ |
|
447 |
return toDouble(tolerateTrailingJunk, true); |
|
448 |
} |
|
449 |
||
450 |
double UString::toDouble() const |
|
451 |
{ |
|
452 |
return toDouble(false, true); |
|
453 |
} |
|
454 |
||
455 |
uint32_t UString::toUInt32(bool* ok) const |
|
456 |
{ |
|
457 |
double d = toDouble(); |
|
458 |
bool b = true; |
|
459 |
||
460 |
if (d != static_cast<uint32_t>(d)) { |
|
461 |
b = false; |
|
462 |
d = 0; |
|
463 |
} |
|
464 |
||
465 |
if (ok) |
|
466 |
*ok = b; |
|
467 |
||
468 |
return static_cast<uint32_t>(d); |
|
469 |
} |
|
470 |
||
471 |
uint32_t UString::toUInt32(bool* ok, bool tolerateEmptyString) const |
|
472 |
{ |
|
473 |
double d = toDouble(false, tolerateEmptyString); |
|
474 |
bool b = true; |
|
475 |
||
476 |
if (d != static_cast<uint32_t>(d)) { |
|
477 |
b = false; |
|
478 |
d = 0; |
|
479 |
} |
|
480 |
||
481 |
if (ok) |
|
482 |
*ok = b; |
|
483 |
||
484 |
return static_cast<uint32_t>(d); |
|
485 |
} |
|
486 |
||
487 |
uint32_t UString::toStrictUInt32(bool* ok) const |
|
488 |
{ |
|
489 |
if (ok) |
|
490 |
*ok = false; |
|
491 |
||
492 |
// Empty string is not OK. |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
493 |
unsigned len = m_rep->length(); |
0 | 494 |
if (len == 0) |
495 |
return 0; |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
496 |
const UChar* p = m_rep->characters(); |
0 | 497 |
unsigned short c = p[0]; |
498 |
||
499 |
// If the first digit is 0, only 0 itself is OK. |
|
500 |
if (c == '0') { |
|
501 |
if (len == 1 && ok) |
|
502 |
*ok = true; |
|
503 |
return 0; |
|
504 |
} |
|
505 |
||
506 |
// Convert to UInt32, checking for overflow. |
|
507 |
uint32_t i = 0; |
|
508 |
while (1) { |
|
509 |
// Process character, turning it into a digit. |
|
510 |
if (c < '0' || c > '9') |
|
511 |
return 0; |
|
512 |
const unsigned d = c - '0'; |
|
513 |
||
514 |
// Multiply by 10, checking for overflow out of 32 bits. |
|
515 |
if (i > 0xFFFFFFFFU / 10) |
|
516 |
return 0; |
|
517 |
i *= 10; |
|
518 |
||
519 |
// Add in the digit, checking for overflow out of 32 bits. |
|
520 |
const unsigned max = 0xFFFFFFFFU - d; |
|
521 |
if (i > max) |
|
522 |
return 0; |
|
523 |
i += d; |
|
524 |
||
525 |
// Handle end of string. |
|
526 |
if (--len == 0) { |
|
527 |
if (ok) |
|
528 |
*ok = true; |
|
529 |
return i; |
|
530 |
} |
|
531 |
||
532 |
// Get next character. |
|
533 |
c = *(++p); |
|
534 |
} |
|
535 |
} |
|
536 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
537 |
unsigned UString::find(const UString& f, unsigned pos) const |
0 | 538 |
{ |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
539 |
unsigned fsz = f.size(); |
0 | 540 |
|
541 |
if (fsz == 1) { |
|
542 |
UChar ch = f[0]; |
|
543 |
const UChar* end = data() + size(); |
|
544 |
for (const UChar* c = data() + pos; c < end; c++) { |
|
545 |
if (*c == ch) |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
546 |
return static_cast<unsigned>(c - data()); |
0 | 547 |
} |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
548 |
return NotFound; |
0 | 549 |
} |
550 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
551 |
unsigned sz = size(); |
0 | 552 |
if (sz < fsz) |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
553 |
return NotFound; |
0 | 554 |
if (fsz == 0) |
555 |
return pos; |
|
556 |
const UChar* end = data() + sz - fsz; |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
557 |
unsigned fsizeminusone = (fsz - 1) * sizeof(UChar); |
0 | 558 |
const UChar* fdata = f.data(); |
559 |
unsigned short fchar = fdata[0]; |
|
560 |
++fdata; |
|
561 |
for (const UChar* c = data() + pos; c <= end; c++) { |
|
562 |
if (c[0] == fchar && !memcmp(c + 1, fdata, fsizeminusone)) |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
563 |
return static_cast<unsigned>(c - data()); |
0 | 564 |
} |
565 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
566 |
return NotFound; |
0 | 567 |
} |
568 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
569 |
unsigned UString::find(UChar ch, unsigned pos) const |
0 | 570 |
{ |
571 |
const UChar* end = data() + size(); |
|
572 |
for (const UChar* c = data() + pos; c < end; c++) { |
|
573 |
if (*c == ch) |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
574 |
return static_cast<unsigned>(c - data()); |
0 | 575 |
} |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
576 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
577 |
return NotFound; |
0 | 578 |
} |
579 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
580 |
unsigned UString::rfind(const UString& f, unsigned pos) const |
0 | 581 |
{ |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
582 |
unsigned sz = size(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
583 |
unsigned fsz = f.size(); |
0 | 584 |
if (sz < fsz) |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
585 |
return NotFound; |
0 | 586 |
if (pos > sz - fsz) |
587 |
pos = sz - fsz; |
|
588 |
if (fsz == 0) |
|
589 |
return pos; |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
590 |
unsigned fsizeminusone = (fsz - 1) * sizeof(UChar); |
0 | 591 |
const UChar* fdata = f.data(); |
592 |
for (const UChar* c = data() + pos; c >= data(); c--) { |
|
593 |
if (*c == *fdata && !memcmp(c + 1, fdata + 1, fsizeminusone)) |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
594 |
return static_cast<unsigned>(c - data()); |
0 | 595 |
} |
596 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
597 |
return NotFound; |
0 | 598 |
} |
599 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
600 |
unsigned UString::rfind(UChar ch, unsigned pos) const |
0 | 601 |
{ |
602 |
if (isEmpty()) |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
603 |
return NotFound; |
0 | 604 |
if (pos + 1 >= size()) |
605 |
pos = size() - 1; |
|
606 |
for (const UChar* c = data() + pos; c >= data(); c--) { |
|
607 |
if (*c == ch) |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
608 |
return static_cast<unsigned>(c - data()); |
0 | 609 |
} |
610 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
611 |
return NotFound; |
0 | 612 |
} |
613 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
614 |
UString UString::substr(unsigned pos, unsigned len) const |
0 | 615 |
{ |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
616 |
unsigned s = size(); |
0 | 617 |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
618 |
if (pos >= s) |
0 | 619 |
pos = s; |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
620 |
unsigned limit = s - pos; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
621 |
if (len > limit) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
622 |
len = limit; |
0 | 623 |
|
624 |
if (pos == 0 && len == s) |
|
625 |
return *this; |
|
626 |
||
627 |
return UString(Rep::create(m_rep, pos, len)); |
|
628 |
} |
|
629 |
||
630 |
bool operator==(const UString& s1, const char *s2) |
|
631 |
{ |
|
632 |
if (s2 == 0) |
|
633 |
return s1.isEmpty(); |
|
634 |
||
635 |
const UChar* u = s1.data(); |
|
636 |
const UChar* uend = u + s1.size(); |
|
637 |
while (u != uend && *s2) { |
|
638 |
if (u[0] != (unsigned char)*s2) |
|
639 |
return false; |
|
640 |
s2++; |
|
641 |
u++; |
|
642 |
} |
|
643 |
||
644 |
return u == uend && *s2 == 0; |
|
645 |
} |
|
646 |
||
647 |
bool operator<(const UString& s1, const UString& s2) |
|
648 |
{ |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
649 |
const unsigned l1 = s1.size(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
650 |
const unsigned l2 = s2.size(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
651 |
const unsigned lmin = l1 < l2 ? l1 : l2; |
0 | 652 |
const UChar* c1 = s1.data(); |
653 |
const UChar* c2 = s2.data(); |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
654 |
unsigned l = 0; |
0 | 655 |
while (l < lmin && *c1 == *c2) { |
656 |
c1++; |
|
657 |
c2++; |
|
658 |
l++; |
|
659 |
} |
|
660 |
if (l < lmin) |
|
661 |
return (c1[0] < c2[0]); |
|
662 |
||
663 |
return (l1 < l2); |
|
664 |
} |
|
665 |
||
666 |
bool operator>(const UString& s1, const UString& s2) |
|
667 |
{ |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
668 |
const unsigned l1 = s1.size(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
669 |
const unsigned l2 = s2.size(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
670 |
const unsigned lmin = l1 < l2 ? l1 : l2; |
0 | 671 |
const UChar* c1 = s1.data(); |
672 |
const UChar* c2 = s2.data(); |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
673 |
unsigned l = 0; |
0 | 674 |
while (l < lmin && *c1 == *c2) { |
675 |
c1++; |
|
676 |
c2++; |
|
677 |
l++; |
|
678 |
} |
|
679 |
if (l < lmin) |
|
680 |
return (c1[0] > c2[0]); |
|
681 |
||
682 |
return (l1 > l2); |
|
683 |
} |
|
684 |
||
685 |
int compare(const UString& s1, const UString& s2) |
|
686 |
{ |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
687 |
const unsigned l1 = s1.size(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
688 |
const unsigned l2 = s2.size(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
689 |
const unsigned lmin = l1 < l2 ? l1 : l2; |
0 | 690 |
const UChar* c1 = s1.data(); |
691 |
const UChar* c2 = s2.data(); |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
692 |
unsigned l = 0; |
0 | 693 |
while (l < lmin && *c1 == *c2) { |
694 |
c1++; |
|
695 |
c2++; |
|
696 |
l++; |
|
697 |
} |
|
698 |
||
699 |
if (l < lmin) |
|
700 |
return (c1[0] > c2[0]) ? 1 : -1; |
|
701 |
||
702 |
if (l1 == l2) |
|
703 |
return 0; |
|
704 |
||
705 |
return (l1 > l2) ? 1 : -1; |
|
706 |
} |
|
707 |
||
708 |
bool equal(const UString::Rep* r, const UString::Rep* b) |
|
709 |
{ |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
710 |
unsigned length = r->length(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
711 |
if (length != b->length()) |
0 | 712 |
return false; |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
713 |
const UChar* d = r->characters(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
714 |
const UChar* s = b->characters(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
715 |
for (unsigned i = 0; i != length; ++i) { |
0 | 716 |
if (d[i] != s[i]) |
717 |
return false; |
|
718 |
} |
|
719 |
return true; |
|
720 |
} |
|
721 |
||
722 |
CString UString::UTF8String(bool strict) const |
|
723 |
{ |
|
724 |
// Allocate a buffer big enough to hold all the characters. |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
725 |
const unsigned length = size(); |
0 | 726 |
Vector<char, 1024> buffer(length * 3); |
727 |
||
728 |
// Convert to runs of 8-bit characters. |
|
729 |
char* p = buffer.data(); |
|
730 |
const UChar* d = reinterpret_cast<const UChar*>(&data()[0]); |
|
731 |
ConversionResult result = convertUTF16ToUTF8(&d, d + length, &p, p + buffer.size(), strict); |
|
732 |
if (result != conversionOK) |
|
733 |
return CString(); |
|
734 |
||
735 |
return CString(buffer.data(), p - buffer.data()); |
|
736 |
} |
|
737 |
||
738 |
} // namespace JSC |