initial import
[vuplus_webkit] / Source / JavaScriptCore / runtime / StringPrototype.cpp
1 /*
2  *  Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
3  *  Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
4  *  Copyright (C) 2009 Torch Mobile, Inc.
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Lesser General Public
8  *  License as published by the Free Software Foundation; either
9  *  version 2 of the License, or (at your option) any later version.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with this library; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #include "config.h"
23 #include "StringPrototype.h"
24
25 #include "CachedCall.h"
26 #include "Error.h"
27 #include "Executable.h"
28 #include "JSGlobalObjectFunctions.h"
29 #include "JSArray.h"
30 #include "JSFunction.h"
31 #include "JSStringBuilder.h"
32 #include "Lookup.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>
42
43 using namespace WTF;
44
45 namespace JSC {
46
47 ASSERT_CLASS_FITS_IN_CELL(StringPrototype);
48
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*);
81
82 }
83
84 #include "StringPrototype.lut.h"
85
86 namespace JSC {
87
88 const ClassInfo StringPrototype::s_info = { "String", &StringObject::s_info, 0, ExecState::stringTable };
89
90 /* Source for StringPrototype.lut.h
91 @begin stringTable 26
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
109
110     # toLocaleLowerCase and toLocaleUpperCase are currently identical to toLowerCase and toUpperCase
111     toLocaleLowerCase     stringProtoFuncToLowerCase       DontEnum|Function       0
112     toLocaleUpperCase     stringProtoFuncToUpperCase       DontEnum|Function       0
113
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
130 @end
131 */
132
133 // ECMA 15.5.4
134 StringPrototype::StringPrototype(ExecState* exec, Structure* structure)
135     : StringObject(exec->globalData(), structure)
136 {
137 }
138
139 void StringPrototype::finishCreation(ExecState* exec, JSGlobalObject*, JSString* nameAndMessage)
140 {
141     Base::finishCreation(exec->globalData(), nameAndMessage);
142     ASSERT(inherits(&s_info));
143
144     // The constructor will be added later, after StringConstructor has been built
145     putDirectWithoutTransition(exec->globalData(), exec->propertyNames().length, jsNumber(0), DontDelete | ReadOnly | DontEnum);
146 }
147
148 bool StringPrototype::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot &slot)
149 {
150     return getStaticFunctionSlot<StringObject>(exec, ExecState::stringTable(exec), this, propertyName, slot);
151 }
152
153 bool StringPrototype::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
154 {
155     return getStaticFunctionDescriptor<StringObject>(exec, ExecState::stringTable(exec), this, propertyName, descriptor);
156 }
157
158 // ------------------------------ Functions --------------------------
159
160 static NEVER_INLINE UString substituteBackreferencesSlow(const UString& replacement, const UString& source, const int* ovector, RegExp* reg, size_t i)
161 {
162     Vector<UChar> substitutedReplacement;
163     int offset = 0;
164     do {
165         if (i + 1 == replacement.length())
166             break;
167
168         UChar ref = replacement[i + 1];
169         if (ref == '$') {
170             // "$$" -> "$"
171             ++i;
172             substitutedReplacement.append(replacement.characters() + offset, i - offset);
173             offset = i + 1;
174             continue;
175         }
176
177         int backrefStart;
178         int backrefLength;
179         int advance = 0;
180         if (ref == '&') {
181             backrefStart = ovector[0];
182             backrefLength = ovector[1] - backrefStart;
183         } else if (ref == '`') {
184             backrefStart = 0;
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())
193                 continue;
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
200                     else
201                         advance = 1;
202                 }
203             }
204             if (!backrefIndex)
205                 continue;
206             backrefStart = ovector[2 * backrefIndex];
207             backrefLength = ovector[2 * backrefIndex + 1] - backrefStart;
208         } else
209             continue;
210
211         if (i - offset)
212             substitutedReplacement.append(replacement.characters() + offset, i - offset);
213         i += 1 + advance;
214         offset = i + 1;
215         if (backrefStart >= 0)
216             substitutedReplacement.append(source.characters() + backrefStart, backrefLength);
217     } while ((i = replacement.find('$', i + 1)) != notFound);
218
219     if (replacement.length() - offset)
220         substitutedReplacement.append(replacement.characters() + offset, replacement.length() - offset);
221
222     substitutedReplacement.shrinkToFit();
223     return UString::adopt(substitutedReplacement);
224 }
225
226 static inline UString substituteBackreferences(const UString& replacement, const UString& source, const int* ovector, RegExp* reg)
227 {
228     size_t i = replacement.find('$', 0);
229     if (UNLIKELY(i != notFound))
230         return substituteBackreferencesSlow(replacement, source, ovector, reg, i);
231     return replacement;
232 }
233
234 static inline int localeCompare(const UString& a, const UString& b)
235 {
236     return Collator::userDefault()->collate(reinterpret_cast<const ::UChar*>(a.characters()), a.length(), reinterpret_cast<const ::UChar*>(b.characters()), b.length());
237 }
238
239 struct StringRange {
240 public:
241     StringRange(int pos, int len)
242         : position(pos)
243         , length(len)
244     {
245     }
246
247     StringRange()
248     {
249     }
250
251     int position;
252     int length;
253 };
254
255 static ALWAYS_INLINE JSValue jsSpliceSubstrings(ExecState* exec, JSString* sourceVal, const UString& source, const StringRange* substringRanges, int rangeCount)
256 {
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)
262             return sourceVal;
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)));
265     }
266
267     int totalLength = 0;
268     for (int i = 0; i < rangeCount; i++)
269         totalLength += substringRanges[i].length;
270
271     if (!totalLength)
272         return jsString(exec, "");
273
274     UChar* buffer;
275     RefPtr<StringImpl> impl = StringImpl::tryCreateUninitialized(totalLength, buffer);
276     if (!impl)
277         return throwOutOfMemoryError(exec);
278
279     int bufferPos = 0;
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);
283             bufferPos += srcLen;
284         }
285     }
286
287     return jsString(exec, impl.release());
288 }
289
290 static ALWAYS_INLINE JSValue jsSpliceSubstringsWithSeparators(ExecState* exec, JSString* sourceVal, const UString& source, const StringRange* substringRanges, int rangeCount, const UString* separators, int separatorCount)
291 {
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)
297             return sourceVal;
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)));
300     }
301
302     int totalLength = 0;
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();
307
308     if (!totalLength)
309         return jsString(exec, "");
310
311     UChar* buffer;
312     RefPtr<StringImpl> impl = StringImpl::tryCreateUninitialized(totalLength, buffer);
313     if (!impl)
314         return throwOutOfMemoryError(exec);
315
316     int maxCount = max(rangeCount, separatorCount);
317     int bufferPos = 0;
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);
322                 bufferPos += srcLen;
323             }
324         }
325         if (i < separatorCount) {
326             if (int sepLen = separators[i].length()) {
327                 StringImpl::copyChars(buffer + bufferPos, separators[i].characters(), sepLen);
328                 bufferPos += sepLen;
329             }
330         }
331     }
332
333     return jsString(exec, impl.release());
334 }
335
336 EncodedJSValue JSC_HOST_CALL stringProtoFuncReplace(ExecState* exec)
337 {
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();
345
346     UString replacementString;
347     CallData callData;
348     CallType callType = getCallData(replacement, callData);
349     if (callType == CallTypeNone)
350         replacementString = replacement.toString(exec);
351
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();
359
360         RegExpConstructor* regExpConstructor = exec->lexicalGlobalObject()->regExpConstructor();
361
362         // Optimization for substring removal (replace with empty).
363         if (global && callType == CallTypeNone && !replacementString.length()) {
364             int lastIndex = 0;
365             unsigned startPosition = 0;
366
367             Vector<StringRange, 16> sourceRanges;
368
369             while (true) {
370                 int matchIndex;
371                 int matchLen = 0;
372                 int* ovector;
373                 regExpConstructor->performMatch(*globalData, reg, source, startPosition, matchIndex, matchLen, &ovector);
374                 if (matchIndex < 0)
375                     break;
376
377                 if (lastIndex < matchIndex)
378                     sourceRanges.append(StringRange(lastIndex, matchIndex - lastIndex));
379
380                 lastIndex = matchIndex + matchLen;
381                 startPosition = lastIndex;
382
383                 // special case of empty match
384                 if (!matchLen) {
385                     startPosition++;
386                     if (startPosition > sourceLen)
387                         break;
388                 }
389             }
390
391             if (!lastIndex)
392                 return JSValue::encode(sourceVal);
393
394             if (static_cast<unsigned>(lastIndex) < sourceLen)
395                 sourceRanges.append(StringRange(lastIndex, sourceLen - lastIndex));
396
397             return JSValue::encode(jsSpliceSubstrings(exec, sourceVal, source, sourceRanges.data(), sourceRanges.size()));
398         }
399
400         int lastIndex = 0;
401         unsigned startPosition = 0;
402
403         Vector<StringRange, 16> sourceRanges;
404         Vector<UString, 16> replacements;
405
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());
414             while (true) {
415                 int matchIndex;
416                 int matchLen = 0;
417                 int* ovector;
418                 regExpConstructor->performMatch(*globalData, reg, source, startPosition, matchIndex, matchLen, &ovector);
419                 if (matchIndex < 0)
420                     break;
421
422                 sourceRanges.append(StringRange(lastIndex, matchIndex - lastIndex));
423
424                 int completeMatchStart = ovector[0];
425                 unsigned i = 0;
426                 for (; i < reg->numSubpatterns() + 1; ++i) {
427                     int matchStart = ovector[i * 2];
428                     int matchLen = ovector[i * 2 + 1] - matchStart;
429
430                     if (matchStart < 0)
431                         cachedCall.setArgument(i, jsUndefined());
432                     else
433                         cachedCall.setArgument(i, jsSubstring(exec, source, matchStart, matchLen));
434                 }
435
436                 cachedCall.setArgument(i++, jsNumber(completeMatchStart));
437                 cachedCall.setArgument(i++, sourceVal);
438
439                 cachedCall.setThis(exec->globalThisValue());
440                 JSValue result = cachedCall.call();
441                 if (LIKELY(result.isString()))
442                     replacements.append(asString(result)->value(exec));
443                 else
444                     replacements.append(result.toString(cachedCall.newCallFrame(exec)));
445                 if (exec->hadException())
446                     break;
447
448                 lastIndex = matchIndex + matchLen;
449                 startPosition = lastIndex;
450
451                 // special case of empty match
452                 if (!matchLen) {
453                     startPosition++;
454                     if (startPosition > sourceLen)
455                         break;
456                 }
457             }
458         } else {
459             do {
460                 int matchIndex;
461                 int matchLen = 0;
462                 int* ovector;
463                 regExpConstructor->performMatch(*globalData, reg, source, startPosition, matchIndex, matchLen, &ovector);
464                 if (matchIndex < 0)
465                     break;
466
467                 if (callType != CallTypeNone) {
468                     sourceRanges.append(StringRange(lastIndex, matchIndex - lastIndex));
469
470                     int completeMatchStart = ovector[0];
471                     MarkedArgumentBuffer args;
472
473                     for (unsigned i = 0; i < reg->numSubpatterns() + 1; ++i) {
474                         int matchStart = ovector[i * 2];
475                         int matchLen = ovector[i * 2 + 1] - matchStart;
476  
477                         if (matchStart < 0)
478                             args.append(jsUndefined());
479                         else
480                             args.append(jsSubstring(exec, source, matchStart, matchLen));
481                     }
482
483                     args.append(jsNumber(completeMatchStart));
484                     args.append(sourceVal);
485
486                     replacements.append(call(exec, replacement, callType, callData, exec->globalThisValue(), args).toString(exec));
487                     if (exec->hadException())
488                         break;
489                 } else {
490                     int replLen = replacementString.length();
491                     if (lastIndex < matchIndex || replLen) {
492                         sourceRanges.append(StringRange(lastIndex, matchIndex - lastIndex));
493  
494                         if (replLen)
495                             replacements.append(substituteBackreferences(replacementString, source, ovector, reg));
496                         else
497                             replacements.append(UString());
498                     }
499                 }
500
501                 lastIndex = matchIndex + matchLen;
502                 startPosition = lastIndex;
503
504                 // special case of empty match
505                 if (!matchLen) {
506                     startPosition++;
507                     if (startPosition > sourceLen)
508                         break;
509                 }
510             } while (global);
511         }
512
513         if (!lastIndex && replacements.isEmpty())
514             return JSValue::encode(sourceVal);
515
516         if (static_cast<unsigned>(lastIndex) < sourceLen)
517             sourceRanges.append(StringRange(lastIndex, sourceLen - lastIndex));
518
519         return JSValue::encode(jsSpliceSubstringsWithSeparators(exec, sourceVal, source, sourceRanges.data(), sourceRanges.size(), replacements.data(), replacements.size()));
520     }
521
522     // Not a regular expression, so treat the pattern as a string.
523
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));
528
529     const UString& source = sourceVal->value(exec);
530     size_t matchPos = source.find(patternString);
531
532     if (matchPos == notFound)
533         return JSValue::encode(sourceVal);
534
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);
541
542         replacementString = call(exec, replacement, callType, callData, exec->globalThisValue(), args).toString(exec);
543     }
544     
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)));
548 }
549
550 EncodedJSValue JSC_HOST_CALL stringProtoFuncToString(ExecState* exec)
551 {
552     JSValue thisValue = exec->hostThisValue();
553     // Also used for valueOf.
554
555     if (thisValue.isString())
556         return JSValue::encode(thisValue);
557
558     if (thisValue.inherits(&StringObject::s_info))
559         return JSValue::encode(asStringObject(thisValue)->internalValue());
560
561     return throwVMTypeError(exec);
562 }
563
564 EncodedJSValue JSC_HOST_CALL stringProtoFuncCharAt(ExecState* exec)
565 {
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);
572     if (a0.isUInt32()) {
573         uint32_t i = a0.asUInt32();
574         if (i < len)
575             return JSValue::encode(jsSingleCharacterSubstring(exec, s, i));
576         return JSValue::encode(jsEmptyString(exec));
577     }
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));
582 }
583
584 EncodedJSValue JSC_HOST_CALL stringProtoFuncCharCodeAt(ExecState* exec)
585 {
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);
592     if (a0.isUInt32()) {
593         uint32_t i = a0.asUInt32();
594         if (i < len)
595             return JSValue::encode(jsNumber(s.characters()[i]));
596         return JSValue::encode(jsNaN());
597     }
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());
602 }
603
604 EncodedJSValue JSC_HOST_CALL stringProtoFuncConcat(ExecState* exec)
605 {
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)));
612     }
613     if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
614         return throwVMTypeError(exec);
615     return JSValue::encode(jsString(exec, thisValue));
616 }
617
618 EncodedJSValue JSC_HOST_CALL stringProtoFuncIndexOf(ExecState* exec)
619 {
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();
625
626     JSValue a0 = exec->argument(0);
627     JSValue a1 = exec->argument(1);
628     UString u2 = a0.toString(exec);
629     int pos;
630     if (a1.isUndefined())
631         pos = 0;
632     else if (a1.isUInt32())
633         pos = min<uint32_t>(a1.asUInt32(), len);
634     else {
635         double dpos = a1.toInteger(exec);
636         if (dpos < 0)
637             dpos = 0;
638         else if (dpos > len)
639             dpos = len;
640         pos = static_cast<int>(dpos);
641     }
642
643     size_t result = s.find(u2, pos);
644     if (result == notFound)
645         return JSValue::encode(jsNumber(-1));
646     return JSValue::encode(jsNumber(result));
647 }
648
649 EncodedJSValue JSC_HOST_CALL stringProtoFuncLastIndexOf(ExecState* exec)
650 {
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();
656
657     JSValue a0 = exec->argument(0);
658     JSValue a1 = exec->argument(1);
659
660     UString u2 = a0.toString(exec);
661     double dpos = a1.toIntegerPreserveNaN(exec);
662     if (dpos < 0)
663         dpos = 0;
664     else if (!(dpos <= len)) // true for NaN
665         dpos = len;
666 #if OS(SYMBIAN)
667     // Work around for broken NaN compare operator
668     else if (isnan(dpos))
669         dpos = len;
670 #endif
671
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));
676 }
677
678 EncodedJSValue JSC_HOST_CALL stringProtoFuncMatch(ExecState* exec)
679 {
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();
685
686     JSValue a0 = exec->argument(0);
687
688     RegExp* reg;
689     if (a0.inherits(&RegExpObject::s_info))
690         reg = asRegExpObject(a0)->regExp();
691     else {
692         /*
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).
696          */
697         reg = RegExp::create(exec->globalData(), a0.toString(exec), NoFlags);
698     }
699     RegExpConstructor* regExpConstructor = exec->lexicalGlobalObject()->regExpConstructor();
700     int pos;
701     int matchLength = 0;
702     regExpConstructor->performMatch(*globalData, reg, s, 0, pos, matchLength);
703     if (!(reg->global())) {
704         // case without 'g' flag is handled like RegExp.prototype.exec
705         if (pos < 0)
706             return JSValue::encode(jsNull());
707         return JSValue::encode(regExpConstructor->arrayOfMatches(exec));
708     }
709
710     // return array of matches
711     MarkedArgumentBuffer list;
712     while (pos >= 0) {
713         list.append(jsSubstring(exec, s, pos, matchLength));
714         pos += matchLength == 0 ? 1 : matchLength;
715         regExpConstructor->performMatch(*globalData, reg, s, pos, pos, matchLength);
716     }
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());
722     }
723
724     return JSValue::encode(constructArray(exec, list));
725 }
726
727 EncodedJSValue JSC_HOST_CALL stringProtoFuncSearch(ExecState* exec)
728 {
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();
734
735     JSValue a0 = exec->argument(0);
736
737     RegExp* reg;
738     if (a0.inherits(&RegExpObject::s_info))
739         reg = asRegExpObject(a0)->regExp();
740     else { 
741         /*
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).
745          */
746         reg = RegExp::create(exec->globalData(), a0.toString(exec), NoFlags);
747     }
748     RegExpConstructor* regExpConstructor = exec->lexicalGlobalObject()->regExpConstructor();
749     int pos;
750     int matchLength = 0;
751     regExpConstructor->performMatch(*globalData, reg, s, 0, pos, matchLength);
752     return JSValue::encode(jsNumber(pos));
753 }
754
755 EncodedJSValue JSC_HOST_CALL stringProtoFuncSlice(ExecState* exec)
756 {
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();
762
763     JSValue a0 = exec->argument(0);
764     JSValue a1 = exec->argument(1);
765
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) {
772         if (from < 0)
773             from = 0;
774         if (to > len)
775             to = len;
776         return JSValue::encode(jsSubstring(exec, s, static_cast<unsigned>(from), static_cast<unsigned>(to) - static_cast<unsigned>(from)));
777     }
778
779     return JSValue::encode(jsEmptyString(exec));
780 }
781
782 EncodedJSValue JSC_HOST_CALL stringProtoFuncSplit(ExecState* exec)
783 {
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();
789
790     JSValue a0 = exec->argument(0);
791     JSValue a1 = exec->argument(1);
792
793     JSArray* result = constructEmptyArray(exec);
794     unsigned i = 0;
795     unsigned p0 = 0;
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);
802         }
803         unsigned pos = 0;
804         while (i != limit && pos < s.length()) {
805             Vector<int, 32> ovector;
806             int mpos = reg->match(*globalData, s, pos, &ovector);
807             if (mpos < 0)
808                 break;
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));
813                 p0 = mpos + mlen;
814             }
815             for (unsigned si = 1; si <= reg->numSubpatterns(); ++si) {
816                 int spos = ovector[si * 2];
817                 if (spos < 0)
818                     result->put(exec, i++, jsUndefined());
819                 else
820                     result->put(exec, i++, jsSubstring(exec, s, spos, ovector[si * 2 + 1] - spos));
821             }
822         }
823     } else {
824         UString u2 = a0.toString(exec);
825         if (u2.isEmpty()) {
826             if (s.isEmpty()) {
827                 // empty separator matches empty string -> empty array
828                 return JSValue::encode(result);
829             }
830             while (i != limit && p0 < s.length() - 1)
831                 result->put(exec, i++, jsSingleCharacterSubstring(exec, s, p0++));
832         } else {
833             size_t pos;
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();
837             }
838         }
839     }
840
841     // add remaining string
842     if (i != limit)
843         result->put(exec, i++, jsSubstring(exec, s, p0, s.length() - p0));
844
845     return JSValue::encode(result);
846 }
847
848 EncodedJSValue JSC_HOST_CALL stringProtoFuncSubstr(ExecState* exec)
849 {
850     JSValue thisValue = exec->hostThisValue();
851     unsigned len;
852     JSString* jsString = 0;
853     UString uString;
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);
860     } else {
861         uString = thisValue.toString(exec);
862         if (exec->hadException())
863             return JSValue::encode(jsUndefined());
864         len = uString.length();
865     }
866
867     JSValue a0 = exec->argument(0);
868     JSValue a1 = exec->argument(1);
869
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));
874     if (start < 0) {
875         start += len;
876         if (start < 0)
877             start = 0;
878     }
879     if (start + length > len)
880         length = len - start;
881     unsigned substringStart = static_cast<unsigned>(start);
882     unsigned substringLength = static_cast<unsigned>(length);
883     if (jsString)
884         return JSValue::encode(jsSubstring(exec, jsString, substringStart, substringLength));
885     return JSValue::encode(jsSubstring(exec, uString, substringStart, substringLength));
886 }
887
888 EncodedJSValue JSC_HOST_CALL stringProtoFuncSubstring(ExecState* exec)
889 {
890     JSValue thisValue = exec->hostThisValue();
891     int len;
892     JSString* jsString = 0;
893     UString uString;
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);
900     } else {
901         uString = thisValue.toString(exec);
902         if (exec->hadException())
903             return JSValue::encode(jsUndefined());
904         len = uString.length();
905     }
906
907     JSValue a0 = exec->argument(0);
908     JSValue a1 = exec->argument(1);
909
910     double start = a0.toNumber(exec);
911     double end;
912     if (!(start >= 0)) // check for negative values or NaN
913         start = 0;
914     else if (start > len)
915         start = len;
916     if (a1.isUndefined())
917         end = len;
918     else { 
919         end = a1.toNumber(exec);
920         if (!(end >= 0)) // check for negative values or NaN
921             end = 0;
922         else if (end > len)
923             end = len;
924     }
925     if (start > end) {
926         double temp = end;
927         end = start;
928         start = temp;
929     }
930     unsigned substringStart = static_cast<unsigned>(start);
931     unsigned substringLength = static_cast<unsigned>(end) - substringStart;
932     if (jsString)
933         return JSValue::encode(jsSubstring(exec, jsString, substringStart, substringLength));
934     return JSValue::encode(jsSubstring(exec, uString, substringStart, substringLength));
935 }
936
937 EncodedJSValue JSC_HOST_CALL stringProtoFuncToLowerCase(ExecState* exec)
938 {
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);
944
945     int sSize = s.length();
946     if (!sSize)
947         return JSValue::encode(sVal);
948
949     const UChar* sData = s.characters();
950     Vector<UChar> buffer(sSize);
951
952     UChar ored = 0;
953     for (int i = 0; i < sSize; i++) {
954         UChar c = sData[i];
955         ored |= c;
956         buffer[i] = toASCIILower(c);
957     }
958     if (!(ored & ~0x7f))
959         return JSValue::encode(jsString(exec, UString::adopt(buffer)));
960
961     bool error;
962     int length = Unicode::toLower(buffer.data(), sSize, sData, sSize, &error);
963     if (error) {
964         buffer.resize(length);
965         length = Unicode::toLower(buffer.data(), length, sData, sSize, &error);
966         if (error)
967             return JSValue::encode(sVal);
968     }
969     if (length == sSize) {
970         if (memcmp(buffer.data(), sData, length * sizeof(UChar)) == 0)
971             return JSValue::encode(sVal);
972     } else
973         buffer.resize(length);
974     return JSValue::encode(jsString(exec, UString::adopt(buffer)));
975 }
976
977 EncodedJSValue JSC_HOST_CALL stringProtoFuncToUpperCase(ExecState* exec)
978 {
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);
984
985     int sSize = s.length();
986     if (!sSize)
987         return JSValue::encode(sVal);
988
989     const UChar* sData = s.characters();
990     Vector<UChar> buffer(sSize);
991
992     UChar ored = 0;
993     for (int i = 0; i < sSize; i++) {
994         UChar c = sData[i];
995         ored |= c;
996         buffer[i] = toASCIIUpper(c);
997     }
998     if (!(ored & ~0x7f))
999         return JSValue::encode(jsString(exec, UString::adopt(buffer)));
1000
1001     bool error;
1002     int length = Unicode::toUpper(buffer.data(), sSize, sData, sSize, &error);
1003     if (error) {
1004         buffer.resize(length);
1005         length = Unicode::toUpper(buffer.data(), length, sData, sSize, &error);
1006         if (error)
1007             return JSValue::encode(sVal);
1008     }
1009     if (length == sSize) {
1010         if (memcmp(buffer.data(), sData, length * sizeof(UChar)) == 0)
1011             return JSValue::encode(sVal);
1012     } else
1013         buffer.resize(length);
1014     return JSValue::encode(jsString(exec, UString::adopt(buffer)));
1015 }
1016
1017 EncodedJSValue JSC_HOST_CALL stringProtoFuncLocaleCompare(ExecState* exec)
1018 {
1019     if (exec->argumentCount() < 1)
1020       return JSValue::encode(jsNumber(0));
1021
1022     JSValue thisValue = exec->hostThisValue();
1023     if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
1024         return throwVMTypeError(exec);
1025     UString s = thisValue.toString(exec);
1026
1027     JSValue a0 = exec->argument(0);
1028     return JSValue::encode(jsNumber(localeCompare(s, a0.toString(exec))));
1029 }
1030
1031 EncodedJSValue JSC_HOST_CALL stringProtoFuncBig(ExecState* exec)
1032 {
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>"));
1038 }
1039
1040 EncodedJSValue JSC_HOST_CALL stringProtoFuncSmall(ExecState* exec)
1041 {
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>"));
1047 }
1048
1049 EncodedJSValue JSC_HOST_CALL stringProtoFuncBlink(ExecState* exec)
1050 {
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>"));
1056 }
1057
1058 EncodedJSValue JSC_HOST_CALL stringProtoFuncBold(ExecState* exec)
1059 {
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>"));
1065 }
1066
1067 EncodedJSValue JSC_HOST_CALL stringProtoFuncFixed(ExecState* exec)
1068 {
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>"));
1074 }
1075
1076 EncodedJSValue JSC_HOST_CALL stringProtoFuncItalics(ExecState* exec)
1077 {
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>"));
1083 }
1084
1085 EncodedJSValue JSC_HOST_CALL stringProtoFuncStrike(ExecState* exec)
1086 {
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>"));
1092 }
1093
1094 EncodedJSValue JSC_HOST_CALL stringProtoFuncSub(ExecState* exec)
1095 {
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>"));
1101 }
1102
1103 EncodedJSValue JSC_HOST_CALL stringProtoFuncSup(ExecState* exec)
1104 {
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>"));
1110 }
1111
1112 EncodedJSValue JSC_HOST_CALL stringProtoFuncFontcolor(ExecState* exec)
1113 {
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>"));
1120 }
1121
1122 EncodedJSValue JSC_HOST_CALL stringProtoFuncFontsize(ExecState* exec)
1123 {
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);
1129
1130     uint32_t smallInteger;
1131     if (a0.getUInt32(smallInteger) && smallInteger <= 9) {
1132         unsigned stringSize = s.length();
1133         unsigned bufferSize = 22 + stringSize;
1134         UChar* buffer;
1135         PassRefPtr<StringImpl> impl = StringImpl::tryCreateUninitialized(bufferSize, buffer);
1136         if (!impl)
1137             return JSValue::encode(jsUndefined());
1138         buffer[0] = '<';
1139         buffer[1] = 'f';
1140         buffer[2] = 'o';
1141         buffer[3] = 'n';
1142         buffer[4] = 't';
1143         buffer[5] = ' ';
1144         buffer[6] = 's';
1145         buffer[7] = 'i';
1146         buffer[8] = 'z';
1147         buffer[9] = 'e';
1148         buffer[10] = '=';
1149         buffer[11] = '"';
1150         buffer[12] = '0' + smallInteger;
1151         buffer[13] = '"';
1152         buffer[14] = '>';
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));
1162     }
1163
1164     return JSValue::encode(jsMakeNontrivialString(exec, "<font size=\"", a0.toString(exec), "\">", s, "</font>"));
1165 }
1166
1167 EncodedJSValue JSC_HOST_CALL stringProtoFuncAnchor(ExecState* exec)
1168 {
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>"));
1175 }
1176
1177 EncodedJSValue JSC_HOST_CALL stringProtoFuncLink(ExecState* exec)
1178 {
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);
1185
1186     unsigned linkTextSize = linkText.length();
1187     unsigned stringSize = s.length();
1188     unsigned bufferSize = 15 + linkTextSize + stringSize;
1189     UChar* buffer;
1190     PassRefPtr<StringImpl> impl = StringImpl::tryCreateUninitialized(bufferSize, buffer);
1191     if (!impl)
1192         return JSValue::encode(jsUndefined());
1193     buffer[0] = '<';
1194     buffer[1] = 'a';
1195     buffer[2] = ' ';
1196     buffer[3] = 'h';
1197     buffer[4] = 'r';
1198     buffer[5] = 'e';
1199     buffer[6] = 'f';
1200     buffer[7] = '=';
1201     buffer[8] = '"';
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));
1211 }
1212
1213 enum {
1214     TrimLeft = 1,
1215     TrimRight = 2
1216 };
1217
1218 static inline bool isTrimWhitespace(UChar c)
1219 {
1220     return isStrWhiteSpace(c) || c == 0x200b;
1221 }
1222
1223 static inline JSValue trimString(ExecState* exec, JSValue thisValue, int trimKind)
1224 {
1225     if (thisValue.isUndefinedOrNull()) // CheckObjectCoercible
1226         return throwTypeError(exec);
1227     UString str = thisValue.toString(exec);
1228     unsigned left = 0;
1229     if (trimKind & TrimLeft) {
1230         while (left < str.length() && isTrimWhitespace(str[left]))
1231             left++;
1232     }
1233     unsigned right = str.length();
1234     if (trimKind & TrimRight) {
1235         while (right > left && isTrimWhitespace(str[right - 1]))
1236             right--;
1237     }
1238
1239     // Don't gc allocate a new string if we don't have to.
1240     if (left == 0 && right == str.length() && thisValue.isString())
1241         return thisValue;
1242
1243     return jsString(exec, str.substringSharingImpl(left, right - left));
1244 }
1245
1246 EncodedJSValue JSC_HOST_CALL stringProtoFuncTrim(ExecState* exec)
1247 {
1248     JSValue thisValue = exec->hostThisValue();
1249     return JSValue::encode(trimString(exec, thisValue, TrimLeft | TrimRight));
1250 }
1251
1252 EncodedJSValue JSC_HOST_CALL stringProtoFuncTrimLeft(ExecState* exec)
1253 {
1254     JSValue thisValue = exec->hostThisValue();
1255     return JSValue::encode(trimString(exec, thisValue, TrimLeft));
1256 }
1257
1258 EncodedJSValue JSC_HOST_CALL stringProtoFuncTrimRight(ExecState* exec)
1259 {
1260     JSValue thisValue = exec->hostThisValue();
1261     return JSValue::encode(trimString(exec, thisValue, TrimRight));
1262 }
1263     
1264     
1265 } // namespace JSC