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, Structure* structure)
135 : StringObject(exec->globalData(), structure)
139 void StringPrototype::finishCreation(ExecState* exec, JSGlobalObject*, JSString* nameAndMessage)
141 Base::finishCreation(exec->globalData(), nameAndMessage);
142 ASSERT(inherits(&s_info));
144 // The constructor will be added later, after StringConstructor has been built
145 putDirectWithoutTransition(exec->globalData(), exec->propertyNames().length, jsNumber(0), DontDelete | ReadOnly | DontEnum);
148 bool StringPrototype::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot &slot)
150 return getStaticFunctionSlot<StringObject>(exec, ExecState::stringTable(exec), this, propertyName, slot);
153 bool StringPrototype::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
155 return getStaticFunctionDescriptor<StringObject>(exec, ExecState::stringTable(exec), this, propertyName, descriptor);
158 // ------------------------------ Functions --------------------------
160 static NEVER_INLINE UString substituteBackreferencesSlow(const UString& replacement, const UString& source, const int* ovector, RegExp* reg, size_t i)
162 Vector<UChar> substitutedReplacement;
165 if (i + 1 == replacement.length())
168 UChar ref = replacement[i + 1];
172 substitutedReplacement.append(replacement.characters() + offset, i - offset);
181 backrefStart = ovector[0];
182 backrefLength = ovector[1] - backrefStart;
183 } else if (ref == '`') {
185 backrefLength = ovector[0];
186 } else if (ref == '\'') {
187 backrefStart = ovector[1];
188 backrefLength = source.length() - backrefStart;
189 } else if (reg && ref >= '0' && ref <= '9') {
190 // 1- and 2-digit back references are allowed
191 unsigned backrefIndex = ref - '0';
192 if (backrefIndex > reg->numSubpatterns())
194 if (replacement.length() > i + 2) {
195 ref = replacement[i + 2];
196 if (ref >= '0' && ref <= '9') {
197 backrefIndex = 10 * backrefIndex + ref - '0';
198 if (backrefIndex > reg->numSubpatterns())
199 backrefIndex = backrefIndex / 10; // Fall back to the 1-digit reference
206 backrefStart = ovector[2 * backrefIndex];
207 backrefLength = ovector[2 * backrefIndex + 1] - backrefStart;
212 substitutedReplacement.append(replacement.characters() + offset, i - offset);
215 if (backrefStart >= 0)
216 substitutedReplacement.append(source.characters() + backrefStart, backrefLength);
217 } while ((i = replacement.find('$', i + 1)) != notFound);
219 if (replacement.length() - offset)
220 substitutedReplacement.append(replacement.characters() + offset, replacement.length() - offset);
222 substitutedReplacement.shrinkToFit();
223 return UString::adopt(substitutedReplacement);
226 static inline UString substituteBackreferences(const UString& replacement, const UString& source, const int* ovector, RegExp* reg)
228 size_t i = replacement.find('$', 0);
229 if (UNLIKELY(i != notFound))
230 return substituteBackreferencesSlow(replacement, source, ovector, reg, i);
234 static inline int localeCompare(const UString& a, const UString& b)
236 return Collator::userDefault()->collate(reinterpret_cast<const ::UChar*>(a.characters()), a.length(), reinterpret_cast<const ::UChar*>(b.characters()), b.length());
241 StringRange(int pos, int len)
255 static ALWAYS_INLINE JSValue jsSpliceSubstrings(ExecState* exec, JSString* sourceVal, const UString& source, const StringRange* substringRanges, int rangeCount)
257 if (rangeCount == 1) {
258 int sourceSize = source.length();
259 int position = substringRanges[0].position;
260 int length = substringRanges[0].length;
261 if (position <= 0 && length >= sourceSize)
263 // We could call UString::substr, but this would result in redundant checks
264 return jsString(exec, StringImpl::create(source.impl(), max(0, position), min(sourceSize, length)));
268 for (int i = 0; i < rangeCount; i++)
269 totalLength += substringRanges[i].length;
272 return jsString(exec, "");
275 RefPtr<StringImpl> impl = StringImpl::tryCreateUninitialized(totalLength, buffer);
277 return throwOutOfMemoryError(exec);
280 for (int i = 0; i < rangeCount; i++) {
281 if (int srcLen = substringRanges[i].length) {
282 StringImpl::copyChars(buffer + bufferPos, source.characters() + substringRanges[i].position, srcLen);
287 return jsString(exec, impl.release());
290 static ALWAYS_INLINE JSValue jsSpliceSubstringsWithSeparators(ExecState* exec, JSString* sourceVal, const UString& source, const StringRange* substringRanges, int rangeCount, const UString* separators, int separatorCount)
292 if (rangeCount == 1 && separatorCount == 0) {
293 int sourceSize = source.length();
294 int position = substringRanges[0].position;
295 int length = substringRanges[0].length;
296 if (position <= 0 && length >= sourceSize)
298 // We could call UString::substr, but this would result in redundant checks
299 return jsString(exec, StringImpl::create(source.impl(), max(0, position), min(sourceSize, length)));
303 for (int i = 0; i < rangeCount; i++)
304 totalLength += substringRanges[i].length;
305 for (int i = 0; i < separatorCount; i++)
306 totalLength += separators[i].length();
309 return jsString(exec, "");
312 RefPtr<StringImpl> impl = StringImpl::tryCreateUninitialized(totalLength, buffer);
314 return throwOutOfMemoryError(exec);
316 int maxCount = max(rangeCount, separatorCount);
318 for (int i = 0; i < maxCount; i++) {
319 if (i < rangeCount) {
320 if (int srcLen = substringRanges[i].length) {
321 StringImpl::copyChars(buffer + bufferPos, source.characters() + substringRanges[i].position, srcLen);
325 if (i < separatorCount) {
326 if (int sepLen = separators[i].length()) {
327 StringImpl::copyChars(buffer + bufferPos, separators[i].characters(), sepLen);
333 return jsString(exec, impl.release());
336 EncodedJSValue JSC_HOST_CALL stringProtoFuncReplace(ExecState* exec)
338 JSValue thisValue = exec->hostThisValue();
339 if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
340 return throwVMTypeError(exec);
341 JSString* sourceVal = thisValue.isString() ? asString(thisValue) : jsString(exec, thisValue.toString(exec));
342 JSValue pattern = exec->argument(0);
343 JSValue replacement = exec->argument(1);
344 JSGlobalData* globalData = &exec->globalData();
346 UString replacementString;
348 CallType callType = getCallData(replacement, callData);
349 if (callType == CallTypeNone)
350 replacementString = replacement.toString(exec);
352 if (pattern.inherits(&RegExpObject::s_info)) {
353 const UString& source = sourceVal->value(exec);
354 unsigned sourceLen = source.length();
355 if (exec->hadException())
356 return JSValue::encode(JSValue());
357 RegExp* reg = asRegExpObject(pattern)->regExp();
358 bool global = reg->global();
360 RegExpConstructor* regExpConstructor = exec->lexicalGlobalObject()->regExpConstructor();
362 // Optimization for substring removal (replace with empty).
363 if (global && callType == CallTypeNone && !replacementString.length()) {
365 unsigned startPosition = 0;
367 Vector<StringRange, 16> sourceRanges;
373 regExpConstructor->performMatch(*globalData, reg, source, startPosition, matchIndex, matchLen, &ovector);
377 if (lastIndex < matchIndex)
378 sourceRanges.append(StringRange(lastIndex, matchIndex - lastIndex));
380 lastIndex = matchIndex + matchLen;
381 startPosition = lastIndex;
383 // special case of empty match
386 if (startPosition > sourceLen)
392 return JSValue::encode(sourceVal);
394 if (static_cast<unsigned>(lastIndex) < sourceLen)
395 sourceRanges.append(StringRange(lastIndex, sourceLen - lastIndex));
397 return JSValue::encode(jsSpliceSubstrings(exec, sourceVal, source, sourceRanges.data(), sourceRanges.size()));
401 unsigned startPosition = 0;
403 Vector<StringRange, 16> sourceRanges;
404 Vector<UString, 16> replacements;
406 // This is either a loop (if global is set) or a one-way (if not).
407 if (global && callType == CallTypeJS) {
408 // reg->numSubpatterns() + 1 for pattern args, + 2 for match start and sourceValue
409 int argCount = reg->numSubpatterns() + 1 + 2;
410 JSFunction* func = asFunction(replacement);
411 CachedCall cachedCall(exec, func, argCount);
412 if (exec->hadException())
413 return JSValue::encode(jsNull());
418 regExpConstructor->performMatch(*globalData, reg, source, startPosition, matchIndex, matchLen, &ovector);
422 sourceRanges.append(StringRange(lastIndex, matchIndex - lastIndex));
424 int completeMatchStart = ovector[0];
426 for (; i < reg->numSubpatterns() + 1; ++i) {
427 int matchStart = ovector[i * 2];
428 int matchLen = ovector[i * 2 + 1] - matchStart;
431 cachedCall.setArgument(i, jsUndefined());
433 cachedCall.setArgument(i, jsSubstring(exec, source, matchStart, matchLen));
436 cachedCall.setArgument(i++, jsNumber(completeMatchStart));
437 cachedCall.setArgument(i++, sourceVal);
439 cachedCall.setThis(exec->globalThisValue());
440 JSValue result = cachedCall.call();
441 if (LIKELY(result.isString()))
442 replacements.append(asString(result)->value(exec));
444 replacements.append(result.toString(cachedCall.newCallFrame(exec)));
445 if (exec->hadException())
448 lastIndex = matchIndex + matchLen;
449 startPosition = lastIndex;
451 // special case of empty match
454 if (startPosition > sourceLen)
463 regExpConstructor->performMatch(*globalData, reg, source, startPosition, matchIndex, matchLen, &ovector);
467 if (callType != CallTypeNone) {
468 sourceRanges.append(StringRange(lastIndex, matchIndex - lastIndex));
470 int completeMatchStart = ovector[0];
471 MarkedArgumentBuffer args;
473 for (unsigned i = 0; i < reg->numSubpatterns() + 1; ++i) {
474 int matchStart = ovector[i * 2];
475 int matchLen = ovector[i * 2 + 1] - matchStart;
478 args.append(jsUndefined());
480 args.append(jsSubstring(exec, source, matchStart, matchLen));
483 args.append(jsNumber(completeMatchStart));
484 args.append(sourceVal);
486 replacements.append(call(exec, replacement, callType, callData, exec->globalThisValue(), args).toString(exec));
487 if (exec->hadException())
490 int replLen = replacementString.length();
491 if (lastIndex < matchIndex || replLen) {
492 sourceRanges.append(StringRange(lastIndex, matchIndex - lastIndex));
495 replacements.append(substituteBackreferences(replacementString, source, ovector, reg));
497 replacements.append(UString());
501 lastIndex = matchIndex + matchLen;
502 startPosition = lastIndex;
504 // special case of empty match
507 if (startPosition > sourceLen)
513 if (!lastIndex && replacements.isEmpty())
514 return JSValue::encode(sourceVal);
516 if (static_cast<unsigned>(lastIndex) < sourceLen)
517 sourceRanges.append(StringRange(lastIndex, sourceLen - lastIndex));
519 return JSValue::encode(jsSpliceSubstringsWithSeparators(exec, sourceVal, source, sourceRanges.data(), sourceRanges.size(), replacements.data(), replacements.size()));
522 // Not a regular expression, so treat the pattern as a string.
524 UString patternString = pattern.toString(exec);
525 // Special case for single character patterns without back reference replacement
526 if (patternString.length() == 1 && callType == CallTypeNone && replacementString.find('$', 0) == notFound)
527 return JSValue::encode(sourceVal->replaceCharacter(exec, patternString[0], replacementString));
529 const UString& source = sourceVal->value(exec);
530 size_t matchPos = source.find(patternString);
532 if (matchPos == notFound)
533 return JSValue::encode(sourceVal);
535 int matchLen = patternString.length();
536 if (callType != CallTypeNone) {
537 MarkedArgumentBuffer args;
538 args.append(jsSubstring(exec, source, matchPos, matchLen));
539 args.append(jsNumber(matchPos));
540 args.append(sourceVal);
542 replacementString = call(exec, replacement, callType, callData, exec->globalThisValue(), args).toString(exec);
545 size_t matchEnd = matchPos + matchLen;
546 int ovector[2] = { matchPos, matchEnd };
547 return JSValue::encode(jsString(exec, source.substringSharingImpl(0, matchPos), substituteBackreferences(replacementString, source, ovector, 0), source.substringSharingImpl(matchEnd)));
550 EncodedJSValue JSC_HOST_CALL stringProtoFuncToString(ExecState* exec)
552 JSValue thisValue = exec->hostThisValue();
553 // Also used for valueOf.
555 if (thisValue.isString())
556 return JSValue::encode(thisValue);
558 if (thisValue.inherits(&StringObject::s_info))
559 return JSValue::encode(asStringObject(thisValue)->internalValue());
561 return throwVMTypeError(exec);
564 EncodedJSValue JSC_HOST_CALL stringProtoFuncCharAt(ExecState* exec)
566 JSValue thisValue = exec->hostThisValue();
567 if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
568 return throwVMTypeError(exec);
569 UString s = thisValue.toString(exec);
570 unsigned len = s.length();
571 JSValue a0 = exec->argument(0);
573 uint32_t i = a0.asUInt32();
575 return JSValue::encode(jsSingleCharacterSubstring(exec, s, i));
576 return JSValue::encode(jsEmptyString(exec));
578 double dpos = a0.toInteger(exec);
579 if (dpos >= 0 && dpos < len)
580 return JSValue::encode(jsSingleCharacterSubstring(exec, s, static_cast<unsigned>(dpos)));
581 return JSValue::encode(jsEmptyString(exec));
584 EncodedJSValue JSC_HOST_CALL stringProtoFuncCharCodeAt(ExecState* exec)
586 JSValue thisValue = exec->hostThisValue();
587 if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
588 return throwVMTypeError(exec);
589 UString s = thisValue.toString(exec);
590 unsigned len = s.length();
591 JSValue a0 = exec->argument(0);
593 uint32_t i = a0.asUInt32();
595 return JSValue::encode(jsNumber(s.characters()[i]));
596 return JSValue::encode(jsNaN());
598 double dpos = a0.toInteger(exec);
599 if (dpos >= 0 && dpos < len)
600 return JSValue::encode(jsNumber(s[static_cast<int>(dpos)]));
601 return JSValue::encode(jsNaN());
604 EncodedJSValue JSC_HOST_CALL stringProtoFuncConcat(ExecState* exec)
606 JSValue thisValue = exec->hostThisValue();
607 if (thisValue.isString() && (exec->argumentCount() == 1)) {
608 JSValue v = exec->argument(0);
609 return JSValue::encode(v.isString()
610 ? jsString(exec, asString(thisValue), asString(v))
611 : jsString(exec, asString(thisValue), v.toString(exec)));
613 if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
614 return throwVMTypeError(exec);
615 return JSValue::encode(jsString(exec, thisValue));
618 EncodedJSValue JSC_HOST_CALL stringProtoFuncIndexOf(ExecState* exec)
620 JSValue thisValue = exec->hostThisValue();
621 if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
622 return throwVMTypeError(exec);
623 UString s = thisValue.toString(exec);
624 int len = s.length();
626 JSValue a0 = exec->argument(0);
627 JSValue a1 = exec->argument(1);
628 UString u2 = a0.toString(exec);
630 if (a1.isUndefined())
632 else if (a1.isUInt32())
633 pos = min<uint32_t>(a1.asUInt32(), len);
635 double dpos = a1.toInteger(exec);
640 pos = static_cast<int>(dpos);
643 size_t result = s.find(u2, pos);
644 if (result == notFound)
645 return JSValue::encode(jsNumber(-1));
646 return JSValue::encode(jsNumber(result));
649 EncodedJSValue JSC_HOST_CALL stringProtoFuncLastIndexOf(ExecState* exec)
651 JSValue thisValue = exec->hostThisValue();
652 if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
653 return throwVMTypeError(exec);
654 UString s = thisValue.toString(exec);
655 int len = s.length();
657 JSValue a0 = exec->argument(0);
658 JSValue a1 = exec->argument(1);
660 UString u2 = a0.toString(exec);
661 double dpos = a1.toIntegerPreserveNaN(exec);
664 else if (!(dpos <= len)) // true for NaN
667 // Work around for broken NaN compare operator
668 else if (isnan(dpos))
672 size_t result = s.reverseFind(u2, static_cast<unsigned>(dpos));
673 if (result == notFound)
674 return JSValue::encode(jsNumber(-1));
675 return JSValue::encode(jsNumber(result));
678 EncodedJSValue JSC_HOST_CALL stringProtoFuncMatch(ExecState* exec)
680 JSValue thisValue = exec->hostThisValue();
681 if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
682 return throwVMTypeError(exec);
683 UString s = thisValue.toString(exec);
684 JSGlobalData* globalData = &exec->globalData();
686 JSValue a0 = exec->argument(0);
689 if (a0.inherits(&RegExpObject::s_info))
690 reg = asRegExpObject(a0)->regExp();
693 * ECMA 15.5.4.12 String.prototype.search (regexp)
694 * If regexp is not an object whose [[Class]] property is "RegExp", it is
695 * replaced with the result of the expression new RegExp(regexp).
697 reg = RegExp::create(exec->globalData(), a0.toString(exec), NoFlags);
699 RegExpConstructor* regExpConstructor = exec->lexicalGlobalObject()->regExpConstructor();
702 regExpConstructor->performMatch(*globalData, reg, s, 0, pos, matchLength);
703 if (!(reg->global())) {
704 // case without 'g' flag is handled like RegExp.prototype.exec
706 return JSValue::encode(jsNull());
707 return JSValue::encode(regExpConstructor->arrayOfMatches(exec));
710 // return array of matches
711 MarkedArgumentBuffer list;
713 list.append(jsSubstring(exec, s, pos, matchLength));
714 pos += matchLength == 0 ? 1 : matchLength;
715 regExpConstructor->performMatch(*globalData, reg, s, pos, pos, matchLength);
717 if (list.isEmpty()) {
718 // if there are no matches at all, it's important to return
719 // Null instead of an empty array, because this matches
720 // other browsers and because Null is a false value.
721 return JSValue::encode(jsNull());
724 return JSValue::encode(constructArray(exec, list));
727 EncodedJSValue JSC_HOST_CALL stringProtoFuncSearch(ExecState* exec)
729 JSValue thisValue = exec->hostThisValue();
730 if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
731 return throwVMTypeError(exec);
732 UString s = thisValue.toString(exec);
733 JSGlobalData* globalData = &exec->globalData();
735 JSValue a0 = exec->argument(0);
738 if (a0.inherits(&RegExpObject::s_info))
739 reg = asRegExpObject(a0)->regExp();
742 * ECMA 15.5.4.12 String.prototype.search (regexp)
743 * If regexp is not an object whose [[Class]] property is "RegExp", it is
744 * replaced with the result of the expression new RegExp(regexp).
746 reg = RegExp::create(exec->globalData(), a0.toString(exec), NoFlags);
748 RegExpConstructor* regExpConstructor = exec->lexicalGlobalObject()->regExpConstructor();
751 regExpConstructor->performMatch(*globalData, reg, s, 0, pos, matchLength);
752 return JSValue::encode(jsNumber(pos));
755 EncodedJSValue JSC_HOST_CALL stringProtoFuncSlice(ExecState* exec)
757 JSValue thisValue = exec->hostThisValue();
758 if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
759 return throwVMTypeError(exec);
760 UString s = thisValue.toString(exec);
761 int len = s.length();
763 JSValue a0 = exec->argument(0);
764 JSValue a1 = exec->argument(1);
766 // The arg processing is very much like ArrayProtoFunc::Slice
767 double start = a0.toInteger(exec);
768 double end = a1.isUndefined() ? len : a1.toInteger(exec);
769 double from = start < 0 ? len + start : start;
770 double to = end < 0 ? len + end : end;
771 if (to > from && to > 0 && from < len) {
776 return JSValue::encode(jsSubstring(exec, s, static_cast<unsigned>(from), static_cast<unsigned>(to) - static_cast<unsigned>(from)));
779 return JSValue::encode(jsEmptyString(exec));
782 EncodedJSValue JSC_HOST_CALL stringProtoFuncSplit(ExecState* exec)
784 JSValue thisValue = exec->hostThisValue();
785 if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
786 return throwVMTypeError(exec);
787 UString s = thisValue.toString(exec);
788 JSGlobalData* globalData = &exec->globalData();
790 JSValue a0 = exec->argument(0);
791 JSValue a1 = exec->argument(1);
793 JSArray* result = constructEmptyArray(exec);
796 unsigned limit = a1.isUndefined() ? 0xFFFFFFFFU : a1.toUInt32(exec);
797 if (a0.inherits(&RegExpObject::s_info)) {
798 RegExp* reg = asRegExpObject(a0)->regExp();
799 if (s.isEmpty() && reg->match(*globalData, s, 0) >= 0) {
800 // empty string matched by regexp -> empty array
801 return JSValue::encode(result);
804 while (i != limit && pos < s.length()) {
805 Vector<int, 32> ovector;
806 int mpos = reg->match(*globalData, s, pos, &ovector);
809 int mlen = ovector[1] - ovector[0];
810 pos = mpos + (mlen == 0 ? 1 : mlen);
811 if (static_cast<unsigned>(mpos) != p0 || mlen) {
812 result->put(exec, i++, jsSubstring(exec, s, p0, mpos - p0));
815 for (unsigned si = 1; si <= reg->numSubpatterns(); ++si) {
816 int spos = ovector[si * 2];
818 result->put(exec, i++, jsUndefined());
820 result->put(exec, i++, jsSubstring(exec, s, spos, ovector[si * 2 + 1] - spos));
824 UString u2 = a0.toString(exec);
827 // empty separator matches empty string -> empty array
828 return JSValue::encode(result);
830 while (i != limit && p0 < s.length() - 1)
831 result->put(exec, i++, jsSingleCharacterSubstring(exec, s, p0++));
834 while (i != limit && (pos = s.find(u2, p0)) != notFound) {
835 result->put(exec, i++, jsSubstring(exec, s, p0, pos - p0));
836 p0 = pos + u2.length();
841 // add remaining string
843 result->put(exec, i++, jsSubstring(exec, s, p0, s.length() - p0));
845 return JSValue::encode(result);
848 EncodedJSValue JSC_HOST_CALL stringProtoFuncSubstr(ExecState* exec)
850 JSValue thisValue = exec->hostThisValue();
852 JSString* jsString = 0;
854 if (thisValue.isString()) {
855 jsString = static_cast<JSString*>(thisValue.asCell());
856 len = jsString->length();
857 } else if (thisValue.isUndefinedOrNull()) {
858 // CheckObjectCoercible
859 return throwVMTypeError(exec);
861 uString = thisValue.toString(exec);
862 if (exec->hadException())
863 return JSValue::encode(jsUndefined());
864 len = uString.length();
867 JSValue a0 = exec->argument(0);
868 JSValue a1 = exec->argument(1);
870 double start = a0.toInteger(exec);
871 double length = a1.isUndefined() ? len : a1.toInteger(exec);
872 if (start >= len || length <= 0)
873 return JSValue::encode(jsEmptyString(exec));
879 if (start + length > len)
880 length = len - start;
881 unsigned substringStart = static_cast<unsigned>(start);
882 unsigned substringLength = static_cast<unsigned>(length);
884 return JSValue::encode(jsSubstring(exec, jsString, substringStart, substringLength));
885 return JSValue::encode(jsSubstring(exec, uString, substringStart, substringLength));
888 EncodedJSValue JSC_HOST_CALL stringProtoFuncSubstring(ExecState* exec)
890 JSValue thisValue = exec->hostThisValue();
892 JSString* jsString = 0;
894 if (thisValue.isString()) {
895 jsString = static_cast<JSString*>(thisValue.asCell());
896 len = jsString->length();
897 } else if (thisValue.isUndefinedOrNull()) {
898 // CheckObjectCoercible
899 return throwVMTypeError(exec);
901 uString = thisValue.toString(exec);
902 if (exec->hadException())
903 return JSValue::encode(jsUndefined());
904 len = uString.length();
907 JSValue a0 = exec->argument(0);
908 JSValue a1 = exec->argument(1);
910 double start = a0.toNumber(exec);
912 if (!(start >= 0)) // check for negative values or NaN
914 else if (start > len)
916 if (a1.isUndefined())
919 end = a1.toNumber(exec);
920 if (!(end >= 0)) // check for negative values or NaN
930 unsigned substringStart = static_cast<unsigned>(start);
931 unsigned substringLength = static_cast<unsigned>(end) - substringStart;
933 return JSValue::encode(jsSubstring(exec, jsString, substringStart, substringLength));
934 return JSValue::encode(jsSubstring(exec, uString, substringStart, substringLength));
937 EncodedJSValue JSC_HOST_CALL stringProtoFuncToLowerCase(ExecState* exec)
939 JSValue thisValue = exec->hostThisValue();
940 if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
941 return throwVMTypeError(exec);
942 JSString* sVal = thisValue.isString() ? asString(thisValue) : jsString(exec, thisValue.toString(exec));
943 const UString& s = sVal->value(exec);
945 int sSize = s.length();
947 return JSValue::encode(sVal);
949 const UChar* sData = s.characters();
950 Vector<UChar> buffer(sSize);
953 for (int i = 0; i < sSize; i++) {
956 buffer[i] = toASCIILower(c);
959 return JSValue::encode(jsString(exec, UString::adopt(buffer)));
962 int length = Unicode::toLower(buffer.data(), sSize, sData, sSize, &error);
964 buffer.resize(length);
965 length = Unicode::toLower(buffer.data(), length, sData, sSize, &error);
967 return JSValue::encode(sVal);
969 if (length == sSize) {
970 if (memcmp(buffer.data(), sData, length * sizeof(UChar)) == 0)
971 return JSValue::encode(sVal);
973 buffer.resize(length);
974 return JSValue::encode(jsString(exec, UString::adopt(buffer)));
977 EncodedJSValue JSC_HOST_CALL stringProtoFuncToUpperCase(ExecState* exec)
979 JSValue thisValue = exec->hostThisValue();
980 if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
981 return throwVMTypeError(exec);
982 JSString* sVal = thisValue.isString() ? asString(thisValue) : jsString(exec, thisValue.toString(exec));
983 const UString& s = sVal->value(exec);
985 int sSize = s.length();
987 return JSValue::encode(sVal);
989 const UChar* sData = s.characters();
990 Vector<UChar> buffer(sSize);
993 for (int i = 0; i < sSize; i++) {
996 buffer[i] = toASCIIUpper(c);
999 return JSValue::encode(jsString(exec, UString::adopt(buffer)));
1002 int length = Unicode::toUpper(buffer.data(), sSize, sData, sSize, &error);
1004 buffer.resize(length);
1005 length = Unicode::toUpper(buffer.data(), length, sData, sSize, &error);
1007 return JSValue::encode(sVal);
1009 if (length == sSize) {
1010 if (memcmp(buffer.data(), sData, length * sizeof(UChar)) == 0)
1011 return JSValue::encode(sVal);
1013 buffer.resize(length);
1014 return JSValue::encode(jsString(exec, UString::adopt(buffer)));
1017 EncodedJSValue JSC_HOST_CALL stringProtoFuncLocaleCompare(ExecState* exec)
1019 if (exec->argumentCount() < 1)
1020 return JSValue::encode(jsNumber(0));
1022 JSValue thisValue = exec->hostThisValue();
1023 if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
1024 return throwVMTypeError(exec);
1025 UString s = thisValue.toString(exec);
1027 JSValue a0 = exec->argument(0);
1028 return JSValue::encode(jsNumber(localeCompare(s, a0.toString(exec))));
1031 EncodedJSValue JSC_HOST_CALL stringProtoFuncBig(ExecState* exec)
1033 JSValue thisValue = exec->hostThisValue();
1034 if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
1035 return throwVMTypeError(exec);
1036 UString s = thisValue.toString(exec);
1037 return JSValue::encode(jsMakeNontrivialString(exec, "<big>", s, "</big>"));
1040 EncodedJSValue JSC_HOST_CALL stringProtoFuncSmall(ExecState* exec)
1042 JSValue thisValue = exec->hostThisValue();
1043 if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
1044 return throwVMTypeError(exec);
1045 UString s = thisValue.toString(exec);
1046 return JSValue::encode(jsMakeNontrivialString(exec, "<small>", s, "</small>"));
1049 EncodedJSValue JSC_HOST_CALL stringProtoFuncBlink(ExecState* exec)
1051 JSValue thisValue = exec->hostThisValue();
1052 if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
1053 return throwVMTypeError(exec);
1054 UString s = thisValue.toString(exec);
1055 return JSValue::encode(jsMakeNontrivialString(exec, "<blink>", s, "</blink>"));
1058 EncodedJSValue JSC_HOST_CALL stringProtoFuncBold(ExecState* exec)
1060 JSValue thisValue = exec->hostThisValue();
1061 if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
1062 return throwVMTypeError(exec);
1063 UString s = thisValue.toString(exec);
1064 return JSValue::encode(jsMakeNontrivialString(exec, "<b>", s, "</b>"));
1067 EncodedJSValue JSC_HOST_CALL stringProtoFuncFixed(ExecState* exec)
1069 JSValue thisValue = exec->hostThisValue();
1070 if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
1071 return throwVMTypeError(exec);
1072 UString s = thisValue.toString(exec);
1073 return JSValue::encode(jsMakeNontrivialString(exec, "<tt>", s, "</tt>"));
1076 EncodedJSValue JSC_HOST_CALL stringProtoFuncItalics(ExecState* exec)
1078 JSValue thisValue = exec->hostThisValue();
1079 if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
1080 return throwVMTypeError(exec);
1081 UString s = thisValue.toString(exec);
1082 return JSValue::encode(jsMakeNontrivialString(exec, "<i>", s, "</i>"));
1085 EncodedJSValue JSC_HOST_CALL stringProtoFuncStrike(ExecState* exec)
1087 JSValue thisValue = exec->hostThisValue();
1088 if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
1089 return throwVMTypeError(exec);
1090 UString s = thisValue.toString(exec);
1091 return JSValue::encode(jsMakeNontrivialString(exec, "<strike>", s, "</strike>"));
1094 EncodedJSValue JSC_HOST_CALL stringProtoFuncSub(ExecState* exec)
1096 JSValue thisValue = exec->hostThisValue();
1097 if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
1098 return throwVMTypeError(exec);
1099 UString s = thisValue.toString(exec);
1100 return JSValue::encode(jsMakeNontrivialString(exec, "<sub>", s, "</sub>"));
1103 EncodedJSValue JSC_HOST_CALL stringProtoFuncSup(ExecState* exec)
1105 JSValue thisValue = exec->hostThisValue();
1106 if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
1107 return throwVMTypeError(exec);
1108 UString s = thisValue.toString(exec);
1109 return JSValue::encode(jsMakeNontrivialString(exec, "<sup>", s, "</sup>"));
1112 EncodedJSValue JSC_HOST_CALL stringProtoFuncFontcolor(ExecState* exec)
1114 JSValue thisValue = exec->hostThisValue();
1115 if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
1116 return throwVMTypeError(exec);
1117 UString s = thisValue.toString(exec);
1118 JSValue a0 = exec->argument(0);
1119 return JSValue::encode(jsMakeNontrivialString(exec, "<font color=\"", a0.toString(exec), "\">", s, "</font>"));
1122 EncodedJSValue JSC_HOST_CALL stringProtoFuncFontsize(ExecState* exec)
1124 JSValue thisValue = exec->hostThisValue();
1125 if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
1126 return throwVMTypeError(exec);
1127 UString s = thisValue.toString(exec);
1128 JSValue a0 = exec->argument(0);
1130 uint32_t smallInteger;
1131 if (a0.getUInt32(smallInteger) && smallInteger <= 9) {
1132 unsigned stringSize = s.length();
1133 unsigned bufferSize = 22 + stringSize;
1135 PassRefPtr<StringImpl> impl = StringImpl::tryCreateUninitialized(bufferSize, buffer);
1137 return JSValue::encode(jsUndefined());
1150 buffer[12] = '0' + smallInteger;
1153 memcpy(&buffer[15], s.characters(), stringSize * sizeof(UChar));
1154 buffer[15 + stringSize] = '<';
1155 buffer[16 + stringSize] = '/';
1156 buffer[17 + stringSize] = 'f';
1157 buffer[18 + stringSize] = 'o';
1158 buffer[19 + stringSize] = 'n';
1159 buffer[20 + stringSize] = 't';
1160 buffer[21 + stringSize] = '>';
1161 return JSValue::encode(jsNontrivialString(exec, impl));
1164 return JSValue::encode(jsMakeNontrivialString(exec, "<font size=\"", a0.toString(exec), "\">", s, "</font>"));
1167 EncodedJSValue JSC_HOST_CALL stringProtoFuncAnchor(ExecState* exec)
1169 JSValue thisValue = exec->hostThisValue();
1170 if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
1171 return throwVMTypeError(exec);
1172 UString s = thisValue.toString(exec);
1173 JSValue a0 = exec->argument(0);
1174 return JSValue::encode(jsMakeNontrivialString(exec, "<a name=\"", a0.toString(exec), "\">", s, "</a>"));
1177 EncodedJSValue JSC_HOST_CALL stringProtoFuncLink(ExecState* exec)
1179 JSValue thisValue = exec->hostThisValue();
1180 if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
1181 return throwVMTypeError(exec);
1182 UString s = thisValue.toString(exec);
1183 JSValue a0 = exec->argument(0);
1184 UString linkText = a0.toString(exec);
1186 unsigned linkTextSize = linkText.length();
1187 unsigned stringSize = s.length();
1188 unsigned bufferSize = 15 + linkTextSize + stringSize;
1190 PassRefPtr<StringImpl> impl = StringImpl::tryCreateUninitialized(bufferSize, buffer);
1192 return JSValue::encode(jsUndefined());
1202 memcpy(&buffer[9], linkText.characters(), linkTextSize * sizeof(UChar));
1203 buffer[9 + linkTextSize] = '"';
1204 buffer[10 + linkTextSize] = '>';
1205 memcpy(&buffer[11 + linkTextSize], s.characters(), stringSize * sizeof(UChar));
1206 buffer[11 + linkTextSize + stringSize] = '<';
1207 buffer[12 + linkTextSize + stringSize] = '/';
1208 buffer[13 + linkTextSize + stringSize] = 'a';
1209 buffer[14 + linkTextSize + stringSize] = '>';
1210 return JSValue::encode(jsNontrivialString(exec, impl));
1218 static inline bool isTrimWhitespace(UChar c)
1220 return isStrWhiteSpace(c) || c == 0x200b;
1223 static inline JSValue trimString(ExecState* exec, JSValue thisValue, int trimKind)
1225 if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
1226 return throwTypeError(exec);
1227 UString str = thisValue.toString(exec);
1229 if (trimKind & TrimLeft) {
1230 while (left < str.length() && isTrimWhitespace(str[left]))
1233 unsigned right = str.length();
1234 if (trimKind & TrimRight) {
1235 while (right > left && isTrimWhitespace(str[right - 1]))
1239 // Don't gc allocate a new string if we don't have to.
1240 if (left == 0 && right == str.length() && thisValue.isString())
1243 return jsString(exec, str.substringSharingImpl(left, right - left));
1246 EncodedJSValue JSC_HOST_CALL stringProtoFuncTrim(ExecState* exec)
1248 JSValue thisValue = exec->hostThisValue();
1249 return JSValue::encode(trimString(exec, thisValue, TrimLeft | TrimRight));
1252 EncodedJSValue JSC_HOST_CALL stringProtoFuncTrimLeft(ExecState* exec)
1254 JSValue thisValue = exec->hostThisValue();
1255 return JSValue::encode(trimString(exec, thisValue, TrimLeft));
1258 EncodedJSValue JSC_HOST_CALL stringProtoFuncTrimRight(ExecState* exec)
1260 JSValue thisValue = exec->hostThisValue();
1261 return JSValue::encode(trimString(exec, thisValue, TrimRight));