author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 14 May 2010 16:40:13 +0300 | |
changeset 22 | 79de32ba3296 |
parent 3 | 41300fa6a67c |
child 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 1 |
/* |
2 |
* Copyright (C) 1999-2001 Harri Porten (porten@kde.org) |
|
3 |
* Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. |
|
4 |
* Copyright (C) 2009 Torch Mobile, Inc. |
|
5 |
* |
|
6 |
* This library is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU Lesser General Public |
|
8 |
* License as published by the Free Software Foundation; either |
|
9 |
* version 2 of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This library is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 |
* Lesser General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU Lesser General Public |
|
17 |
* License along with this library; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
19 |
* |
|
20 |
*/ |
|
21 |
||
22 |
#include "config.h" |
|
23 |
#include "StringPrototype.h" |
|
24 |
||
25 |
#include "CachedCall.h" |
|
26 |
#include "Error.h" |
|
27 |
#include "Executable.h" |
|
28 |
#include "JSGlobalObjectFunctions.h" |
|
29 |
#include "JSArray.h" |
|
30 |
#include "JSFunction.h" |
|
31 |
#include "ObjectPrototype.h" |
|
32 |
#include "PropertyNameArray.h" |
|
33 |
#include "RegExpConstructor.h" |
|
34 |
#include "RegExpObject.h" |
|
35 |
#include <wtf/ASCIICType.h> |
|
36 |
#include <wtf/MathExtras.h> |
|
37 |
#include <wtf/unicode/Collator.h> |
|
38 |
||
39 |
using namespace WTF; |
|
40 |
||
41 |
namespace JSC { |
|
42 |
||
43 |
ASSERT_CLASS_FITS_IN_CELL(StringPrototype); |
|
44 |
||
45 |
static JSValue JSC_HOST_CALL stringProtoFuncToString(ExecState*, JSObject*, JSValue, const ArgList&); |
|
46 |
static JSValue JSC_HOST_CALL stringProtoFuncCharAt(ExecState*, JSObject*, JSValue, const ArgList&); |
|
47 |
static JSValue JSC_HOST_CALL stringProtoFuncCharCodeAt(ExecState*, JSObject*, JSValue, const ArgList&); |
|
48 |
static JSValue JSC_HOST_CALL stringProtoFuncConcat(ExecState*, JSObject*, JSValue, const ArgList&); |
|
49 |
static JSValue JSC_HOST_CALL stringProtoFuncIndexOf(ExecState*, JSObject*, JSValue, const ArgList&); |
|
50 |
static JSValue JSC_HOST_CALL stringProtoFuncLastIndexOf(ExecState*, JSObject*, JSValue, const ArgList&); |
|
51 |
static JSValue JSC_HOST_CALL stringProtoFuncMatch(ExecState*, JSObject*, JSValue, const ArgList&); |
|
52 |
static JSValue JSC_HOST_CALL stringProtoFuncReplace(ExecState*, JSObject*, JSValue, const ArgList&); |
|
53 |
static JSValue JSC_HOST_CALL stringProtoFuncSearch(ExecState*, JSObject*, JSValue, const ArgList&); |
|
54 |
static JSValue JSC_HOST_CALL stringProtoFuncSlice(ExecState*, JSObject*, JSValue, const ArgList&); |
|
55 |
static JSValue JSC_HOST_CALL stringProtoFuncSplit(ExecState*, JSObject*, JSValue, const ArgList&); |
|
56 |
static JSValue JSC_HOST_CALL stringProtoFuncSubstr(ExecState*, JSObject*, JSValue, const ArgList&); |
|
57 |
static JSValue JSC_HOST_CALL stringProtoFuncSubstring(ExecState*, JSObject*, JSValue, const ArgList&); |
|
58 |
static JSValue JSC_HOST_CALL stringProtoFuncToLowerCase(ExecState*, JSObject*, JSValue, const ArgList&); |
|
59 |
static JSValue JSC_HOST_CALL stringProtoFuncToUpperCase(ExecState*, JSObject*, JSValue, const ArgList&); |
|
60 |
static JSValue JSC_HOST_CALL stringProtoFuncLocaleCompare(ExecState*, JSObject*, JSValue, const ArgList&); |
|
61 |
||
62 |
static JSValue JSC_HOST_CALL stringProtoFuncBig(ExecState*, JSObject*, JSValue, const ArgList&); |
|
63 |
static JSValue JSC_HOST_CALL stringProtoFuncSmall(ExecState*, JSObject*, JSValue, const ArgList&); |
|
64 |
static JSValue JSC_HOST_CALL stringProtoFuncBlink(ExecState*, JSObject*, JSValue, const ArgList&); |
|
65 |
static JSValue JSC_HOST_CALL stringProtoFuncBold(ExecState*, JSObject*, JSValue, const ArgList&); |
|
66 |
static JSValue JSC_HOST_CALL stringProtoFuncFixed(ExecState*, JSObject*, JSValue, const ArgList&); |
|
67 |
static JSValue JSC_HOST_CALL stringProtoFuncItalics(ExecState*, JSObject*, JSValue, const ArgList&); |
|
68 |
static JSValue JSC_HOST_CALL stringProtoFuncStrike(ExecState*, JSObject*, JSValue, const ArgList&); |
|
69 |
static JSValue JSC_HOST_CALL stringProtoFuncSub(ExecState*, JSObject*, JSValue, const ArgList&); |
|
70 |
static JSValue JSC_HOST_CALL stringProtoFuncSup(ExecState*, JSObject*, JSValue, const ArgList&); |
|
71 |
static JSValue JSC_HOST_CALL stringProtoFuncFontcolor(ExecState*, JSObject*, JSValue, const ArgList&); |
|
72 |
static JSValue JSC_HOST_CALL stringProtoFuncFontsize(ExecState*, JSObject*, JSValue, const ArgList&); |
|
73 |
static JSValue JSC_HOST_CALL stringProtoFuncAnchor(ExecState*, JSObject*, JSValue, const ArgList&); |
|
74 |
static JSValue JSC_HOST_CALL stringProtoFuncLink(ExecState*, JSObject*, JSValue, const ArgList&); |
|
75 |
||
76 |
static JSValue JSC_HOST_CALL stringProtoFuncTrim(ExecState*, JSObject*, JSValue, const ArgList&); |
|
77 |
static JSValue JSC_HOST_CALL stringProtoFuncTrimLeft(ExecState*, JSObject*, JSValue, const ArgList&); |
|
78 |
static JSValue JSC_HOST_CALL stringProtoFuncTrimRight(ExecState*, JSObject*, JSValue, const ArgList&); |
|
79 |
||
80 |
} |
|
81 |
||
82 |
#include "StringPrototype.lut.h" |
|
83 |
||
84 |
namespace JSC { |
|
85 |
||
86 |
const ClassInfo StringPrototype::info = { "String", &StringObject::info, 0, ExecState::stringTable }; |
|
87 |
||
88 |
/* Source for StringPrototype.lut.h |
|
89 |
@begin stringTable 26 |
|
90 |
toString stringProtoFuncToString DontEnum|Function 0 |
|
91 |
valueOf stringProtoFuncToString DontEnum|Function 0 |
|
92 |
charAt stringProtoFuncCharAt DontEnum|Function 1 |
|
93 |
charCodeAt stringProtoFuncCharCodeAt DontEnum|Function 1 |
|
94 |
concat stringProtoFuncConcat DontEnum|Function 1 |
|
95 |
indexOf stringProtoFuncIndexOf DontEnum|Function 1 |
|
96 |
lastIndexOf stringProtoFuncLastIndexOf DontEnum|Function 1 |
|
97 |
match stringProtoFuncMatch DontEnum|Function 1 |
|
98 |
replace stringProtoFuncReplace DontEnum|Function 2 |
|
99 |
search stringProtoFuncSearch DontEnum|Function 1 |
|
100 |
slice stringProtoFuncSlice DontEnum|Function 2 |
|
101 |
split stringProtoFuncSplit DontEnum|Function 2 |
|
102 |
substr stringProtoFuncSubstr DontEnum|Function 2 |
|
103 |
substring stringProtoFuncSubstring DontEnum|Function 2 |
|
104 |
toLowerCase stringProtoFuncToLowerCase DontEnum|Function 0 |
|
105 |
toUpperCase stringProtoFuncToUpperCase DontEnum|Function 0 |
|
106 |
localeCompare stringProtoFuncLocaleCompare DontEnum|Function 1 |
|
107 |
||
108 |
# toLocaleLowerCase and toLocaleUpperCase are currently identical to toLowerCase and toUpperCase |
|
109 |
toLocaleLowerCase stringProtoFuncToLowerCase DontEnum|Function 0 |
|
110 |
toLocaleUpperCase stringProtoFuncToUpperCase DontEnum|Function 0 |
|
111 |
||
112 |
big stringProtoFuncBig DontEnum|Function 0 |
|
113 |
small stringProtoFuncSmall DontEnum|Function 0 |
|
114 |
blink stringProtoFuncBlink DontEnum|Function 0 |
|
115 |
bold stringProtoFuncBold DontEnum|Function 0 |
|
116 |
fixed stringProtoFuncFixed DontEnum|Function 0 |
|
117 |
italics stringProtoFuncItalics DontEnum|Function 0 |
|
118 |
strike stringProtoFuncStrike DontEnum|Function 0 |
|
119 |
sub stringProtoFuncSub DontEnum|Function 0 |
|
120 |
sup stringProtoFuncSup DontEnum|Function 0 |
|
121 |
fontcolor stringProtoFuncFontcolor DontEnum|Function 1 |
|
122 |
fontsize stringProtoFuncFontsize DontEnum|Function 1 |
|
123 |
anchor stringProtoFuncAnchor DontEnum|Function 1 |
|
124 |
link stringProtoFuncLink DontEnum|Function 1 |
|
125 |
trim stringProtoFuncTrim DontEnum|Function 0 |
|
126 |
trimLeft stringProtoFuncTrimLeft DontEnum|Function 0 |
|
127 |
trimRight stringProtoFuncTrimRight DontEnum|Function 0 |
|
128 |
@end |
|
129 |
*/ |
|
130 |
||
131 |
// ECMA 15.5.4 |
|
132 |
StringPrototype::StringPrototype(ExecState* exec, NonNullPassRefPtr<Structure> structure) |
|
133 |
: StringObject(exec, structure) |
|
134 |
{ |
|
135 |
// The constructor will be added later, after StringConstructor has been built |
|
136 |
putDirectWithoutTransition(exec->propertyNames().length, jsNumber(exec, 0), DontDelete | ReadOnly | DontEnum); |
|
137 |
} |
|
138 |
||
139 |
bool StringPrototype::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot &slot) |
|
140 |
{ |
|
141 |
return getStaticFunctionSlot<StringObject>(exec, ExecState::stringTable(exec), this, propertyName, slot); |
|
142 |
} |
|
143 |
||
144 |
bool StringPrototype::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor) |
|
145 |
{ |
|
146 |
return getStaticFunctionDescriptor<StringObject>(exec, ExecState::stringTable(exec), this, propertyName, descriptor); |
|
147 |
} |
|
148 |
||
149 |
// ------------------------------ Functions -------------------------- |
|
150 |
||
151 |
static inline UString substituteBackreferences(const UString& replacement, const UString& source, const int* ovector, RegExp* reg) |
|
152 |
{ |
|
153 |
UString substitutedReplacement; |
|
154 |
int offset = 0; |
|
155 |
int i = -1; |
|
156 |
while ((i = replacement.find('$', i + 1)) != -1) { |
|
157 |
if (i + 1 == replacement.size()) |
|
158 |
break; |
|
159 |
||
160 |
UChar ref = replacement[i + 1]; |
|
161 |
if (ref == '$') { |
|
162 |
// "$$" -> "$" |
|
163 |
++i; |
|
164 |
substitutedReplacement.append(replacement.data() + offset, i - offset); |
|
165 |
offset = i + 1; |
|
166 |
continue; |
|
167 |
} |
|
168 |
||
169 |
int backrefStart; |
|
170 |
int backrefLength; |
|
171 |
int advance = 0; |
|
172 |
if (ref == '&') { |
|
173 |
backrefStart = ovector[0]; |
|
174 |
backrefLength = ovector[1] - backrefStart; |
|
175 |
} else if (ref == '`') { |
|
176 |
backrefStart = 0; |
|
177 |
backrefLength = ovector[0]; |
|
178 |
} else if (ref == '\'') { |
|
179 |
backrefStart = ovector[1]; |
|
180 |
backrefLength = source.size() - backrefStart; |
|
181 |
} else if (reg && ref >= '0' && ref <= '9') { |
|
182 |
// 1- and 2-digit back references are allowed |
|
183 |
unsigned backrefIndex = ref - '0'; |
|
184 |
if (backrefIndex > reg->numSubpatterns()) |
|
185 |
continue; |
|
186 |
if (replacement.size() > i + 2) { |
|
187 |
ref = replacement[i + 2]; |
|
188 |
if (ref >= '0' && ref <= '9') { |
|
189 |
backrefIndex = 10 * backrefIndex + ref - '0'; |
|
190 |
if (backrefIndex > reg->numSubpatterns()) |
|
191 |
backrefIndex = backrefIndex / 10; // Fall back to the 1-digit reference |
|
192 |
else |
|
193 |
advance = 1; |
|
194 |
} |
|
195 |
} |
|
196 |
if (!backrefIndex) |
|
197 |
continue; |
|
198 |
backrefStart = ovector[2 * backrefIndex]; |
|
199 |
backrefLength = ovector[2 * backrefIndex + 1] - backrefStart; |
|
200 |
} else |
|
201 |
continue; |
|
202 |
||
203 |
if (i - offset) |
|
204 |
substitutedReplacement.append(replacement.data() + offset, i - offset); |
|
205 |
i += 1 + advance; |
|
206 |
offset = i + 1; |
|
207 |
substitutedReplacement.append(source.data() + backrefStart, backrefLength); |
|
208 |
} |
|
209 |
||
210 |
if (!offset) |
|
211 |
return replacement; |
|
212 |
||
213 |
if (replacement.size() - offset) |
|
214 |
substitutedReplacement.append(replacement.data() + offset, replacement.size() - offset); |
|
215 |
||
216 |
return substitutedReplacement; |
|
217 |
} |
|
218 |
||
219 |
static inline int localeCompare(const UString& a, const UString& b) |
|
220 |
{ |
|
221 |
return Collator::userDefault()->collate(reinterpret_cast<const ::UChar*>(a.data()), a.size(), reinterpret_cast<const ::UChar*>(b.data()), b.size()); |
|
222 |
} |
|
223 |
||
224 |
JSValue JSC_HOST_CALL stringProtoFuncReplace(ExecState* exec, JSObject*, JSValue thisValue, const ArgList& args) |
|
225 |
{ |
|
226 |
JSString* sourceVal = thisValue.toThisJSString(exec); |
|
227 |
const UString& source = sourceVal->value(); |
|
228 |
||
229 |
JSValue pattern = args.at(0); |
|
230 |
||
231 |
JSValue replacement = args.at(1); |
|
232 |
UString replacementString; |
|
233 |
CallData callData; |
|
234 |
CallType callType = replacement.getCallData(callData); |
|
235 |
if (callType == CallTypeNone) |
|
236 |
replacementString = replacement.toString(exec); |
|
237 |
||
238 |
if (pattern.inherits(&RegExpObject::info)) { |
|
239 |
RegExp* reg = asRegExpObject(pattern)->regExp(); |
|
240 |
bool global = reg->global(); |
|
241 |
||
242 |
RegExpConstructor* regExpConstructor = exec->lexicalGlobalObject()->regExpConstructor(); |
|
243 |
||
244 |
int lastIndex = 0; |
|
245 |
int startPosition = 0; |
|
246 |
||
247 |
Vector<UString::Range, 16> sourceRanges; |
|
248 |
Vector<UString, 16> replacements; |
|
249 |
||
250 |
// This is either a loop (if global is set) or a one-way (if not). |
|
251 |
if (global && callType == CallTypeJS) { |
|
252 |
// reg->numSubpatterns() + 1 for pattern args, + 2 for match start and sourceValue |
|
253 |
int argCount = reg->numSubpatterns() + 1 + 2; |
|
254 |
JSFunction* func = asFunction(replacement); |
|
255 |
CachedCall cachedCall(exec, func, argCount, exec->exceptionSlot()); |
|
256 |
if (exec->hadException()) |
|
257 |
return jsNull(); |
|
258 |
while (true) { |
|
259 |
int matchIndex; |
|
260 |
int matchLen = 0; |
|
261 |
int* ovector; |
|
262 |
regExpConstructor->performMatch(reg, source, startPosition, matchIndex, matchLen, &ovector); |
|
263 |
if (matchIndex < 0) |
|
264 |
break; |
|
265 |
||
266 |
sourceRanges.append(UString::Range(lastIndex, matchIndex - lastIndex)); |
|
267 |
||
268 |
int completeMatchStart = ovector[0]; |
|
269 |
unsigned i = 0; |
|
270 |
for (; i < reg->numSubpatterns() + 1; ++i) { |
|
271 |
int matchStart = ovector[i * 2]; |
|
272 |
int matchLen = ovector[i * 2 + 1] - matchStart; |
|
273 |
||
274 |
if (matchStart < 0) |
|
275 |
cachedCall.setArgument(i, jsUndefined()); |
|
276 |
else |
|
277 |
cachedCall.setArgument(i, jsSubstring(exec, source, matchStart, matchLen)); |
|
278 |
} |
|
279 |
||
280 |
cachedCall.setArgument(i++, jsNumber(exec, completeMatchStart)); |
|
281 |
cachedCall.setArgument(i++, sourceVal); |
|
282 |
||
283 |
cachedCall.setThis(exec->globalThisValue()); |
|
284 |
replacements.append(cachedCall.call().toString(cachedCall.newCallFrame())); |
|
285 |
if (exec->hadException()) |
|
286 |
break; |
|
287 |
||
288 |
lastIndex = matchIndex + matchLen; |
|
289 |
startPosition = lastIndex; |
|
290 |
||
291 |
// special case of empty match |
|
292 |
if (matchLen == 0) { |
|
293 |
startPosition++; |
|
294 |
if (startPosition > source.size()) |
|
295 |
break; |
|
296 |
} |
|
297 |
} |
|
298 |
} else { |
|
299 |
do { |
|
300 |
int matchIndex; |
|
301 |
int matchLen = 0; |
|
302 |
int* ovector; |
|
303 |
regExpConstructor->performMatch(reg, source, startPosition, matchIndex, matchLen, &ovector); |
|
304 |
if (matchIndex < 0) |
|
305 |
break; |
|
306 |
||
307 |
sourceRanges.append(UString::Range(lastIndex, matchIndex - lastIndex)); |
|
308 |
||
309 |
if (callType != CallTypeNone) { |
|
310 |
int completeMatchStart = ovector[0]; |
|
311 |
MarkedArgumentBuffer args; |
|
312 |
||
313 |
for (unsigned i = 0; i < reg->numSubpatterns() + 1; ++i) { |
|
314 |
int matchStart = ovector[i * 2]; |
|
315 |
int matchLen = ovector[i * 2 + 1] - matchStart; |
|
316 |
||
317 |
if (matchStart < 0) |
|
318 |
args.append(jsUndefined()); |
|
319 |
else |
|
320 |
args.append(jsSubstring(exec, source, matchStart, matchLen)); |
|
321 |
} |
|
322 |
||
323 |
args.append(jsNumber(exec, completeMatchStart)); |
|
324 |
args.append(sourceVal); |
|
325 |
||
326 |
replacements.append(call(exec, replacement, callType, callData, exec->globalThisValue(), args).toString(exec)); |
|
327 |
if (exec->hadException()) |
|
328 |
break; |
|
329 |
} else |
|
330 |
replacements.append(substituteBackreferences(replacementString, source, ovector, reg)); |
|
331 |
||
332 |
lastIndex = matchIndex + matchLen; |
|
333 |
startPosition = lastIndex; |
|
334 |
||
335 |
// special case of empty match |
|
336 |
if (matchLen == 0) { |
|
337 |
startPosition++; |
|
338 |
if (startPosition > source.size()) |
|
339 |
break; |
|
340 |
} |
|
341 |
} while (global); |
|
342 |
} |
|
343 |
||
344 |
if (!lastIndex && replacements.isEmpty()) |
|
345 |
return sourceVal; |
|
346 |
||
347 |
if (lastIndex < source.size()) |
|
348 |
sourceRanges.append(UString::Range(lastIndex, source.size() - lastIndex)); |
|
349 |
||
350 |
return jsString(exec, source.spliceSubstringsWithSeparators(sourceRanges.data(), sourceRanges.size(), |
|
351 |
replacements.data(), replacements.size())); |
|
352 |
} |
|
353 |
||
354 |
// Not a regular expression, so treat the pattern as a string. |
|
355 |
||
356 |
UString patternString = pattern.toString(exec); |
|
357 |
int matchPos = source.find(patternString); |
|
358 |
||
359 |
if (matchPos == -1) |
|
360 |
return sourceVal; |
|
361 |
||
362 |
int matchLen = patternString.size(); |
|
363 |
if (callType != CallTypeNone) { |
|
364 |
MarkedArgumentBuffer args; |
|
365 |
args.append(jsSubstring(exec, source, matchPos, matchLen)); |
|
366 |
args.append(jsNumber(exec, matchPos)); |
|
367 |
args.append(sourceVal); |
|
368 |
||
369 |
replacementString = call(exec, replacement, callType, callData, exec->globalThisValue(), args).toString(exec); |
|
370 |
} |
|
371 |
||
372 |
int ovector[2] = { matchPos, matchPos + matchLen }; |
|
373 |
return jsString(exec, source.replaceRange(matchPos, matchLen, substituteBackreferences(replacementString, source, ovector, 0))); |
|
374 |
} |
|
375 |
||
376 |
JSValue JSC_HOST_CALL stringProtoFuncToString(ExecState* exec, JSObject*, JSValue thisValue, const ArgList&) |
|
377 |
{ |
|
378 |
// Also used for valueOf. |
|
379 |
||
380 |
if (thisValue.isString()) |
|
381 |
return thisValue; |
|
382 |
||
383 |
if (thisValue.inherits(&StringObject::info)) |
|
384 |
return asStringObject(thisValue)->internalValue(); |
|
385 |
||
386 |
return throwError(exec, TypeError); |
|
387 |
} |
|
388 |
||
389 |
JSValue JSC_HOST_CALL stringProtoFuncCharAt(ExecState* exec, JSObject*, JSValue thisValue, const ArgList& args) |
|
390 |
{ |
|
391 |
UString s = thisValue.toThisString(exec); |
|
392 |
unsigned len = s.size(); |
|
393 |
JSValue a0 = args.at(0); |
|
394 |
if (a0.isUInt32()) { |
|
395 |
uint32_t i = a0.asUInt32(); |
|
396 |
if (i < len) |
|
397 |
return jsSingleCharacterSubstring(exec, s, i); |
|
398 |
return jsEmptyString(exec); |
|
399 |
} |
|
400 |
double dpos = a0.toInteger(exec); |
|
401 |
if (dpos >= 0 && dpos < len) |
|
402 |
return jsSingleCharacterSubstring(exec, s, static_cast<unsigned>(dpos)); |
|
403 |
return jsEmptyString(exec); |
|
404 |
} |
|
405 |
||
406 |
JSValue JSC_HOST_CALL stringProtoFuncCharCodeAt(ExecState* exec, JSObject*, JSValue thisValue, const ArgList& args) |
|
407 |
{ |
|
408 |
UString s = thisValue.toThisString(exec); |
|
409 |
unsigned len = s.size(); |
|
410 |
JSValue a0 = args.at(0); |
|
411 |
if (a0.isUInt32()) { |
|
412 |
uint32_t i = a0.asUInt32(); |
|
413 |
if (i < len) |
|
414 |
return jsNumber(exec, s.data()[i]); |
|
415 |
return jsNaN(exec); |
|
416 |
} |
|
417 |
double dpos = a0.toInteger(exec); |
|
418 |
if (dpos >= 0 && dpos < len) |
|
419 |
return jsNumber(exec, s[static_cast<int>(dpos)]); |
|
420 |
return jsNaN(exec); |
|
421 |
} |
|
422 |
||
423 |
JSValue JSC_HOST_CALL stringProtoFuncConcat(ExecState* exec, JSObject*, JSValue thisValue, const ArgList& args) |
|
424 |
{ |
|
425 |
UString s = thisValue.toThisString(exec); |
|
426 |
||
427 |
ArgList::const_iterator end = args.end(); |
|
428 |
for (ArgList::const_iterator it = args.begin(); it != end; ++it) |
|
429 |
s += (*it).toString(exec); |
|
430 |
return jsString(exec, s); |
|
431 |
} |
|
432 |
||
433 |
JSValue JSC_HOST_CALL stringProtoFuncIndexOf(ExecState* exec, JSObject*, JSValue thisValue, const ArgList& args) |
|
434 |
{ |
|
435 |
UString s = thisValue.toThisString(exec); |
|
436 |
int len = s.size(); |
|
437 |
||
438 |
JSValue a0 = args.at(0); |
|
439 |
JSValue a1 = args.at(1); |
|
440 |
UString u2 = a0.toString(exec); |
|
441 |
int pos; |
|
442 |
if (a1.isUndefined()) |
|
443 |
pos = 0; |
|
444 |
else if (a1.isUInt32()) |
|
445 |
pos = min<uint32_t>(a1.asUInt32(), len); |
|
446 |
else { |
|
447 |
double dpos = a1.toInteger(exec); |
|
448 |
if (dpos < 0) |
|
449 |
dpos = 0; |
|
450 |
else if (dpos > len) |
|
451 |
dpos = len; |
|
452 |
pos = static_cast<int>(dpos); |
|
453 |
} |
|
454 |
||
455 |
return jsNumber(exec, s.find(u2, pos)); |
|
456 |
} |
|
457 |
||
458 |
JSValue JSC_HOST_CALL stringProtoFuncLastIndexOf(ExecState* exec, JSObject*, JSValue thisValue, const ArgList& args) |
|
459 |
{ |
|
460 |
UString s = thisValue.toThisString(exec); |
|
461 |
int len = s.size(); |
|
462 |
||
463 |
JSValue a0 = args.at(0); |
|
464 |
JSValue a1 = args.at(1); |
|
465 |
||
466 |
UString u2 = a0.toString(exec); |
|
467 |
double dpos = a1.toIntegerPreserveNaN(exec); |
|
468 |
if (dpos < 0) |
|
469 |
dpos = 0; |
|
470 |
else if (!(dpos <= len)) // true for NaN |
|
471 |
dpos = len; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
472 |
#if PLATFORM(SYMBIAN) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
473 |
// Work around for broken NaN compare operator |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
474 |
else if (isnan(dpos)) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
475 |
dpos = len; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
476 |
#endif |
0 | 477 |
return jsNumber(exec, s.rfind(u2, static_cast<int>(dpos))); |
478 |
} |
|
479 |
||
480 |
JSValue JSC_HOST_CALL stringProtoFuncMatch(ExecState* exec, JSObject*, JSValue thisValue, const ArgList& args) |
|
481 |
{ |
|
482 |
UString s = thisValue.toThisString(exec); |
|
483 |
||
484 |
JSValue a0 = args.at(0); |
|
485 |
||
486 |
UString u = s; |
|
487 |
RefPtr<RegExp> reg; |
|
488 |
RegExpObject* imp = 0; |
|
489 |
if (a0.inherits(&RegExpObject::info)) |
|
490 |
reg = asRegExpObject(a0)->regExp(); |
|
491 |
else { |
|
492 |
/* |
|
493 |
* ECMA 15.5.4.12 String.prototype.search (regexp) |
|
494 |
* If regexp is not an object whose [[Class]] property is "RegExp", it is |
|
495 |
* replaced with the result of the expression new RegExp(regexp). |
|
496 |
*/ |
|
497 |
reg = RegExp::create(&exec->globalData(), a0.toString(exec)); |
|
498 |
} |
|
499 |
RegExpConstructor* regExpConstructor = exec->lexicalGlobalObject()->regExpConstructor(); |
|
500 |
int pos; |
|
501 |
int matchLength = 0; |
|
502 |
regExpConstructor->performMatch(reg.get(), u, 0, pos, matchLength); |
|
503 |
if (!(reg->global())) { |
|
504 |
// case without 'g' flag is handled like RegExp.prototype.exec |
|
505 |
if (pos < 0) |
|
506 |
return jsNull(); |
|
507 |
return regExpConstructor->arrayOfMatches(exec); |
|
508 |
} |
|
509 |
||
510 |
// return array of matches |
|
511 |
MarkedArgumentBuffer list; |
|
512 |
int lastIndex = 0; |
|
513 |
while (pos >= 0) { |
|
514 |
list.append(jsSubstring(exec, u, pos, matchLength)); |
|
515 |
lastIndex = pos; |
|
516 |
pos += matchLength == 0 ? 1 : matchLength; |
|
517 |
regExpConstructor->performMatch(reg.get(), u, pos, pos, matchLength); |
|
518 |
} |
|
519 |
if (imp) |
|
520 |
imp->setLastIndex(lastIndex); |
|
521 |
if (list.isEmpty()) { |
|
522 |
// if there are no matches at all, it's important to return |
|
523 |
// Null instead of an empty array, because this matches |
|
524 |
// other browsers and because Null is a false value. |
|
525 |
return jsNull(); |
|
526 |
} |
|
527 |
||
528 |
return constructArray(exec, list); |
|
529 |
} |
|
530 |
||
531 |
JSValue JSC_HOST_CALL stringProtoFuncSearch(ExecState* exec, JSObject*, JSValue thisValue, const ArgList& args) |
|
532 |
{ |
|
533 |
UString s = thisValue.toThisString(exec); |
|
534 |
||
535 |
JSValue a0 = args.at(0); |
|
536 |
||
537 |
UString u = s; |
|
538 |
RefPtr<RegExp> reg; |
|
539 |
if (a0.inherits(&RegExpObject::info)) |
|
540 |
reg = asRegExpObject(a0)->regExp(); |
|
541 |
else { |
|
542 |
/* |
|
543 |
* ECMA 15.5.4.12 String.prototype.search (regexp) |
|
544 |
* If regexp is not an object whose [[Class]] property is "RegExp", it is |
|
545 |
* replaced with the result of the expression new RegExp(regexp). |
|
546 |
*/ |
|
547 |
reg = RegExp::create(&exec->globalData(), a0.toString(exec)); |
|
548 |
} |
|
549 |
RegExpConstructor* regExpConstructor = exec->lexicalGlobalObject()->regExpConstructor(); |
|
550 |
int pos; |
|
551 |
int matchLength = 0; |
|
552 |
regExpConstructor->performMatch(reg.get(), u, 0, pos, matchLength); |
|
553 |
return jsNumber(exec, pos); |
|
554 |
} |
|
555 |
||
556 |
JSValue JSC_HOST_CALL stringProtoFuncSlice(ExecState* exec, JSObject*, JSValue thisValue, const ArgList& args) |
|
557 |
{ |
|
558 |
UString s = thisValue.toThisString(exec); |
|
559 |
int len = s.size(); |
|
560 |
||
561 |
JSValue a0 = args.at(0); |
|
562 |
JSValue a1 = args.at(1); |
|
563 |
||
564 |
// The arg processing is very much like ArrayProtoFunc::Slice |
|
565 |
double start = a0.toInteger(exec); |
|
566 |
double end = a1.isUndefined() ? len : a1.toInteger(exec); |
|
567 |
double from = start < 0 ? len + start : start; |
|
568 |
double to = end < 0 ? len + end : end; |
|
569 |
if (to > from && to > 0 && from < len) { |
|
570 |
if (from < 0) |
|
571 |
from = 0; |
|
572 |
if (to > len) |
|
573 |
to = len; |
|
574 |
return jsSubstring(exec, s, static_cast<unsigned>(from), static_cast<unsigned>(to) - static_cast<unsigned>(from)); |
|
575 |
} |
|
576 |
||
577 |
return jsEmptyString(exec); |
|
578 |
} |
|
579 |
||
580 |
JSValue JSC_HOST_CALL stringProtoFuncSplit(ExecState* exec, JSObject*, JSValue thisValue, const ArgList& args) |
|
581 |
{ |
|
582 |
UString s = thisValue.toThisString(exec); |
|
583 |
||
584 |
JSValue a0 = args.at(0); |
|
585 |
JSValue a1 = args.at(1); |
|
586 |
||
587 |
JSArray* result = constructEmptyArray(exec); |
|
588 |
unsigned i = 0; |
|
589 |
int p0 = 0; |
|
590 |
unsigned limit = a1.isUndefined() ? 0xFFFFFFFFU : a1.toUInt32(exec); |
|
591 |
if (a0.inherits(&RegExpObject::info)) { |
|
592 |
RegExp* reg = asRegExpObject(a0)->regExp(); |
|
593 |
if (s.isEmpty() && reg->match(s, 0) >= 0) { |
|
594 |
// empty string matched by regexp -> empty array |
|
595 |
return result; |
|
596 |
} |
|
597 |
int pos = 0; |
|
598 |
while (i != limit && pos < s.size()) { |
|
599 |
Vector<int, 32> ovector; |
|
600 |
int mpos = reg->match(s, pos, &ovector); |
|
601 |
if (mpos < 0) |
|
602 |
break; |
|
603 |
int mlen = ovector[1] - ovector[0]; |
|
604 |
pos = mpos + (mlen == 0 ? 1 : mlen); |
|
605 |
if (mpos != p0 || mlen) { |
|
606 |
result->put(exec, i++, jsSubstring(exec, s, p0, mpos - p0)); |
|
607 |
p0 = mpos + mlen; |
|
608 |
} |
|
609 |
for (unsigned si = 1; si <= reg->numSubpatterns(); ++si) { |
|
610 |
int spos = ovector[si * 2]; |
|
611 |
if (spos < 0) |
|
612 |
result->put(exec, i++, jsUndefined()); |
|
613 |
else |
|
614 |
result->put(exec, i++, jsSubstring(exec, s, spos, ovector[si * 2 + 1] - spos)); |
|
615 |
} |
|
616 |
} |
|
617 |
} else { |
|
618 |
UString u2 = a0.toString(exec); |
|
619 |
if (u2.isEmpty()) { |
|
620 |
if (s.isEmpty()) { |
|
621 |
// empty separator matches empty string -> empty array |
|
622 |
return result; |
|
623 |
} |
|
624 |
while (i != limit && p0 < s.size() - 1) |
|
625 |
result->put(exec, i++, jsSingleCharacterSubstring(exec, s, p0++)); |
|
626 |
} else { |
|
627 |
int pos; |
|
628 |
while (i != limit && (pos = s.find(u2, p0)) >= 0) { |
|
629 |
result->put(exec, i++, jsSubstring(exec, s, p0, pos - p0)); |
|
630 |
p0 = pos + u2.size(); |
|
631 |
} |
|
632 |
} |
|
633 |
} |
|
634 |
||
635 |
// add remaining string |
|
636 |
if (i != limit) |
|
637 |
result->put(exec, i++, jsSubstring(exec, s, p0, s.size() - p0)); |
|
638 |
||
639 |
return result; |
|
640 |
} |
|
641 |
||
642 |
JSValue JSC_HOST_CALL stringProtoFuncSubstr(ExecState* exec, JSObject*, JSValue thisValue, const ArgList& args) |
|
643 |
{ |
|
644 |
UString s = thisValue.toThisString(exec); |
|
645 |
int len = s.size(); |
|
646 |
||
647 |
JSValue a0 = args.at(0); |
|
648 |
JSValue a1 = args.at(1); |
|
649 |
||
650 |
double start = a0.toInteger(exec); |
|
651 |
double length = a1.isUndefined() ? len : a1.toInteger(exec); |
|
652 |
if (start >= len || length <= 0) |
|
653 |
return jsEmptyString(exec); |
|
654 |
if (start < 0) { |
|
655 |
start += len; |
|
656 |
if (start < 0) |
|
657 |
start = 0; |
|
658 |
} |
|
659 |
if (start + length > len) |
|
660 |
length = len - start; |
|
661 |
return jsSubstring(exec, s, static_cast<unsigned>(start), static_cast<unsigned>(length)); |
|
662 |
} |
|
663 |
||
664 |
JSValue JSC_HOST_CALL stringProtoFuncSubstring(ExecState* exec, JSObject*, JSValue thisValue, const ArgList& args) |
|
665 |
{ |
|
666 |
UString s = thisValue.toThisString(exec); |
|
667 |
int len = s.size(); |
|
668 |
||
669 |
JSValue a0 = args.at(0); |
|
670 |
JSValue a1 = args.at(1); |
|
671 |
||
672 |
double start = a0.toNumber(exec); |
|
673 |
double end = a1.toNumber(exec); |
|
674 |
if (isnan(start)) |
|
675 |
start = 0; |
|
676 |
if (isnan(end)) |
|
677 |
end = 0; |
|
678 |
if (start < 0) |
|
679 |
start = 0; |
|
680 |
if (end < 0) |
|
681 |
end = 0; |
|
682 |
if (start > len) |
|
683 |
start = len; |
|
684 |
if (end > len) |
|
685 |
end = len; |
|
686 |
if (a1.isUndefined()) |
|
687 |
end = len; |
|
688 |
if (start > end) { |
|
689 |
double temp = end; |
|
690 |
end = start; |
|
691 |
start = temp; |
|
692 |
} |
|
693 |
return jsSubstring(exec, s, static_cast<unsigned>(start), static_cast<unsigned>(end) - static_cast<unsigned>(start)); |
|
694 |
} |
|
695 |
||
696 |
JSValue JSC_HOST_CALL stringProtoFuncToLowerCase(ExecState* exec, JSObject*, JSValue thisValue, const ArgList&) |
|
697 |
{ |
|
698 |
JSString* sVal = thisValue.toThisJSString(exec); |
|
699 |
const UString& s = sVal->value(); |
|
700 |
||
701 |
int sSize = s.size(); |
|
702 |
if (!sSize) |
|
703 |
return sVal; |
|
704 |
||
705 |
const UChar* sData = s.data(); |
|
706 |
Vector<UChar> buffer(sSize); |
|
707 |
||
708 |
UChar ored = 0; |
|
709 |
for (int i = 0; i < sSize; i++) { |
|
710 |
UChar c = sData[i]; |
|
711 |
ored |= c; |
|
712 |
buffer[i] = toASCIILower(c); |
|
713 |
} |
|
714 |
if (!(ored & ~0x7f)) |
|
715 |
return jsString(exec, UString(buffer.releaseBuffer(), sSize, false)); |
|
716 |
||
717 |
bool error; |
|
718 |
int length = Unicode::toLower(buffer.data(), sSize, sData, sSize, &error); |
|
719 |
if (error) { |
|
720 |
buffer.resize(length); |
|
721 |
length = Unicode::toLower(buffer.data(), length, sData, sSize, &error); |
|
722 |
if (error) |
|
723 |
return sVal; |
|
724 |
} |
|
725 |
if (length == sSize && memcmp(buffer.data(), sData, length * sizeof(UChar)) == 0) |
|
726 |
return sVal; |
|
727 |
return jsString(exec, UString(buffer.releaseBuffer(), length, false)); |
|
728 |
} |
|
729 |
||
730 |
JSValue JSC_HOST_CALL stringProtoFuncToUpperCase(ExecState* exec, JSObject*, JSValue thisValue, const ArgList&) |
|
731 |
{ |
|
732 |
JSString* sVal = thisValue.toThisJSString(exec); |
|
733 |
const UString& s = sVal->value(); |
|
734 |
||
735 |
int sSize = s.size(); |
|
736 |
if (!sSize) |
|
737 |
return sVal; |
|
738 |
||
739 |
const UChar* sData = s.data(); |
|
740 |
Vector<UChar> buffer(sSize); |
|
741 |
||
742 |
UChar ored = 0; |
|
743 |
for (int i = 0; i < sSize; i++) { |
|
744 |
UChar c = sData[i]; |
|
745 |
ored |= c; |
|
746 |
buffer[i] = toASCIIUpper(c); |
|
747 |
} |
|
748 |
if (!(ored & ~0x7f)) |
|
749 |
return jsString(exec, UString(buffer.releaseBuffer(), sSize, false)); |
|
750 |
||
751 |
bool error; |
|
752 |
int length = Unicode::toUpper(buffer.data(), sSize, sData, sSize, &error); |
|
753 |
if (error) { |
|
754 |
buffer.resize(length); |
|
755 |
length = Unicode::toUpper(buffer.data(), length, sData, sSize, &error); |
|
756 |
if (error) |
|
757 |
return sVal; |
|
758 |
} |
|
759 |
if (length == sSize && memcmp(buffer.data(), sData, length * sizeof(UChar)) == 0) |
|
760 |
return sVal; |
|
761 |
return jsString(exec, UString(buffer.releaseBuffer(), length, false)); |
|
762 |
} |
|
763 |
||
764 |
JSValue JSC_HOST_CALL stringProtoFuncLocaleCompare(ExecState* exec, JSObject*, JSValue thisValue, const ArgList& args) |
|
765 |
{ |
|
766 |
if (args.size() < 1) |
|
767 |
return jsNumber(exec, 0); |
|
768 |
||
769 |
UString s = thisValue.toThisString(exec); |
|
770 |
JSValue a0 = args.at(0); |
|
771 |
return jsNumber(exec, localeCompare(s, a0.toString(exec))); |
|
772 |
} |
|
773 |
||
774 |
JSValue JSC_HOST_CALL stringProtoFuncBig(ExecState* exec, JSObject*, JSValue thisValue, const ArgList&) |
|
775 |
{ |
|
776 |
UString s = thisValue.toThisString(exec); |
|
777 |
return jsNontrivialString(exec, "<big>" + s + "</big>"); |
|
778 |
} |
|
779 |
||
780 |
JSValue JSC_HOST_CALL stringProtoFuncSmall(ExecState* exec, JSObject*, JSValue thisValue, const ArgList&) |
|
781 |
{ |
|
782 |
UString s = thisValue.toThisString(exec); |
|
783 |
return jsNontrivialString(exec, "<small>" + s + "</small>"); |
|
784 |
} |
|
785 |
||
786 |
JSValue JSC_HOST_CALL stringProtoFuncBlink(ExecState* exec, JSObject*, JSValue thisValue, const ArgList&) |
|
787 |
{ |
|
788 |
UString s = thisValue.toThisString(exec); |
|
789 |
return jsNontrivialString(exec, "<blink>" + s + "</blink>"); |
|
790 |
} |
|
791 |
||
792 |
JSValue JSC_HOST_CALL stringProtoFuncBold(ExecState* exec, JSObject*, JSValue thisValue, const ArgList&) |
|
793 |
{ |
|
794 |
UString s = thisValue.toThisString(exec); |
|
795 |
return jsNontrivialString(exec, "<b>" + s + "</b>"); |
|
796 |
} |
|
797 |
||
798 |
JSValue JSC_HOST_CALL stringProtoFuncFixed(ExecState* exec, JSObject*, JSValue thisValue, const ArgList&) |
|
799 |
{ |
|
800 |
UString s = thisValue.toThisString(exec); |
|
801 |
return jsString(exec, "<tt>" + s + "</tt>"); |
|
802 |
} |
|
803 |
||
804 |
JSValue JSC_HOST_CALL stringProtoFuncItalics(ExecState* exec, JSObject*, JSValue thisValue, const ArgList&) |
|
805 |
{ |
|
806 |
UString s = thisValue.toThisString(exec); |
|
807 |
return jsNontrivialString(exec, "<i>" + s + "</i>"); |
|
808 |
} |
|
809 |
||
810 |
JSValue JSC_HOST_CALL stringProtoFuncStrike(ExecState* exec, JSObject*, JSValue thisValue, const ArgList&) |
|
811 |
{ |
|
812 |
UString s = thisValue.toThisString(exec); |
|
813 |
return jsNontrivialString(exec, "<strike>" + s + "</strike>"); |
|
814 |
} |
|
815 |
||
816 |
JSValue JSC_HOST_CALL stringProtoFuncSub(ExecState* exec, JSObject*, JSValue thisValue, const ArgList&) |
|
817 |
{ |
|
818 |
UString s = thisValue.toThisString(exec); |
|
819 |
return jsNontrivialString(exec, "<sub>" + s + "</sub>"); |
|
820 |
} |
|
821 |
||
822 |
JSValue JSC_HOST_CALL stringProtoFuncSup(ExecState* exec, JSObject*, JSValue thisValue, const ArgList&) |
|
823 |
{ |
|
824 |
UString s = thisValue.toThisString(exec); |
|
825 |
return jsNontrivialString(exec, "<sup>" + s + "</sup>"); |
|
826 |
} |
|
827 |
||
828 |
JSValue JSC_HOST_CALL stringProtoFuncFontcolor(ExecState* exec, JSObject*, JSValue thisValue, const ArgList& args) |
|
829 |
{ |
|
830 |
UString s = thisValue.toThisString(exec); |
|
831 |
JSValue a0 = args.at(0); |
|
832 |
return jsNontrivialString(exec, "<font color=\"" + a0.toString(exec) + "\">" + s + "</font>"); |
|
833 |
} |
|
834 |
||
835 |
JSValue JSC_HOST_CALL stringProtoFuncFontsize(ExecState* exec, JSObject*, JSValue thisValue, const ArgList& args) |
|
836 |
{ |
|
837 |
UString s = thisValue.toThisString(exec); |
|
838 |
JSValue a0 = args.at(0); |
|
839 |
||
840 |
uint32_t smallInteger; |
|
841 |
if (a0.getUInt32(smallInteger) && smallInteger <= 9) { |
|
842 |
unsigned stringSize = s.size(); |
|
843 |
unsigned bufferSize = 22 + stringSize; |
|
844 |
UChar* buffer; |
|
845 |
if (!tryFastMalloc(bufferSize * sizeof(UChar)).getValue(buffer)) |
|
846 |
return jsUndefined(); |
|
847 |
buffer[0] = '<'; |
|
848 |
buffer[1] = 'f'; |
|
849 |
buffer[2] = 'o'; |
|
850 |
buffer[3] = 'n'; |
|
851 |
buffer[4] = 't'; |
|
852 |
buffer[5] = ' '; |
|
853 |
buffer[6] = 's'; |
|
854 |
buffer[7] = 'i'; |
|
855 |
buffer[8] = 'z'; |
|
856 |
buffer[9] = 'e'; |
|
857 |
buffer[10] = '='; |
|
858 |
buffer[11] = '"'; |
|
859 |
buffer[12] = '0' + smallInteger; |
|
860 |
buffer[13] = '"'; |
|
861 |
buffer[14] = '>'; |
|
862 |
memcpy(&buffer[15], s.data(), stringSize * sizeof(UChar)); |
|
863 |
buffer[15 + stringSize] = '<'; |
|
864 |
buffer[16 + stringSize] = '/'; |
|
865 |
buffer[17 + stringSize] = 'f'; |
|
866 |
buffer[18 + stringSize] = 'o'; |
|
867 |
buffer[19 + stringSize] = 'n'; |
|
868 |
buffer[20 + stringSize] = 't'; |
|
869 |
buffer[21 + stringSize] = '>'; |
|
870 |
return jsNontrivialString(exec, UString(buffer, bufferSize, false)); |
|
871 |
} |
|
872 |
||
873 |
return jsNontrivialString(exec, "<font size=\"" + a0.toString(exec) + "\">" + s + "</font>"); |
|
874 |
} |
|
875 |
||
876 |
JSValue JSC_HOST_CALL stringProtoFuncAnchor(ExecState* exec, JSObject*, JSValue thisValue, const ArgList& args) |
|
877 |
{ |
|
878 |
UString s = thisValue.toThisString(exec); |
|
879 |
JSValue a0 = args.at(0); |
|
880 |
return jsNontrivialString(exec, "<a name=\"" + a0.toString(exec) + "\">" + s + "</a>"); |
|
881 |
} |
|
882 |
||
883 |
JSValue JSC_HOST_CALL stringProtoFuncLink(ExecState* exec, JSObject*, JSValue thisValue, const ArgList& args) |
|
884 |
{ |
|
885 |
UString s = thisValue.toThisString(exec); |
|
886 |
JSValue a0 = args.at(0); |
|
887 |
UString linkText = a0.toString(exec); |
|
888 |
||
889 |
unsigned linkTextSize = linkText.size(); |
|
890 |
unsigned stringSize = s.size(); |
|
891 |
unsigned bufferSize = 15 + linkTextSize + stringSize; |
|
892 |
UChar* buffer; |
|
893 |
if (!tryFastMalloc(bufferSize * sizeof(UChar)).getValue(buffer)) |
|
894 |
return jsUndefined(); |
|
895 |
buffer[0] = '<'; |
|
896 |
buffer[1] = 'a'; |
|
897 |
buffer[2] = ' '; |
|
898 |
buffer[3] = 'h'; |
|
899 |
buffer[4] = 'r'; |
|
900 |
buffer[5] = 'e'; |
|
901 |
buffer[6] = 'f'; |
|
902 |
buffer[7] = '='; |
|
903 |
buffer[8] = '"'; |
|
904 |
memcpy(&buffer[9], linkText.data(), linkTextSize * sizeof(UChar)); |
|
905 |
buffer[9 + linkTextSize] = '"'; |
|
906 |
buffer[10 + linkTextSize] = '>'; |
|
907 |
memcpy(&buffer[11 + linkTextSize], s.data(), stringSize * sizeof(UChar)); |
|
908 |
buffer[11 + linkTextSize + stringSize] = '<'; |
|
909 |
buffer[12 + linkTextSize + stringSize] = '/'; |
|
910 |
buffer[13 + linkTextSize + stringSize] = 'a'; |
|
911 |
buffer[14 + linkTextSize + stringSize] = '>'; |
|
912 |
return jsNontrivialString(exec, UString(buffer, bufferSize, false)); |
|
913 |
} |
|
914 |
||
915 |
enum { |
|
916 |
TrimLeft = 1, |
|
917 |
TrimRight = 2 |
|
918 |
}; |
|
919 |
||
920 |
static inline bool isTrimWhitespace(UChar c) |
|
921 |
{ |
|
922 |
return isStrWhiteSpace(c) || c == 0x200b; |
|
923 |
} |
|
924 |
||
925 |
static inline JSValue trimString(ExecState* exec, JSValue thisValue, int trimKind) |
|
926 |
{ |
|
927 |
UString str = thisValue.toThisString(exec); |
|
928 |
int left = 0; |
|
929 |
if (trimKind & TrimLeft) { |
|
930 |
while (left < str.size() && isTrimWhitespace(str[left])) |
|
931 |
left++; |
|
932 |
} |
|
933 |
int right = str.size(); |
|
934 |
if (trimKind & TrimRight) { |
|
935 |
while (right > left && isTrimWhitespace(str[right - 1])) |
|
936 |
right--; |
|
937 |
} |
|
938 |
||
939 |
// Don't gc allocate a new string if we don't have to. |
|
940 |
if (left == 0 && right == str.size() && thisValue.isString()) |
|
941 |
return thisValue; |
|
942 |
||
943 |
return jsString(exec, str.substr(left, right - left)); |
|
944 |
} |
|
945 |
||
946 |
JSValue JSC_HOST_CALL stringProtoFuncTrim(ExecState* exec, JSObject*, JSValue thisValue, const ArgList&) |
|
947 |
{ |
|
948 |
return trimString(exec, thisValue, TrimLeft | TrimRight); |
|
949 |
} |
|
950 |
||
951 |
JSValue JSC_HOST_CALL stringProtoFuncTrimLeft(ExecState* exec, JSObject*, JSValue thisValue, const ArgList&) |
|
952 |
{ |
|
953 |
return trimString(exec, thisValue, TrimLeft); |
|
954 |
} |
|
955 |
||
956 |
JSValue JSC_HOST_CALL stringProtoFuncTrimRight(ExecState* exec, JSObject*, JSValue thisValue, const ArgList&) |
|
957 |
{ |
|
958 |
return trimString(exec, thisValue, TrimRight); |
|
959 |
} |
|
960 |
||
961 |
||
962 |
} // namespace JSC |