2 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
3 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "StringPrototype.h"
24 #include "CachedCall.h"
26 #include "JSFunction.h"
27 #include "ObjectPrototype.h"
28 #include "PropertyNameArray.h"
29 #include "RegExpConstructor.h"
30 #include "RegExpObject.h"
31 #include <wtf/ASCIICType.h>
32 #include <wtf/MathExtras.h>
33 #include <wtf/unicode/Collator.h>
39 ASSERT_CLASS_FITS_IN_CELL(StringPrototype);
41 static JSValue JSC_HOST_CALL stringProtoFuncToString(ExecState*, JSObject*, JSValue, const ArgList&);
42 static JSValue JSC_HOST_CALL stringProtoFuncCharAt(ExecState*, JSObject*, JSValue, const ArgList&);
43 static JSValue JSC_HOST_CALL stringProtoFuncCharCodeAt(ExecState*, JSObject*, JSValue, const ArgList&);
44 static JSValue JSC_HOST_CALL stringProtoFuncConcat(ExecState*, JSObject*, JSValue, const ArgList&);
45 static JSValue JSC_HOST_CALL stringProtoFuncIndexOf(ExecState*, JSObject*, JSValue, const ArgList&);
46 static JSValue JSC_HOST_CALL stringProtoFuncLastIndexOf(ExecState*, JSObject*, JSValue, const ArgList&);
47 static JSValue JSC_HOST_CALL stringProtoFuncMatch(ExecState*, JSObject*, JSValue, const ArgList&);
48 static JSValue JSC_HOST_CALL stringProtoFuncReplace(ExecState*, JSObject*, JSValue, const ArgList&);
49 static JSValue JSC_HOST_CALL stringProtoFuncSearch(ExecState*, JSObject*, JSValue, const ArgList&);
50 static JSValue JSC_HOST_CALL stringProtoFuncSlice(ExecState*, JSObject*, JSValue, const ArgList&);
51 static JSValue JSC_HOST_CALL stringProtoFuncSplit(ExecState*, JSObject*, JSValue, const ArgList&);
52 static JSValue JSC_HOST_CALL stringProtoFuncSubstr(ExecState*, JSObject*, JSValue, const ArgList&);
53 static JSValue JSC_HOST_CALL stringProtoFuncSubstring(ExecState*, JSObject*, JSValue, const ArgList&);
54 static JSValue JSC_HOST_CALL stringProtoFuncToLowerCase(ExecState*, JSObject*, JSValue, const ArgList&);
55 static JSValue JSC_HOST_CALL stringProtoFuncToUpperCase(ExecState*, JSObject*, JSValue, const ArgList&);
56 static JSValue JSC_HOST_CALL stringProtoFuncLocaleCompare(ExecState*, JSObject*, JSValue, const ArgList&);
58 static JSValue JSC_HOST_CALL stringProtoFuncBig(ExecState*, JSObject*, JSValue, const ArgList&);
59 static JSValue JSC_HOST_CALL stringProtoFuncSmall(ExecState*, JSObject*, JSValue, const ArgList&);
60 static JSValue JSC_HOST_CALL stringProtoFuncBlink(ExecState*, JSObject*, JSValue, const ArgList&);
61 static JSValue JSC_HOST_CALL stringProtoFuncBold(ExecState*, JSObject*, JSValue, const ArgList&);
62 static JSValue JSC_HOST_CALL stringProtoFuncFixed(ExecState*, JSObject*, JSValue, const ArgList&);
63 static JSValue JSC_HOST_CALL stringProtoFuncItalics(ExecState*, JSObject*, JSValue, const ArgList&);
64 static JSValue JSC_HOST_CALL stringProtoFuncStrike(ExecState*, JSObject*, JSValue, const ArgList&);
65 static JSValue JSC_HOST_CALL stringProtoFuncSub(ExecState*, JSObject*, JSValue, const ArgList&);
66 static JSValue JSC_HOST_CALL stringProtoFuncSup(ExecState*, JSObject*, JSValue, const ArgList&);
67 static JSValue JSC_HOST_CALL stringProtoFuncFontcolor(ExecState*, JSObject*, JSValue, const ArgList&);
68 static JSValue JSC_HOST_CALL stringProtoFuncFontsize(ExecState*, JSObject*, JSValue, const ArgList&);
69 static JSValue JSC_HOST_CALL stringProtoFuncAnchor(ExecState*, JSObject*, JSValue, const ArgList&);
70 static JSValue JSC_HOST_CALL stringProtoFuncLink(ExecState*, JSObject*, JSValue, const ArgList&);
74 #include "StringPrototype.lut.h"
78 const ClassInfo StringPrototype::info = { "String", &StringObject::info, 0, ExecState::stringTable };
80 /* Source for StringPrototype.lut.h
82 toString stringProtoFuncToString DontEnum|Function 0
83 valueOf stringProtoFuncToString DontEnum|Function 0
84 charAt stringProtoFuncCharAt DontEnum|Function 1
85 charCodeAt stringProtoFuncCharCodeAt DontEnum|Function 1
86 concat stringProtoFuncConcat DontEnum|Function 1
87 indexOf stringProtoFuncIndexOf DontEnum|Function 1
88 lastIndexOf stringProtoFuncLastIndexOf DontEnum|Function 1
89 match stringProtoFuncMatch DontEnum|Function 1
90 replace stringProtoFuncReplace DontEnum|Function 2
91 search stringProtoFuncSearch DontEnum|Function 1
92 slice stringProtoFuncSlice DontEnum|Function 2
93 split stringProtoFuncSplit DontEnum|Function 2
94 substr stringProtoFuncSubstr DontEnum|Function 2
95 substring stringProtoFuncSubstring DontEnum|Function 2
96 toLowerCase stringProtoFuncToLowerCase DontEnum|Function 0
97 toUpperCase stringProtoFuncToUpperCase DontEnum|Function 0
98 localeCompare stringProtoFuncLocaleCompare DontEnum|Function 1
100 # toLocaleLowerCase and toLocaleUpperCase are currently identical to toLowerCase and toUpperCase
101 toLocaleLowerCase stringProtoFuncToLowerCase DontEnum|Function 0
102 toLocaleUpperCase stringProtoFuncToUpperCase DontEnum|Function 0
104 big stringProtoFuncBig DontEnum|Function 0
105 small stringProtoFuncSmall DontEnum|Function 0
106 blink stringProtoFuncBlink DontEnum|Function 0
107 bold stringProtoFuncBold DontEnum|Function 0
108 fixed stringProtoFuncFixed DontEnum|Function 0
109 italics stringProtoFuncItalics DontEnum|Function 0
110 strike stringProtoFuncStrike DontEnum|Function 0
111 sub stringProtoFuncSub DontEnum|Function 0
112 sup stringProtoFuncSup DontEnum|Function 0
113 fontcolor stringProtoFuncFontcolor DontEnum|Function 1
114 fontsize stringProtoFuncFontsize DontEnum|Function 1
115 anchor stringProtoFuncAnchor DontEnum|Function 1
116 link stringProtoFuncLink DontEnum|Function 1
121 StringPrototype::StringPrototype(ExecState* exec, PassRefPtr<Structure> structure)
122 : StringObject(exec, structure)
124 // The constructor will be added later, after StringConstructor has been built
125 putDirectWithoutTransition(exec->propertyNames().length, jsNumber(exec, 0), DontDelete | ReadOnly | DontEnum);
128 bool StringPrototype::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot &slot)
130 return getStaticFunctionSlot<StringObject>(exec, ExecState::stringTable(exec), this, propertyName, slot);
133 // ------------------------------ Functions --------------------------
135 static inline UString substituteBackreferences(const UString& replacement, const UString& source, const int* ovector, RegExp* reg)
137 UString substitutedReplacement;
140 while ((i = replacement.find('$', i + 1)) != -1) {
141 if (i + 1 == replacement.size())
144 UChar ref = replacement[i + 1];
148 substitutedReplacement.append(replacement.data() + offset, i - offset);
157 backrefStart = ovector[0];
158 backrefLength = ovector[1] - backrefStart;
159 } else if (ref == '`') {
161 backrefLength = ovector[0];
162 } else if (ref == '\'') {
163 backrefStart = ovector[1];
164 backrefLength = source.size() - backrefStart;
165 } else if (reg && ref >= '0' && ref <= '9') {
166 // 1- and 2-digit back references are allowed
167 unsigned backrefIndex = ref - '0';
168 if (backrefIndex > reg->numSubpatterns())
170 if (replacement.size() > i + 2) {
171 ref = replacement[i + 2];
172 if (ref >= '0' && ref <= '9') {
173 backrefIndex = 10 * backrefIndex + ref - '0';
174 if (backrefIndex > reg->numSubpatterns())
175 backrefIndex = backrefIndex / 10; // Fall back to the 1-digit reference
182 backrefStart = ovector[2 * backrefIndex];
183 backrefLength = ovector[2 * backrefIndex + 1] - backrefStart;
188 substitutedReplacement.append(replacement.data() + offset, i - offset);
191 substitutedReplacement.append(source.data() + backrefStart, backrefLength);
197 if (replacement.size() - offset)
198 substitutedReplacement.append(replacement.data() + offset, replacement.size() - offset);
200 return substitutedReplacement;
203 static inline int localeCompare(const UString& a, const UString& b)
205 return Collator::userDefault()->collate(reinterpret_cast<const ::UChar*>(a.data()), a.size(), reinterpret_cast<const ::UChar*>(b.data()), b.size());
208 JSValue JSC_HOST_CALL stringProtoFuncReplace(ExecState* exec, JSObject*, JSValue thisValue, const ArgList& args)
210 JSString* sourceVal = thisValue.toThisJSString(exec);
211 const UString& source = sourceVal->value();
213 JSValue pattern = args.at(0);
215 JSValue replacement = args.at(1);
216 UString replacementString;
218 CallType callType = replacement.getCallData(callData);
219 if (callType == CallTypeNone)
220 replacementString = replacement.toString(exec);
222 if (pattern.isObject(&RegExpObject::info)) {
223 RegExp* reg = asRegExpObject(pattern)->regExp();
224 bool global = reg->global();
226 RegExpConstructor* regExpConstructor = exec->lexicalGlobalObject()->regExpConstructor();
229 int startPosition = 0;
231 Vector<UString::Range, 16> sourceRanges;
232 Vector<UString, 16> replacements;
234 // This is either a loop (if global is set) or a one-way (if not).
235 if (global && callType == CallTypeJS) {
236 // reg->numSubpatterns() + 1 for pattern args, + 2 for match start and sourceValue
237 int argCount = reg->numSubpatterns() + 1 + 2;
238 JSFunction* func = asFunction(replacement);
239 CachedCall cachedCall(exec, func, argCount, exec->exceptionSlot());
240 if (exec->hadException())
246 regExpConstructor->performMatch(reg, source, startPosition, matchIndex, matchLen, &ovector);
250 sourceRanges.append(UString::Range(lastIndex, matchIndex - lastIndex));
252 int completeMatchStart = ovector[0];
254 for (; i < reg->numSubpatterns() + 1; ++i) {
255 int matchStart = ovector[i * 2];
256 int matchLen = ovector[i * 2 + 1] - matchStart;
259 cachedCall.setArgument(i, jsUndefined());
261 cachedCall.setArgument(i, jsSubstring(exec, source, matchStart, matchLen));
264 cachedCall.setArgument(i++, jsNumber(exec, completeMatchStart));
265 cachedCall.setArgument(i++, sourceVal);
267 cachedCall.setThis(exec->globalThisValue());
268 replacements.append(cachedCall.call().toString(cachedCall.newCallFrame()));
269 if (exec->hadException())
272 lastIndex = matchIndex + matchLen;
273 startPosition = lastIndex;
275 // special case of empty match
278 if (startPosition > source.size())
287 regExpConstructor->performMatch(reg, source, startPosition, matchIndex, matchLen, &ovector);
291 sourceRanges.append(UString::Range(lastIndex, matchIndex - lastIndex));
293 if (callType != CallTypeNone) {
294 int completeMatchStart = ovector[0];
295 MarkedArgumentBuffer args;
297 for (unsigned i = 0; i < reg->numSubpatterns() + 1; ++i) {
298 int matchStart = ovector[i * 2];
299 int matchLen = ovector[i * 2 + 1] - matchStart;
302 args.append(jsUndefined());
304 args.append(jsSubstring(exec, source, matchStart, matchLen));
307 args.append(jsNumber(exec, completeMatchStart));
308 args.append(sourceVal);
310 replacements.append(call(exec, replacement, callType, callData, exec->globalThisValue(), args).toString(exec));
311 if (exec->hadException())
314 replacements.append(substituteBackreferences(replacementString, source, ovector, reg));
316 lastIndex = matchIndex + matchLen;
317 startPosition = lastIndex;
319 // special case of empty match
322 if (startPosition > source.size())
328 if (!lastIndex && replacements.isEmpty())
331 if (lastIndex < source.size())
332 sourceRanges.append(UString::Range(lastIndex, source.size() - lastIndex));
334 return jsString(exec, source.spliceSubstringsWithSeparators(sourceRanges.data(), sourceRanges.size(),
335 replacements.data(), replacements.size()));
338 // Not a regular expression, so treat the pattern as a string.
340 UString patternString = pattern.toString(exec);
341 int matchPos = source.find(patternString);
346 int matchLen = patternString.size();
347 if (callType != CallTypeNone) {
348 MarkedArgumentBuffer args;
349 args.append(jsSubstring(exec, source, matchPos, matchLen));
350 args.append(jsNumber(exec, matchPos));
351 args.append(sourceVal);
353 replacementString = call(exec, replacement, callType, callData, exec->globalThisValue(), args).toString(exec);
356 int ovector[2] = { matchPos, matchPos + matchLen };
357 return jsString(exec, source.replaceRange(matchPos, matchLen, substituteBackreferences(replacementString, source, ovector, 0)));
360 JSValue JSC_HOST_CALL stringProtoFuncToString(ExecState* exec, JSObject*, JSValue thisValue, const ArgList&)
362 // Also used for valueOf.
364 if (thisValue.isString())
367 if (thisValue.isObject(&StringObject::info))
368 return asStringObject(thisValue)->internalValue();
370 return throwError(exec, TypeError);
373 JSValue JSC_HOST_CALL stringProtoFuncCharAt(ExecState* exec, JSObject*, JSValue thisValue, const ArgList& args)
375 UString s = thisValue.toThisString(exec);
376 unsigned len = s.size();
377 JSValue a0 = args.at(0);
378 if (a0.isUInt32Fast()) {
379 uint32_t i = a0.getUInt32Fast();
381 return jsSingleCharacterSubstring(exec, s, i);
382 return jsEmptyString(exec);
384 double dpos = a0.toInteger(exec);
385 if (dpos >= 0 && dpos < len)
386 return jsSingleCharacterSubstring(exec, s, static_cast<unsigned>(dpos));
387 return jsEmptyString(exec);
390 JSValue JSC_HOST_CALL stringProtoFuncCharCodeAt(ExecState* exec, JSObject*, JSValue thisValue, const ArgList& args)
392 UString s = thisValue.toThisString(exec);
393 unsigned len = s.size();
394 JSValue a0 = args.at(0);
395 if (a0.isUInt32Fast()) {
396 uint32_t i = a0.getUInt32Fast();
398 return jsNumber(exec, s.data()[i]);
401 double dpos = a0.toInteger(exec);
402 if (dpos >= 0 && dpos < len)
403 return jsNumber(exec, s[static_cast<int>(dpos)]);
407 JSValue JSC_HOST_CALL stringProtoFuncConcat(ExecState* exec, JSObject*, JSValue thisValue, const ArgList& args)
409 UString s = thisValue.toThisString(exec);
411 ArgList::const_iterator end = args.end();
412 for (ArgList::const_iterator it = args.begin(); it != end; ++it)
413 s += (*it).toString(exec);
414 return jsString(exec, s);
417 JSValue JSC_HOST_CALL stringProtoFuncIndexOf(ExecState* exec, JSObject*, JSValue thisValue, const ArgList& args)
419 UString s = thisValue.toThisString(exec);
422 JSValue a0 = args.at(0);
423 JSValue a1 = args.at(1);
424 UString u2 = a0.toString(exec);
426 if (a1.isUndefined())
428 else if (a1.isUInt32Fast())
429 pos = min<uint32_t>(a1.getUInt32Fast(), len);
431 double dpos = a1.toInteger(exec);
436 pos = static_cast<int>(dpos);
439 return jsNumber(exec, s.find(u2, pos));
442 JSValue JSC_HOST_CALL stringProtoFuncLastIndexOf(ExecState* exec, JSObject*, JSValue thisValue, const ArgList& args)
444 UString s = thisValue.toThisString(exec);
447 JSValue a0 = args.at(0);
448 JSValue a1 = args.at(1);
450 UString u2 = a0.toString(exec);
451 double dpos = a1.toIntegerPreserveNaN(exec);
454 else if (!(dpos <= len)) // true for NaN
456 return jsNumber(exec, s.rfind(u2, static_cast<int>(dpos)));
459 JSValue JSC_HOST_CALL stringProtoFuncMatch(ExecState* exec, JSObject*, JSValue thisValue, const ArgList& args)
461 UString s = thisValue.toThisString(exec);
463 JSValue a0 = args.at(0);
467 RegExpObject* imp = 0;
468 if (a0.isObject(&RegExpObject::info))
469 reg = asRegExpObject(a0)->regExp();
472 * ECMA 15.5.4.12 String.prototype.search (regexp)
473 * If regexp is not an object whose [[Class]] property is "RegExp", it is
474 * replaced with the result of the expression new RegExp(regexp).
476 reg = RegExp::create(&exec->globalData(), a0.toString(exec));
478 RegExpConstructor* regExpConstructor = exec->lexicalGlobalObject()->regExpConstructor();
481 regExpConstructor->performMatch(reg.get(), u, 0, pos, matchLength);
482 if (!(reg->global())) {
483 // case without 'g' flag is handled like RegExp.prototype.exec
486 return regExpConstructor->arrayOfMatches(exec);
489 // return array of matches
490 MarkedArgumentBuffer list;
493 list.append(jsSubstring(exec, u, pos, matchLength));
495 pos += matchLength == 0 ? 1 : matchLength;
496 regExpConstructor->performMatch(reg.get(), u, pos, pos, matchLength);
499 imp->setLastIndex(lastIndex);
500 if (list.isEmpty()) {
501 // if there are no matches at all, it's important to return
502 // Null instead of an empty array, because this matches
503 // other browsers and because Null is a false value.
507 return constructArray(exec, list);
510 JSValue JSC_HOST_CALL stringProtoFuncSearch(ExecState* exec, JSObject*, JSValue thisValue, const ArgList& args)
512 UString s = thisValue.toThisString(exec);
514 JSValue a0 = args.at(0);
518 if (a0.isObject(&RegExpObject::info))
519 reg = asRegExpObject(a0)->regExp();
522 * ECMA 15.5.4.12 String.prototype.search (regexp)
523 * If regexp is not an object whose [[Class]] property is "RegExp", it is
524 * replaced with the result of the expression new RegExp(regexp).
526 reg = RegExp::create(&exec->globalData(), a0.toString(exec));
528 RegExpConstructor* regExpConstructor = exec->lexicalGlobalObject()->regExpConstructor();
531 regExpConstructor->performMatch(reg.get(), u, 0, pos, matchLength);
532 return jsNumber(exec, pos);
535 JSValue JSC_HOST_CALL stringProtoFuncSlice(ExecState* exec, JSObject*, JSValue thisValue, const ArgList& args)
537 UString s = thisValue.toThisString(exec);
540 JSValue a0 = args.at(0);
541 JSValue a1 = args.at(1);
543 // The arg processing is very much like ArrayProtoFunc::Slice
544 double start = a0.toInteger(exec);
545 double end = a1.isUndefined() ? len : a1.toInteger(exec);
546 double from = start < 0 ? len + start : start;
547 double to = end < 0 ? len + end : end;
548 if (to > from && to > 0 && from < len) {
553 return jsSubstring(exec, s, static_cast<unsigned>(from), static_cast<unsigned>(to) - static_cast<unsigned>(from));
556 return jsEmptyString(exec);
559 JSValue JSC_HOST_CALL stringProtoFuncSplit(ExecState* exec, JSObject*, JSValue thisValue, const ArgList& args)
561 UString s = thisValue.toThisString(exec);
563 JSValue a0 = args.at(0);
564 JSValue a1 = args.at(1);
566 JSArray* result = constructEmptyArray(exec);
569 unsigned limit = a1.isUndefined() ? 0xFFFFFFFFU : a1.toUInt32(exec);
570 if (a0.isObject(&RegExpObject::info)) {
571 RegExp* reg = asRegExpObject(a0)->regExp();
572 if (s.isEmpty() && reg->match(s, 0) >= 0) {
573 // empty string matched by regexp -> empty array
577 while (i != limit && pos < s.size()) {
578 OwnArrayPtr<int> ovector;
579 int mpos = reg->match(s, pos, &ovector);
582 int mlen = ovector[1] - ovector[0];
583 pos = mpos + (mlen == 0 ? 1 : mlen);
584 if (mpos != p0 || mlen) {
585 result->put(exec, i++, jsSubstring(exec, s, p0, mpos - p0));
588 for (unsigned si = 1; si <= reg->numSubpatterns(); ++si) {
589 int spos = ovector[si * 2];
591 result->put(exec, i++, jsUndefined());
593 result->put(exec, i++, jsSubstring(exec, s, spos, ovector[si * 2 + 1] - spos));
597 UString u2 = a0.toString(exec);
600 // empty separator matches empty string -> empty array
603 while (i != limit && p0 < s.size() - 1)
604 result->put(exec, i++, jsSingleCharacterSubstring(exec, s, p0++));
607 while (i != limit && (pos = s.find(u2, p0)) >= 0) {
608 result->put(exec, i++, jsSubstring(exec, s, p0, pos - p0));
609 p0 = pos + u2.size();
614 // add remaining string
616 result->put(exec, i++, jsSubstring(exec, s, p0, s.size() - p0));
621 JSValue JSC_HOST_CALL stringProtoFuncSubstr(ExecState* exec, JSObject*, JSValue thisValue, const ArgList& args)
623 UString s = thisValue.toThisString(exec);
626 JSValue a0 = args.at(0);
627 JSValue a1 = args.at(1);
629 double start = a0.toInteger(exec);
630 double length = a1.isUndefined() ? len : a1.toInteger(exec);
631 if (start >= len || length <= 0)
632 return jsEmptyString(exec);
638 if (start + length > len)
639 length = len - start;
640 return jsSubstring(exec, s, static_cast<unsigned>(start), static_cast<unsigned>(length));
643 JSValue JSC_HOST_CALL stringProtoFuncSubstring(ExecState* exec, JSObject*, JSValue thisValue, const ArgList& args)
645 UString s = thisValue.toThisString(exec);
648 JSValue a0 = args.at(0);
649 JSValue a1 = args.at(1);
651 double start = a0.toNumber(exec);
652 double end = a1.toNumber(exec);
665 if (a1.isUndefined())
672 return jsSubstring(exec, s, static_cast<unsigned>(start), static_cast<unsigned>(end) - static_cast<unsigned>(start));
675 JSValue JSC_HOST_CALL stringProtoFuncToLowerCase(ExecState* exec, JSObject*, JSValue thisValue, const ArgList&)
677 JSString* sVal = thisValue.toThisJSString(exec);
678 const UString& s = sVal->value();
680 int sSize = s.size();
684 const UChar* sData = s.data();
685 Vector<UChar> buffer(sSize);
688 for (int i = 0; i < sSize; i++) {
691 buffer[i] = toASCIILower(c);
694 return jsString(exec, UString(buffer.releaseBuffer(), sSize, false));
697 int length = Unicode::toLower(buffer.data(), sSize, sData, sSize, &error);
699 buffer.resize(length);
700 length = Unicode::toLower(buffer.data(), length, sData, sSize, &error);
704 if (length == sSize && memcmp(buffer.data(), sData, length * sizeof(UChar)) == 0)
706 return jsString(exec, UString(buffer.releaseBuffer(), length, false));
709 JSValue JSC_HOST_CALL stringProtoFuncToUpperCase(ExecState* exec, JSObject*, JSValue thisValue, const ArgList&)
711 JSString* sVal = thisValue.toThisJSString(exec);
712 const UString& s = sVal->value();
714 int sSize = s.size();
718 const UChar* sData = s.data();
719 Vector<UChar> buffer(sSize);
722 for (int i = 0; i < sSize; i++) {
725 buffer[i] = toASCIIUpper(c);
728 return jsString(exec, UString(buffer.releaseBuffer(), sSize, false));
731 int length = Unicode::toUpper(buffer.data(), sSize, sData, sSize, &error);
733 buffer.resize(length);
734 length = Unicode::toUpper(buffer.data(), length, sData, sSize, &error);
738 if (length == sSize && memcmp(buffer.data(), sData, length * sizeof(UChar)) == 0)
740 return jsString(exec, UString(buffer.releaseBuffer(), length, false));
743 JSValue JSC_HOST_CALL stringProtoFuncLocaleCompare(ExecState* exec, JSObject*, JSValue thisValue, const ArgList& args)
746 return jsNumber(exec, 0);
748 UString s = thisValue.toThisString(exec);
749 JSValue a0 = args.at(0);
750 return jsNumber(exec, localeCompare(s, a0.toString(exec)));
753 JSValue JSC_HOST_CALL stringProtoFuncBig(ExecState* exec, JSObject*, JSValue thisValue, const ArgList&)
755 UString s = thisValue.toThisString(exec);
756 return jsNontrivialString(exec, "<big>" + s + "</big>");
759 JSValue JSC_HOST_CALL stringProtoFuncSmall(ExecState* exec, JSObject*, JSValue thisValue, const ArgList&)
761 UString s = thisValue.toThisString(exec);
762 return jsNontrivialString(exec, "<small>" + s + "</small>");
765 JSValue JSC_HOST_CALL stringProtoFuncBlink(ExecState* exec, JSObject*, JSValue thisValue, const ArgList&)
767 UString s = thisValue.toThisString(exec);
768 return jsNontrivialString(exec, "<blink>" + s + "</blink>");
771 JSValue JSC_HOST_CALL stringProtoFuncBold(ExecState* exec, JSObject*, JSValue thisValue, const ArgList&)
773 UString s = thisValue.toThisString(exec);
774 return jsNontrivialString(exec, "<b>" + s + "</b>");
777 JSValue JSC_HOST_CALL stringProtoFuncFixed(ExecState* exec, JSObject*, JSValue thisValue, const ArgList&)
779 UString s = thisValue.toThisString(exec);
780 return jsString(exec, "<tt>" + s + "</tt>");
783 JSValue JSC_HOST_CALL stringProtoFuncItalics(ExecState* exec, JSObject*, JSValue thisValue, const ArgList&)
785 UString s = thisValue.toThisString(exec);
786 return jsNontrivialString(exec, "<i>" + s + "</i>");
789 JSValue JSC_HOST_CALL stringProtoFuncStrike(ExecState* exec, JSObject*, JSValue thisValue, const ArgList&)
791 UString s = thisValue.toThisString(exec);
792 return jsNontrivialString(exec, "<strike>" + s + "</strike>");
795 JSValue JSC_HOST_CALL stringProtoFuncSub(ExecState* exec, JSObject*, JSValue thisValue, const ArgList&)
797 UString s = thisValue.toThisString(exec);
798 return jsNontrivialString(exec, "<sub>" + s + "</sub>");
801 JSValue JSC_HOST_CALL stringProtoFuncSup(ExecState* exec, JSObject*, JSValue thisValue, const ArgList&)
803 UString s = thisValue.toThisString(exec);
804 return jsNontrivialString(exec, "<sup>" + s + "</sup>");
807 JSValue JSC_HOST_CALL stringProtoFuncFontcolor(ExecState* exec, JSObject*, JSValue thisValue, const ArgList& args)
809 UString s = thisValue.toThisString(exec);
810 JSValue a0 = args.at(0);
811 return jsNontrivialString(exec, "<font color=\"" + a0.toString(exec) + "\">" + s + "</font>");
814 JSValue JSC_HOST_CALL stringProtoFuncFontsize(ExecState* exec, JSObject*, JSValue thisValue, const ArgList& args)
816 UString s = thisValue.toThisString(exec);
817 JSValue a0 = args.at(0);
819 uint32_t smallInteger;
820 if (a0.getUInt32(smallInteger) && smallInteger <= 9) {
821 unsigned stringSize = s.size();
822 unsigned bufferSize = 22 + stringSize;
823 UChar* buffer = static_cast<UChar*>(tryFastMalloc(bufferSize * sizeof(UChar)));
825 return jsUndefined();
838 buffer[12] = '0' + smallInteger;
841 memcpy(&buffer[15], s.data(), stringSize * sizeof(UChar));
842 buffer[15 + stringSize] = '<';
843 buffer[16 + stringSize] = '/';
844 buffer[17 + stringSize] = 'f';
845 buffer[18 + stringSize] = 'o';
846 buffer[19 + stringSize] = 'n';
847 buffer[20 + stringSize] = 't';
848 buffer[21 + stringSize] = '>';
849 return jsNontrivialString(exec, UString(buffer, bufferSize, false));
852 return jsNontrivialString(exec, "<font size=\"" + a0.toString(exec) + "\">" + s + "</font>");
855 JSValue JSC_HOST_CALL stringProtoFuncAnchor(ExecState* exec, JSObject*, JSValue thisValue, const ArgList& args)
857 UString s = thisValue.toThisString(exec);
858 JSValue a0 = args.at(0);
859 return jsNontrivialString(exec, "<a name=\"" + a0.toString(exec) + "\">" + s + "</a>");
862 JSValue JSC_HOST_CALL stringProtoFuncLink(ExecState* exec, JSObject*, JSValue thisValue, const ArgList& args)
864 UString s = thisValue.toThisString(exec);
865 JSValue a0 = args.at(0);
866 UString linkText = a0.toString(exec);
868 unsigned linkTextSize = linkText.size();
869 unsigned stringSize = s.size();
870 unsigned bufferSize = 15 + linkTextSize + stringSize;
871 UChar* buffer = static_cast<UChar*>(tryFastMalloc(bufferSize * sizeof(UChar)));
873 return jsUndefined();
883 memcpy(&buffer[9], linkText.data(), linkTextSize * sizeof(UChar));
884 buffer[9 + linkTextSize] = '"';
885 buffer[10 + linkTextSize] = '>';
886 memcpy(&buffer[11 + linkTextSize], s.data(), stringSize * sizeof(UChar));
887 buffer[11 + linkTextSize + stringSize] = '<';
888 buffer[12 + linkTextSize + stringSize] = '/';
889 buffer[13 + linkTextSize + stringSize] = 'a';
890 buffer[14 + linkTextSize + stringSize] = '>';
891 return jsNontrivialString(exec, UString(buffer, bufferSize, false));