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.
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.
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.
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
23 #include "StringPrototype.h"
25 #include "CachedCall.h"
27 #include "Executable.h"
28 #include "JSGlobalObjectFunctions.h"
30 #include "JSFunction.h"
31 #include "JSStringBuilder.h"
33 #include "ObjectPrototype.h"
34 #include "Operations.h"
35 #include "PropertyNameArray.h"
36 #include "RegExpCache.h"
37 #include "RegExpConstructor.h"
38 #include "RegExpObject.h"
39 #include <wtf/ASCIICType.h>
40 #include <wtf/MathExtras.h>
41 #include <wtf/unicode/Collator.h>
47 ASSERT_CLASS_FITS_IN_CELL(StringPrototype);
49 static EncodedJSValue JSC_HOST_CALL stringProtoFuncToString(ExecState*);
50 static EncodedJSValue JSC_HOST_CALL stringProtoFuncCharAt(ExecState*);
51 static EncodedJSValue JSC_HOST_CALL stringProtoFuncCharCodeAt(ExecState*);
52 static EncodedJSValue JSC_HOST_CALL stringProtoFuncConcat(ExecState*);
53 static EncodedJSValue JSC_HOST_CALL stringProtoFuncIndexOf(ExecState*);
54 static EncodedJSValue JSC_HOST_CALL stringProtoFuncLastIndexOf(ExecState*);
55 static EncodedJSValue JSC_HOST_CALL stringProtoFuncMatch(ExecState*);
56 static EncodedJSValue JSC_HOST_CALL stringProtoFuncReplace(ExecState*);
57 static EncodedJSValue JSC_HOST_CALL stringProtoFuncSearch(ExecState*);
58 static EncodedJSValue JSC_HOST_CALL stringProtoFuncSlice(ExecState*);
59 static EncodedJSValue JSC_HOST_CALL stringProtoFuncSplit(ExecState*);
60 static EncodedJSValue JSC_HOST_CALL stringProtoFuncSubstr(ExecState*);
61 static EncodedJSValue JSC_HOST_CALL stringProtoFuncSubstring(ExecState*);
62 static EncodedJSValue JSC_HOST_CALL stringProtoFuncToLowerCase(ExecState*);
63 static EncodedJSValue JSC_HOST_CALL stringProtoFuncToUpperCase(ExecState*);
64 static EncodedJSValue JSC_HOST_CALL stringProtoFuncLocaleCompare(ExecState*);
65 static EncodedJSValue JSC_HOST_CALL stringProtoFuncBig(ExecState*);
66 static EncodedJSValue JSC_HOST_CALL stringProtoFuncSmall(ExecState*);
67 static EncodedJSValue JSC_HOST_CALL stringProtoFuncBlink(ExecState*);
68 static EncodedJSValue JSC_HOST_CALL stringProtoFuncBold(ExecState*);
69 static EncodedJSValue JSC_HOST_CALL stringProtoFuncFixed(ExecState*);
70 static EncodedJSValue JSC_HOST_CALL stringProtoFuncItalics(ExecState*);
71 static EncodedJSValue JSC_HOST_CALL stringProtoFuncStrike(ExecState*);
72 static EncodedJSValue JSC_HOST_CALL stringProtoFuncSub(ExecState*);
73 static EncodedJSValue JSC_HOST_CALL stringProtoFuncSup(ExecState*);
74 static EncodedJSValue JSC_HOST_CALL stringProtoFuncFontcolor(ExecState*);
75 static EncodedJSValue JSC_HOST_CALL stringProtoFuncFontsize(ExecState*);
76 static EncodedJSValue JSC_HOST_CALL stringProtoFuncAnchor(ExecState*);
77 static EncodedJSValue JSC_HOST_CALL stringProtoFuncLink(ExecState*);
78 static EncodedJSValue JSC_HOST_CALL stringProtoFuncTrim(ExecState*);
79 static EncodedJSValue JSC_HOST_CALL stringProtoFuncTrimLeft(ExecState*);
80 static EncodedJSValue JSC_HOST_CALL stringProtoFuncTrimRight(ExecState*);
84 #include "StringPrototype.lut.h"
88 const ClassInfo StringPrototype::s_info = { "String", &StringObject::s_info, 0, ExecState::stringTable };
90 /* Source for StringPrototype.lut.h
92 toString stringProtoFuncToString DontEnum|Function 0
93 valueOf stringProtoFuncToString DontEnum|Function 0
94 charAt stringProtoFuncCharAt DontEnum|Function 1
95 charCodeAt stringProtoFuncCharCodeAt DontEnum|Function 1
96 concat stringProtoFuncConcat DontEnum|Function 1
97 indexOf stringProtoFuncIndexOf DontEnum|Function 1
98 lastIndexOf stringProtoFuncLastIndexOf DontEnum|Function 1
99 match stringProtoFuncMatch DontEnum|Function 1
100 replace stringProtoFuncReplace DontEnum|Function 2
101 search stringProtoFuncSearch DontEnum|Function 1
102 slice stringProtoFuncSlice DontEnum|Function 2
103 split stringProtoFuncSplit DontEnum|Function 2
104 substr stringProtoFuncSubstr DontEnum|Function 2
105 substring stringProtoFuncSubstring DontEnum|Function 2
106 toLowerCase stringProtoFuncToLowerCase DontEnum|Function 0
107 toUpperCase stringProtoFuncToUpperCase DontEnum|Function 0
108 localeCompare stringProtoFuncLocaleCompare DontEnum|Function 1
110 # toLocaleLowerCase and toLocaleUpperCase are currently identical to toLowerCase and toUpperCase
111 toLocaleLowerCase stringProtoFuncToLowerCase DontEnum|Function 0
112 toLocaleUpperCase stringProtoFuncToUpperCase DontEnum|Function 0
114 big stringProtoFuncBig DontEnum|Function 0
115 small stringProtoFuncSmall DontEnum|Function 0
116 blink stringProtoFuncBlink DontEnum|Function 0
117 bold stringProtoFuncBold DontEnum|Function 0
118 fixed stringProtoFuncFixed DontEnum|Function 0
119 italics stringProtoFuncItalics DontEnum|Function 0
120 strike stringProtoFuncStrike DontEnum|Function 0
121 sub stringProtoFuncSub DontEnum|Function 0
122 sup stringProtoFuncSup DontEnum|Function 0
123 fontcolor stringProtoFuncFontcolor DontEnum|Function 1
124 fontsize stringProtoFuncFontsize DontEnum|Function 1
125 anchor stringProtoFuncAnchor DontEnum|Function 1
126 link stringProtoFuncLink DontEnum|Function 1
127 trim stringProtoFuncTrim DontEnum|Function 0
128 trimLeft stringProtoFuncTrimLeft DontEnum|Function 0
129 trimRight stringProtoFuncTrimRight DontEnum|Function 0
134 StringPrototype::StringPrototype(ExecState* exec, JSGlobalObject* globalObject, Structure* structure)
135 : StringObject(exec, structure)
137 ASSERT(inherits(&s_info));
139 putAnonymousValue(exec->globalData(), 0, globalObject);
140 // The constructor will be added later, after StringConstructor has been built
141 putDirectWithoutTransition(exec->globalData(), exec->propertyNames().length, jsNumber(0), DontDelete | ReadOnly | DontEnum);
144 bool StringPrototype::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot &slot)
146 return getStaticFunctionSlot<StringObject>(exec, ExecState::stringTable(exec), this, propertyName, slot);
149 bool StringPrototype::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
151 return getStaticFunctionDescriptor<StringObject>(exec, ExecState::stringTable(exec), this, propertyName, descriptor);
154 // ------------------------------ Functions --------------------------
156 static NEVER_INLINE UString substituteBackreferencesSlow(const UString& replacement, const UString& source, const int* ovector, RegExp* reg, size_t i)
158 Vector<UChar> substitutedReplacement;
161 if (i + 1 == replacement.length())
164 UChar ref = replacement[i + 1];
168 substitutedReplacement.append(replacement.characters() + offset, i - offset);
177 backrefStart = ovector[0];
178 backrefLength = ovector[1] - backrefStart;
179 } else if (ref == '`') {
181 backrefLength = ovector[0];
182 } else if (ref == '\'') {
183 backrefStart = ovector[1];
184 backrefLength = source.length() - backrefStart;
185 } else if (reg && ref >= '0' && ref <= '9') {
186 // 1- and 2-digit back references are allowed
187 unsigned backrefIndex = ref - '0';
188 if (backrefIndex > reg->numSubpatterns())
190 if (replacement.length() > i + 2) {
191 ref = replacement[i + 2];
192 if (ref >= '0' && ref <= '9') {
193 backrefIndex = 10 * backrefIndex + ref - '0';
194 if (backrefIndex > reg->numSubpatterns())
195 backrefIndex = backrefIndex / 10; // Fall back to the 1-digit reference
202 backrefStart = ovector[2 * backrefIndex];
203 backrefLength = ovector[2 * backrefIndex + 1] - backrefStart;
208 substitutedReplacement.append(replacement.characters() + offset, i - offset);
211 if (backrefStart >= 0)
212 substitutedReplacement.append(source.characters() + backrefStart, backrefLength);
213 } while ((i = replacement.find('$', i + 1)) != notFound);
215 if (replacement.length() - offset)
216 substitutedReplacement.append(replacement.characters() + offset, replacement.length() - offset);
218 substitutedReplacement.shrinkToFit();
219 return UString::adopt(substitutedReplacement);
222 static inline UString substituteBackreferences(const UString& replacement, const UString& source, const int* ovector, RegExp* reg)
224 size_t i = replacement.find('$', 0);
225 if (UNLIKELY(i != notFound))
226 return substituteBackreferencesSlow(replacement, source, ovector, reg, i);
230 static inline int localeCompare(const UString& a, const UString& b)
232 return Collator::userDefault()->collate(reinterpret_cast<const ::UChar*>(a.characters()), a.length(), reinterpret_cast<const ::UChar*>(b.characters()), b.length());
237 StringRange(int pos, int len)
251 static ALWAYS_INLINE JSValue jsSpliceSubstringsWithSeparators(ExecState* exec, JSString* sourceVal, const UString& source, const StringRange* substringRanges, int rangeCount, const UString* separators, int separatorCount)
253 if (rangeCount == 1 && separatorCount == 0) {
254 int sourceSize = source.length();
255 int position = substringRanges[0].position;
256 int length = substringRanges[0].length;
257 if (position <= 0 && length >= sourceSize)
259 // We could call UString::substr, but this would result in redundant checks
260 return jsString(exec, StringImpl::create(source.impl(), max(0, position), min(sourceSize, length)));
264 for (int i = 0; i < rangeCount; i++)
265 totalLength += substringRanges[i].length;
266 for (int i = 0; i < separatorCount; i++)
267 totalLength += separators[i].length();
269 if (totalLength == 0)
270 return jsString(exec, "");
273 PassRefPtr<StringImpl> impl = StringImpl::tryCreateUninitialized(totalLength, buffer);
275 return throwOutOfMemoryError(exec);
277 int maxCount = max(rangeCount, separatorCount);
279 for (int i = 0; i < maxCount; i++) {
280 if (i < rangeCount) {
281 if (int srcLen = substringRanges[i].length) {
282 StringImpl::copyChars(buffer + bufferPos, source.characters() + substringRanges[i].position, srcLen);
286 if (i < separatorCount) {
287 if (int sepLen = separators[i].length()) {
288 StringImpl::copyChars(buffer + bufferPos, separators[i].characters(), sepLen);
294 return jsString(exec, impl);
297 EncodedJSValue JSC_HOST_CALL stringProtoFuncReplace(ExecState* exec)
299 JSValue thisValue = exec->hostThisValue();
300 JSString* sourceVal = thisValue.toThisJSString(exec);
301 JSValue pattern = exec->argument(0);
302 JSValue replacement = exec->argument(1);
304 UString replacementString;
306 CallType callType = getCallData(replacement, callData);
307 if (callType == CallTypeNone)
308 replacementString = replacement.toString(exec);
310 if (pattern.inherits(&RegExpObject::s_info)) {
311 const UString& source = sourceVal->value(exec);
312 unsigned sourceLen = source.length();
313 if (exec->hadException())
314 return JSValue::encode(JSValue());
315 RegExp* reg = asRegExpObject(pattern)->regExp();
316 bool global = reg->global();
318 RegExpConstructor* regExpConstructor = exec->lexicalGlobalObject()->regExpConstructor();
321 unsigned startPosition = 0;
323 Vector<StringRange, 16> sourceRanges;
324 Vector<UString, 16> replacements;
326 // This is either a loop (if global is set) or a one-way (if not).
327 if (global && callType == CallTypeJS) {
328 // reg->numSubpatterns() + 1 for pattern args, + 2 for match start and sourceValue
329 int argCount = reg->numSubpatterns() + 1 + 2;
330 JSFunction* func = asFunction(replacement);
331 CachedCall cachedCall(exec, func, argCount);
332 if (exec->hadException())
333 return JSValue::encode(jsNull());
338 regExpConstructor->performMatch(reg, source, startPosition, matchIndex, matchLen, &ovector);
342 sourceRanges.append(StringRange(lastIndex, matchIndex - lastIndex));
344 int completeMatchStart = ovector[0];
346 for (; i < reg->numSubpatterns() + 1; ++i) {
347 int matchStart = ovector[i * 2];
348 int matchLen = ovector[i * 2 + 1] - matchStart;
351 cachedCall.setArgument(i, jsUndefined());
353 cachedCall.setArgument(i, jsSubstring(exec, source, matchStart, matchLen));
356 cachedCall.setArgument(i++, jsNumber(completeMatchStart));
357 cachedCall.setArgument(i++, sourceVal);
359 cachedCall.setThis(exec->globalThisValue());
360 JSValue result = cachedCall.call();
361 if (LIKELY(result.isString()))
362 replacements.append(asString(result)->value(exec));
364 replacements.append(result.toString(cachedCall.newCallFrame(exec)));
365 if (exec->hadException())
368 lastIndex = matchIndex + matchLen;
369 startPosition = lastIndex;
371 // special case of empty match
374 if (startPosition > sourceLen)
383 regExpConstructor->performMatch(reg, source, startPosition, matchIndex, matchLen, &ovector);
387 if (callType != CallTypeNone) {
388 sourceRanges.append(StringRange(lastIndex, matchIndex - lastIndex));
390 int completeMatchStart = ovector[0];
391 MarkedArgumentBuffer args;
393 for (unsigned i = 0; i < reg->numSubpatterns() + 1; ++i) {
394 int matchStart = ovector[i * 2];
395 int matchLen = ovector[i * 2 + 1] - matchStart;
398 args.append(jsUndefined());
400 args.append(jsSubstring(exec, source, matchStart, matchLen));
403 args.append(jsNumber(completeMatchStart));
404 args.append(sourceVal);
406 replacements.append(call(exec, replacement, callType, callData, exec->globalThisValue(), args).toString(exec));
407 if (exec->hadException())
410 int replLen = replacementString.length();
411 if (lastIndex < matchIndex || replLen) {
412 sourceRanges.append(StringRange(lastIndex, matchIndex - lastIndex));
415 replacements.append(substituteBackreferences(replacementString, source, ovector, reg));
417 replacements.append(UString());
421 lastIndex = matchIndex + matchLen;
422 startPosition = lastIndex;
424 // special case of empty match
427 if (startPosition > sourceLen)
433 if (!lastIndex && replacements.isEmpty())
434 return JSValue::encode(sourceVal);
436 if (static_cast<unsigned>(lastIndex) < sourceLen)
437 sourceRanges.append(StringRange(lastIndex, sourceLen - lastIndex));
439 return JSValue::encode(jsSpliceSubstringsWithSeparators(exec, sourceVal, source, sourceRanges.data(), sourceRanges.size(), replacements.data(), replacements.size()));
442 // Not a regular expression, so treat the pattern as a string.
444 UString patternString = pattern.toString(exec);
445 // Special case for single character patterns without back reference replacement
446 if (patternString.length() == 1 && callType == CallTypeNone && replacementString.find('$', 0) == notFound)
447 return JSValue::encode(sourceVal->replaceCharacter(exec, patternString[0], replacementString));
449 const UString& source = sourceVal->value(exec);
450 size_t matchPos = source.find(patternString);
452 if (matchPos == notFound)
453 return JSValue::encode(sourceVal);
455 int matchLen = patternString.length();
456 if (callType != CallTypeNone) {
457 MarkedArgumentBuffer args;
458 args.append(jsSubstring(exec, source, matchPos, matchLen));
459 args.append(jsNumber(matchPos));
460 args.append(sourceVal);
462 replacementString = call(exec, replacement, callType, callData, exec->globalThisValue(), args).toString(exec);
465 size_t matchEnd = matchPos + matchLen;
466 int ovector[2] = { matchPos, matchEnd };
467 return JSValue::encode(jsString(exec, source.substringSharingImpl(0, matchPos), substituteBackreferences(replacementString, source, ovector, 0), source.substringSharingImpl(matchEnd)));
470 EncodedJSValue JSC_HOST_CALL stringProtoFuncToString(ExecState* exec)
472 JSValue thisValue = exec->hostThisValue();
473 // Also used for valueOf.
475 if (thisValue.isString())
476 return JSValue::encode(thisValue);
478 if (thisValue.inherits(&StringObject::s_info))
479 return JSValue::encode(asStringObject(thisValue)->internalValue());
481 return throwVMTypeError(exec);
484 EncodedJSValue JSC_HOST_CALL stringProtoFuncCharAt(ExecState* exec)
486 JSValue thisValue = exec->hostThisValue();
487 if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
488 return throwVMTypeError(exec);
489 UString s = thisValue.toThisString(exec);
490 unsigned len = s.length();
491 JSValue a0 = exec->argument(0);
493 uint32_t i = a0.asUInt32();
495 return JSValue::encode(jsSingleCharacterSubstring(exec, s, i));
496 return JSValue::encode(jsEmptyString(exec));
498 double dpos = a0.toInteger(exec);
499 if (dpos >= 0 && dpos < len)
500 return JSValue::encode(jsSingleCharacterSubstring(exec, s, static_cast<unsigned>(dpos)));
501 return JSValue::encode(jsEmptyString(exec));
504 EncodedJSValue JSC_HOST_CALL stringProtoFuncCharCodeAt(ExecState* exec)
506 JSValue thisValue = exec->hostThisValue();
507 if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
508 return throwVMTypeError(exec);
509 UString s = thisValue.toThisString(exec);
510 unsigned len = s.length();
511 JSValue a0 = exec->argument(0);
513 uint32_t i = a0.asUInt32();
515 return JSValue::encode(jsNumber(s.characters()[i]));
516 return JSValue::encode(jsNaN());
518 double dpos = a0.toInteger(exec);
519 if (dpos >= 0 && dpos < len)
520 return JSValue::encode(jsNumber(s[static_cast<int>(dpos)]));
521 return JSValue::encode(jsNaN());
524 EncodedJSValue JSC_HOST_CALL stringProtoFuncConcat(ExecState* exec)
526 JSValue thisValue = exec->hostThisValue();
527 if (thisValue.isString() && (exec->argumentCount() == 1)) {
528 JSValue v = exec->argument(0);
529 return JSValue::encode(v.isString()
530 ? jsString(exec, asString(thisValue), asString(v))
531 : jsString(exec, asString(thisValue), v.toString(exec)));
533 if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
534 return throwVMTypeError(exec);
535 return JSValue::encode(jsString(exec, thisValue));
538 EncodedJSValue JSC_HOST_CALL stringProtoFuncIndexOf(ExecState* exec)
540 JSValue thisValue = exec->hostThisValue();
541 if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
542 return throwVMTypeError(exec);
543 UString s = thisValue.toThisString(exec);
544 int len = s.length();
546 JSValue a0 = exec->argument(0);
547 JSValue a1 = exec->argument(1);
548 UString u2 = a0.toString(exec);
550 if (a1.isUndefined())
552 else if (a1.isUInt32())
553 pos = min<uint32_t>(a1.asUInt32(), len);
555 double dpos = a1.toInteger(exec);
560 pos = static_cast<int>(dpos);
563 size_t result = s.find(u2, pos);
564 if (result == notFound)
565 return JSValue::encode(jsNumber(-1));
566 return JSValue::encode(jsNumber(result));
569 EncodedJSValue JSC_HOST_CALL stringProtoFuncLastIndexOf(ExecState* exec)
571 JSValue thisValue = exec->hostThisValue();
572 if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
573 return throwVMTypeError(exec);
574 UString s = thisValue.toThisString(exec);
575 int len = s.length();
577 JSValue a0 = exec->argument(0);
578 JSValue a1 = exec->argument(1);
580 UString u2 = a0.toString(exec);
581 double dpos = a1.toIntegerPreserveNaN(exec);
584 else if (!(dpos <= len)) // true for NaN
587 // Work around for broken NaN compare operator
588 else if (isnan(dpos))
592 size_t result = s.reverseFind(u2, static_cast<unsigned>(dpos));
593 if (result == notFound)
594 return JSValue::encode(jsNumber(-1));
595 return JSValue::encode(jsNumber(result));
598 EncodedJSValue JSC_HOST_CALL stringProtoFuncMatch(ExecState* exec)
600 JSValue thisValue = exec->hostThisValue();
601 if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
602 return throwVMTypeError(exec);
603 UString s = thisValue.toThisString(exec);
605 JSValue a0 = exec->argument(0);
608 if (a0.inherits(&RegExpObject::s_info))
609 reg = asRegExpObject(a0)->regExp();
612 * ECMA 15.5.4.12 String.prototype.search (regexp)
613 * If regexp is not an object whose [[Class]] property is "RegExp", it is
614 * replaced with the result of the expression new RegExp(regexp).
616 reg = exec->globalData().regExpCache()->lookupOrCreate(a0.toString(exec), NoFlags);
618 RegExpConstructor* regExpConstructor = exec->lexicalGlobalObject()->regExpConstructor();
621 regExpConstructor->performMatch(reg.get(), s, 0, pos, matchLength);
622 if (!(reg->global())) {
623 // case without 'g' flag is handled like RegExp.prototype.exec
625 return JSValue::encode(jsNull());
626 return JSValue::encode(regExpConstructor->arrayOfMatches(exec));
629 // return array of matches
630 MarkedArgumentBuffer list;
631 unsigned lastIndex = 0;
633 list.append(jsSubstring(exec, s, pos, matchLength));
635 pos += matchLength == 0 ? 1 : matchLength;
636 regExpConstructor->performMatch(reg.get(), s, pos, pos, matchLength);
638 if (list.isEmpty()) {
639 // if there are no matches at all, it's important to return
640 // Null instead of an empty array, because this matches
641 // other browsers and because Null is a false value.
642 return JSValue::encode(jsNull());
645 return JSValue::encode(constructArray(exec, list));
648 EncodedJSValue JSC_HOST_CALL stringProtoFuncSearch(ExecState* exec)
650 JSValue thisValue = exec->hostThisValue();
651 if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
652 return throwVMTypeError(exec);
653 UString s = thisValue.toThisString(exec);
655 JSValue a0 = exec->argument(0);
658 if (a0.inherits(&RegExpObject::s_info))
659 reg = asRegExpObject(a0)->regExp();
662 * ECMA 15.5.4.12 String.prototype.search (regexp)
663 * If regexp is not an object whose [[Class]] property is "RegExp", it is
664 * replaced with the result of the expression new RegExp(regexp).
666 reg = exec->globalData().regExpCache()->lookupOrCreate(a0.toString(exec), NoFlags);
668 RegExpConstructor* regExpConstructor = exec->lexicalGlobalObject()->regExpConstructor();
671 regExpConstructor->performMatch(reg.get(), s, 0, pos, matchLength);
672 return JSValue::encode(jsNumber(pos));
675 EncodedJSValue JSC_HOST_CALL stringProtoFuncSlice(ExecState* exec)
677 JSValue thisValue = exec->hostThisValue();
678 if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
679 return throwVMTypeError(exec);
680 UString s = thisValue.toThisString(exec);
681 int len = s.length();
683 JSValue a0 = exec->argument(0);
684 JSValue a1 = exec->argument(1);
686 // The arg processing is very much like ArrayProtoFunc::Slice
687 double start = a0.toInteger(exec);
688 double end = a1.isUndefined() ? len : a1.toInteger(exec);
689 double from = start < 0 ? len + start : start;
690 double to = end < 0 ? len + end : end;
691 if (to > from && to > 0 && from < len) {
696 return JSValue::encode(jsSubstring(exec, s, static_cast<unsigned>(from), static_cast<unsigned>(to) - static_cast<unsigned>(from)));
699 return JSValue::encode(jsEmptyString(exec));
702 EncodedJSValue JSC_HOST_CALL stringProtoFuncSplit(ExecState* exec)
704 JSValue thisValue = exec->hostThisValue();
705 if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
706 return throwVMTypeError(exec);
707 UString s = thisValue.toThisString(exec);
709 JSValue a0 = exec->argument(0);
710 JSValue a1 = exec->argument(1);
712 JSArray* result = constructEmptyArray(exec);
715 unsigned limit = a1.isUndefined() ? 0xFFFFFFFFU : a1.toUInt32(exec);
716 if (a0.inherits(&RegExpObject::s_info)) {
717 RegExp* reg = asRegExpObject(a0)->regExp();
718 if (s.isEmpty() && reg->match(s, 0) >= 0) {
719 // empty string matched by regexp -> empty array
720 return JSValue::encode(result);
723 while (i != limit && pos < s.length()) {
724 Vector<int, 32> ovector;
725 int mpos = reg->match(s, pos, &ovector);
728 int mlen = ovector[1] - ovector[0];
729 pos = mpos + (mlen == 0 ? 1 : mlen);
730 if (static_cast<unsigned>(mpos) != p0 || mlen) {
731 result->put(exec, i++, jsSubstring(exec, s, p0, mpos - p0));
734 for (unsigned si = 1; si <= reg->numSubpatterns(); ++si) {
735 int spos = ovector[si * 2];
737 result->put(exec, i++, jsUndefined());
739 result->put(exec, i++, jsSubstring(exec, s, spos, ovector[si * 2 + 1] - spos));
743 UString u2 = a0.toString(exec);
746 // empty separator matches empty string -> empty array
747 return JSValue::encode(result);
749 while (i != limit && p0 < s.length() - 1)
750 result->put(exec, i++, jsSingleCharacterSubstring(exec, s, p0++));
753 while (i != limit && (pos = s.find(u2, p0)) != notFound) {
754 result->put(exec, i++, jsSubstring(exec, s, p0, pos - p0));
755 p0 = pos + u2.length();
760 // add remaining string
762 result->put(exec, i++, jsSubstring(exec, s, p0, s.length() - p0));
764 return JSValue::encode(result);
767 EncodedJSValue JSC_HOST_CALL stringProtoFuncSubstr(ExecState* exec)
769 JSValue thisValue = exec->hostThisValue();
770 if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
771 return throwVMTypeError(exec);
773 JSString* jsString = 0;
775 if (thisValue.isString()) {
776 jsString = static_cast<JSString*>(thisValue.asCell());
777 len = jsString->length();
779 uString = thisValue.toThisObject(exec)->toString(exec);
780 len = uString.length();
783 JSValue a0 = exec->argument(0);
784 JSValue a1 = exec->argument(1);
786 double start = a0.toInteger(exec);
787 double length = a1.isUndefined() ? len : a1.toInteger(exec);
788 if (start >= len || length <= 0)
789 return JSValue::encode(jsEmptyString(exec));
795 if (start + length > len)
796 length = len - start;
797 unsigned substringStart = static_cast<unsigned>(start);
798 unsigned substringLength = static_cast<unsigned>(length);
800 return JSValue::encode(jsSubstring(exec, jsString, substringStart, substringLength));
801 return JSValue::encode(jsSubstring(exec, uString, substringStart, substringLength));
804 EncodedJSValue JSC_HOST_CALL stringProtoFuncSubstring(ExecState* exec)
806 JSValue thisValue = exec->hostThisValue();
807 if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
808 return throwVMTypeError(exec);
810 JSString* jsString = 0;
812 if (thisValue.isString()) {
813 jsString = static_cast<JSString*>(thisValue.asCell());
814 len = jsString->length();
816 uString = thisValue.toThisObject(exec)->toString(exec);
817 len = uString.length();
820 JSValue a0 = exec->argument(0);
821 JSValue a1 = exec->argument(1);
823 double start = a0.toNumber(exec);
825 if (!(start >= 0)) // check for negative values or NaN
827 else if (start > len)
829 if (a1.isUndefined())
832 end = a1.toNumber(exec);
833 if (!(end >= 0)) // check for negative values or NaN
843 unsigned substringStart = static_cast<unsigned>(start);
844 unsigned substringLength = static_cast<unsigned>(end) - substringStart;
846 return JSValue::encode(jsSubstring(exec, jsString, substringStart, substringLength));
847 return JSValue::encode(jsSubstring(exec, uString, substringStart, substringLength));
850 EncodedJSValue JSC_HOST_CALL stringProtoFuncToLowerCase(ExecState* exec)
852 JSValue thisValue = exec->hostThisValue();
853 if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
854 return throwVMTypeError(exec);
855 JSString* sVal = thisValue.toThisJSString(exec);
856 const UString& s = sVal->value(exec);
858 int sSize = s.length();
860 return JSValue::encode(sVal);
862 const UChar* sData = s.characters();
863 Vector<UChar> buffer(sSize);
866 for (int i = 0; i < sSize; i++) {
869 buffer[i] = toASCIILower(c);
872 return JSValue::encode(jsString(exec, UString::adopt(buffer)));
875 int length = Unicode::toLower(buffer.data(), sSize, sData, sSize, &error);
877 buffer.resize(length);
878 length = Unicode::toLower(buffer.data(), length, sData, sSize, &error);
880 return JSValue::encode(sVal);
882 if (length == sSize) {
883 if (memcmp(buffer.data(), sData, length * sizeof(UChar)) == 0)
884 return JSValue::encode(sVal);
886 buffer.resize(length);
887 return JSValue::encode(jsString(exec, UString::adopt(buffer)));
890 EncodedJSValue JSC_HOST_CALL stringProtoFuncToUpperCase(ExecState* exec)
892 JSValue thisValue = exec->hostThisValue();
893 if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
894 return throwVMTypeError(exec);
895 JSString* sVal = thisValue.toThisJSString(exec);
896 const UString& s = sVal->value(exec);
898 int sSize = s.length();
900 return JSValue::encode(sVal);
902 const UChar* sData = s.characters();
903 Vector<UChar> buffer(sSize);
906 for (int i = 0; i < sSize; i++) {
909 buffer[i] = toASCIIUpper(c);
912 return JSValue::encode(jsString(exec, UString::adopt(buffer)));
915 int length = Unicode::toUpper(buffer.data(), sSize, sData, sSize, &error);
917 buffer.resize(length);
918 length = Unicode::toUpper(buffer.data(), length, sData, sSize, &error);
920 return JSValue::encode(sVal);
922 if (length == sSize) {
923 if (memcmp(buffer.data(), sData, length * sizeof(UChar)) == 0)
924 return JSValue::encode(sVal);
926 buffer.resize(length);
927 return JSValue::encode(jsString(exec, UString::adopt(buffer)));
930 EncodedJSValue JSC_HOST_CALL stringProtoFuncLocaleCompare(ExecState* exec)
932 if (exec->argumentCount() < 1)
933 return JSValue::encode(jsNumber(0));
935 JSValue thisValue = exec->hostThisValue();
936 if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
937 return throwVMTypeError(exec);
939 UString s = thisValue.toThisString(exec);
940 JSValue a0 = exec->argument(0);
941 return JSValue::encode(jsNumber(localeCompare(s, a0.toString(exec))));
944 EncodedJSValue JSC_HOST_CALL stringProtoFuncBig(ExecState* exec)
946 JSValue thisValue = exec->hostThisValue();
947 UString s = thisValue.toThisString(exec);
948 return JSValue::encode(jsMakeNontrivialString(exec, "<big>", s, "</big>"));
951 EncodedJSValue JSC_HOST_CALL stringProtoFuncSmall(ExecState* exec)
953 JSValue thisValue = exec->hostThisValue();
954 UString s = thisValue.toThisString(exec);
955 return JSValue::encode(jsMakeNontrivialString(exec, "<small>", s, "</small>"));
958 EncodedJSValue JSC_HOST_CALL stringProtoFuncBlink(ExecState* exec)
960 JSValue thisValue = exec->hostThisValue();
961 UString s = thisValue.toThisString(exec);
962 return JSValue::encode(jsMakeNontrivialString(exec, "<blink>", s, "</blink>"));
965 EncodedJSValue JSC_HOST_CALL stringProtoFuncBold(ExecState* exec)
967 JSValue thisValue = exec->hostThisValue();
968 UString s = thisValue.toThisString(exec);
969 return JSValue::encode(jsMakeNontrivialString(exec, "<b>", s, "</b>"));
972 EncodedJSValue JSC_HOST_CALL stringProtoFuncFixed(ExecState* exec)
974 JSValue thisValue = exec->hostThisValue();
975 UString s = thisValue.toThisString(exec);
976 return JSValue::encode(jsMakeNontrivialString(exec, "<tt>", s, "</tt>"));
979 EncodedJSValue JSC_HOST_CALL stringProtoFuncItalics(ExecState* exec)
981 JSValue thisValue = exec->hostThisValue();
982 UString s = thisValue.toThisString(exec);
983 return JSValue::encode(jsMakeNontrivialString(exec, "<i>", s, "</i>"));
986 EncodedJSValue JSC_HOST_CALL stringProtoFuncStrike(ExecState* exec)
988 JSValue thisValue = exec->hostThisValue();
989 UString s = thisValue.toThisString(exec);
990 return JSValue::encode(jsMakeNontrivialString(exec, "<strike>", s, "</strike>"));
993 EncodedJSValue JSC_HOST_CALL stringProtoFuncSub(ExecState* exec)
995 JSValue thisValue = exec->hostThisValue();
996 UString s = thisValue.toThisString(exec);
997 return JSValue::encode(jsMakeNontrivialString(exec, "<sub>", s, "</sub>"));
1000 EncodedJSValue JSC_HOST_CALL stringProtoFuncSup(ExecState* exec)
1002 JSValue thisValue = exec->hostThisValue();
1003 UString s = thisValue.toThisString(exec);
1004 return JSValue::encode(jsMakeNontrivialString(exec, "<sup>", s, "</sup>"));
1007 EncodedJSValue JSC_HOST_CALL stringProtoFuncFontcolor(ExecState* exec)
1009 JSValue thisValue = exec->hostThisValue();
1010 UString s = thisValue.toThisString(exec);
1011 JSValue a0 = exec->argument(0);
1012 return JSValue::encode(jsMakeNontrivialString(exec, "<font color=\"", a0.toString(exec), "\">", s, "</font>"));
1015 EncodedJSValue JSC_HOST_CALL stringProtoFuncFontsize(ExecState* exec)
1017 JSValue thisValue = exec->hostThisValue();
1018 UString s = thisValue.toThisString(exec);
1019 JSValue a0 = exec->argument(0);
1021 uint32_t smallInteger;
1022 if (a0.getUInt32(smallInteger) && smallInteger <= 9) {
1023 unsigned stringSize = s.length();
1024 unsigned bufferSize = 22 + stringSize;
1026 PassRefPtr<StringImpl> impl = StringImpl::tryCreateUninitialized(bufferSize, buffer);
1028 return JSValue::encode(jsUndefined());
1041 buffer[12] = '0' + smallInteger;
1044 memcpy(&buffer[15], s.characters(), stringSize * sizeof(UChar));
1045 buffer[15 + stringSize] = '<';
1046 buffer[16 + stringSize] = '/';
1047 buffer[17 + stringSize] = 'f';
1048 buffer[18 + stringSize] = 'o';
1049 buffer[19 + stringSize] = 'n';
1050 buffer[20 + stringSize] = 't';
1051 buffer[21 + stringSize] = '>';
1052 return JSValue::encode(jsNontrivialString(exec, impl));
1055 return JSValue::encode(jsMakeNontrivialString(exec, "<font size=\"", a0.toString(exec), "\">", s, "</font>"));
1058 EncodedJSValue JSC_HOST_CALL stringProtoFuncAnchor(ExecState* exec)
1060 JSValue thisValue = exec->hostThisValue();
1061 UString s = thisValue.toThisString(exec);
1062 JSValue a0 = exec->argument(0);
1063 return JSValue::encode(jsMakeNontrivialString(exec, "<a name=\"", a0.toString(exec), "\">", s, "</a>"));
1066 EncodedJSValue JSC_HOST_CALL stringProtoFuncLink(ExecState* exec)
1068 JSValue thisValue = exec->hostThisValue();
1069 UString s = thisValue.toThisString(exec);
1070 JSValue a0 = exec->argument(0);
1071 UString linkText = a0.toString(exec);
1073 unsigned linkTextSize = linkText.length();
1074 unsigned stringSize = s.length();
1075 unsigned bufferSize = 15 + linkTextSize + stringSize;
1077 PassRefPtr<StringImpl> impl = StringImpl::tryCreateUninitialized(bufferSize, buffer);
1079 return JSValue::encode(jsUndefined());
1089 memcpy(&buffer[9], linkText.characters(), linkTextSize * sizeof(UChar));
1090 buffer[9 + linkTextSize] = '"';
1091 buffer[10 + linkTextSize] = '>';
1092 memcpy(&buffer[11 + linkTextSize], s.characters(), stringSize * sizeof(UChar));
1093 buffer[11 + linkTextSize + stringSize] = '<';
1094 buffer[12 + linkTextSize + stringSize] = '/';
1095 buffer[13 + linkTextSize + stringSize] = 'a';
1096 buffer[14 + linkTextSize + stringSize] = '>';
1097 return JSValue::encode(jsNontrivialString(exec, impl));
1105 static inline bool isTrimWhitespace(UChar c)
1107 return isStrWhiteSpace(c) || c == 0x200b;
1110 static inline JSValue trimString(ExecState* exec, JSValue thisValue, int trimKind)
1112 if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
1113 return throwTypeError(exec);
1114 UString str = thisValue.toThisString(exec);
1116 if (trimKind & TrimLeft) {
1117 while (left < str.length() && isTrimWhitespace(str[left]))
1120 unsigned right = str.length();
1121 if (trimKind & TrimRight) {
1122 while (right > left && isTrimWhitespace(str[right - 1]))
1126 // Don't gc allocate a new string if we don't have to.
1127 if (left == 0 && right == str.length() && thisValue.isString())
1130 return jsString(exec, str.substringSharingImpl(left, right - left));
1133 EncodedJSValue JSC_HOST_CALL stringProtoFuncTrim(ExecState* exec)
1135 JSValue thisValue = exec->hostThisValue();
1136 return JSValue::encode(trimString(exec, thisValue, TrimLeft | TrimRight));
1139 EncodedJSValue JSC_HOST_CALL stringProtoFuncTrimLeft(ExecState* exec)
1141 JSValue thisValue = exec->hostThisValue();
1142 return JSValue::encode(trimString(exec, thisValue, TrimLeft));
1145 EncodedJSValue JSC_HOST_CALL stringProtoFuncTrimRight(ExecState* exec)
1147 JSValue thisValue = exec->hostThisValue();
1148 return JSValue::encode(trimString(exec, thisValue, TrimRight));