1// -*- c-basic-offset: 2 -*-
2/*
3 * This file is part of the KDE libraries
4 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
5 * Copyright (C) 2004 Apple Computer, Inc.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 *
21 */
22
23#include "string_object.h"
24#include "string_object.lut.h"
25#include <config-kjs.h>
26
27#include "JSWrapperObject.h"
28#include "error_object.h"
29#include "operations.h"
30#include "PropertyNameArray.h"
31#include "regexp_object.h"
32#include "commonunicode.h"
33#include <wtf/unicode/libc/UnicodeLibC.h>
34
35#if PLATFORM(WIN_OS)
36#include <windows.h>
37#endif
38
39using namespace WTF;
40
41namespace KJS {
42
43// ------------------------------ StringInstance ----------------------------
44
45const ClassInfo StringInstance::info = {"String", 0, 0, 0};
46
47StringInstance::StringInstance(JSObject *proto)
48 : JSWrapperObject(proto), m_conversionsCustomized(false)
49{
50 setInternalValue(jsString(""));
51}
52
53StringInstance::StringInstance(JSObject *proto, StringImp* string)
54 : JSWrapperObject(proto), m_conversionsCustomized(false)
55{
56 setInternalValue(string);
57}
58
59StringInstance::StringInstance(JSObject *proto, const UString &string)
60 : JSWrapperObject(proto), m_conversionsCustomized(false)
61{
62 setInternalValue(jsString(string));
63}
64
65JSValue *StringInstance::lengthGetter(ExecState*, JSObject*, const Identifier&, const PropertySlot &slot)
66{
67 return jsNumber(static_cast<StringInstance*>(slot.slotBase())->internalValue()->value().size());
68}
69
70JSValue *StringInstance::indexGetter(ExecState*, JSObject*, unsigned, const PropertySlot &slot)
71{
72 const UChar c = static_cast<StringInstance*>(slot.slotBase())->internalValue()->value()[slot.index()];
73 return jsString(UString(&c, 1));
74}
75
76bool StringInstance::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
77{
78 if (propertyName == exec->propertyNames().length) {
79 slot.setCustom(this, lengthGetter);
80 return true;
81 }
82
83 bool isStrictUInt32;
84 unsigned i = propertyName.toStrictUInt32(&isStrictUInt32);
85 unsigned length = internalValue()->value().size();
86 if (isStrictUInt32 && i < length) {
87 slot.setCustomIndex(this, i, indexGetter);
88 return true;
89 }
90
91 return JSObject::getOwnPropertySlot(exec, propertyName, slot);
92}
93
94bool StringInstance::getOwnPropertySlot(ExecState* exec, unsigned propertyName, PropertySlot& slot)
95{
96 unsigned length = internalValue()->value().size();
97 if (propertyName < length) {
98 slot.setCustomIndex(this, propertyName, indexGetter);
99 return true;
100 }
101
102 return JSObject::getOwnPropertySlot(exec, Identifier::from(propertyName), slot);
103}
104
105bool StringInstance::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& desc)
106{
107 if (propertyName == exec->propertyNames().length) {
108 desc.setPropertyDescriptorValues(exec, jsNumber(internalValue()->value().size()), ReadOnly|DontDelete|DontEnum);
109 return true;
110 }
111
112 return KJS::JSObject::getOwnPropertyDescriptor(exec, propertyName, desc);
113}
114
115void StringInstance::put(ExecState *exec, const Identifier &propertyName, JSValue *value, int attr)
116{
117 if (propertyName == exec->propertyNames().length)
118 return;
119
120 if (propertyName == exec->propertyNames().valueOf || propertyName == exec->propertyNames().toString)
121 m_conversionsCustomized = true;
122
123 JSObject::put(exec, propertyName, value, attr);
124}
125
126bool StringInstance::deleteProperty(ExecState *exec, const Identifier &propertyName)
127{
128 if (propertyName == exec->propertyNames().length)
129 return false;
130 return JSObject::deleteProperty(exec, propertyName);
131}
132
133void StringInstance::getOwnPropertyNames(ExecState* exec, PropertyNameArray& propertyNames, PropertyMap::PropertyMode mode)
134{
135 int size = internalValue()->getString().size();
136 for (int i = 0; i < size; i++)
137 propertyNames.add(Identifier(UString::from(i)));
138
139 if (mode == PropertyMap::IncludeDontEnumProperties)
140 propertyNames.add(exec->propertyNames().length);
141
142 return JSObject::getOwnPropertyNames(exec, propertyNames, mode);
143}
144
145UString StringInstance::toString(ExecState *exec) const
146{
147 if (prototype() == originalProto() && !conversionsCustomized() &&
148 !static_cast<StringPrototype*>(prototype())->conversionsCustomized())
149 return internalValue()->value();
150 else
151 return JSObject::toString(exec);
152}
153
154JSObject* StringInstance::valueClone(Interpreter* targetCtx) const
155{
156 return new StringInstance(targetCtx->builtinStringPrototype(), internalValue());
157}
158
159// ------------------------------ StringPrototype ---------------------------
160const ClassInfo StringPrototype::info = {"String", &StringInstance::info, &stringTable, 0};
161/* Source for string_object.lut.h
162@begin stringTable 26
163 toString StringProtoFunc::ToString DontEnum|Function 0
164 valueOf StringProtoFunc::ValueOf DontEnum|Function 0
165 charAt StringProtoFunc::CharAt DontEnum|Function 1
166 charCodeAt StringProtoFunc::CharCodeAt DontEnum|Function 1
167 concat StringProtoFunc::Concat DontEnum|Function 1
168 indexOf StringProtoFunc::IndexOf DontEnum|Function 1
169 lastIndexOf StringProtoFunc::LastIndexOf DontEnum|Function 1
170 match StringProtoFunc::Match DontEnum|Function 1
171 replace StringProtoFunc::Replace DontEnum|Function 2
172 search StringProtoFunc::Search DontEnum|Function 1
173 slice StringProtoFunc::Slice DontEnum|Function 2
174 split StringProtoFunc::Split DontEnum|Function 2
175 substr StringProtoFunc::Substr DontEnum|Function 2
176 substring StringProtoFunc::Substring DontEnum|Function 2
177 toLowerCase StringProtoFunc::ToLowerCase DontEnum|Function 0
178 toUpperCase StringProtoFunc::ToUpperCase DontEnum|Function 0
179 toLocaleLowerCase StringProtoFunc::ToLocaleLowerCase DontEnum|Function 0
180 toLocaleUpperCase StringProtoFunc::ToLocaleUpperCase DontEnum|Function 0
181 trim StringProtoFunc::Trim DontEnum|Function 0
182 localeCompare StringProtoFunc::LocaleCompare DontEnum|Function 1
183#
184# Under here: html extension, should only exist if KJS_PURE_ECMA is not defined
185# I guess we need to generate two hashtables in the .lut.h file, and use #ifdef
186# to select the right one... TODO. #####
187 big StringProtoFunc::Big DontEnum|Function 0
188 small StringProtoFunc::Small DontEnum|Function 0
189 blink StringProtoFunc::Blink DontEnum|Function 0
190 bold StringProtoFunc::Bold DontEnum|Function 0
191 fixed StringProtoFunc::Fixed DontEnum|Function 0
192 italics StringProtoFunc::Italics DontEnum|Function 0
193 strike StringProtoFunc::Strike DontEnum|Function 0
194 sub StringProtoFunc::Sub DontEnum|Function 0
195 sup StringProtoFunc::Sup DontEnum|Function 0
196 fontcolor StringProtoFunc::Fontcolor DontEnum|Function 1
197 fontsize StringProtoFunc::Fontsize DontEnum|Function 1
198 anchor StringProtoFunc::Anchor DontEnum|Function 1
199 link StringProtoFunc::Link DontEnum|Function 1
200 trimLeft StringProtoFunc::TrimLeft DontEnum|Function 0
201 trimRight StringProtoFunc::TrimRight DontEnum|Function 0
202@end
203*/
204// ECMA 15.5.4
205StringPrototype::StringPrototype(ExecState* exec, ObjectPrototype* objProto)
206 : StringInstance(objProto)
207{
208 // The constructor will be added later, after StringObjectImp has been built
209 putDirect(exec->propertyNames().length, jsNumber(0), DontDelete | ReadOnly | DontEnum);
210}
211
212bool StringPrototype::getOwnPropertySlot(ExecState *exec, const Identifier& propertyName, PropertySlot &slot)
213{
214 return getStaticFunctionSlot<StringProtoFunc, StringInstance>(exec, &stringTable, this, propertyName, slot);
215}
216
217// ------------------------------ StringProtoFunc ---------------------------
218
219StringProtoFunc::StringProtoFunc(ExecState* exec, int i, int len, const Identifier& name)
220 : InternalFunctionImp(static_cast<FunctionPrototype*>(exec->lexicalInterpreter()->builtinFunctionPrototype()), name)
221 , id(i)
222{
223 putDirect(exec->propertyNames().length, len, DontDelete | ReadOnly | DontEnum);
224}
225
226static inline void expandSourceRanges(UString::Range * & array, int& count, int& capacity)
227{
228 int newCapacity;
229 if (capacity == 0) {
230 newCapacity = 16;
231 } else {
232 newCapacity = capacity * 2;
233 }
234
235 UString::Range *newArray = new UString::Range[newCapacity];
236 for (int i = 0; i < count; i++) {
237 newArray[i] = array[i];
238 }
239
240 delete [] array;
241
242 capacity = newCapacity;
243 array = newArray;
244}
245
246static void pushSourceRange(UString::Range * & array, int& count, int& capacity, UString::Range range)
247{
248 if (count + 1 > capacity)
249 expandSourceRanges(array, count, capacity);
250
251 array[count] = range;
252 count++;
253}
254
255static inline void expandReplacements(UString * & array, int& count, int& capacity)
256{
257 int newCapacity;
258 if (capacity == 0) {
259 newCapacity = 16;
260 } else {
261 newCapacity = capacity * 2;
262 }
263
264 UString *newArray = new UString[newCapacity];
265 for (int i = 0; i < count; i++) {
266 newArray[i] = array[i];
267 }
268
269 delete [] array;
270
271 capacity = newCapacity;
272 array = newArray;
273}
274
275static void pushReplacement(UString * & array, int& count, int& capacity, UString replacement)
276{
277 if (count + 1 > capacity)
278 expandReplacements(array, count, capacity);
279
280 array[count] = replacement;
281 count++;
282}
283
284static inline UString substituteBackreferences(const UString &replacement, const UString &source, int *ovector, RegExp *reg)
285{
286 UString substitutedReplacement = replacement;
287
288 int i = -1;
289 while ((i = substitutedReplacement.find(UString("$"), i + 1)) != -1) {
290 if (i+1 == substitutedReplacement.size())
291 break;
292
293 unsigned short ref = substitutedReplacement[i+1].unicode();
294 int backrefStart = 0;
295 int backrefLength = 0;
296 int advance = 0;
297
298 if (ref == '$') { // "$$" -> "$"
299 substitutedReplacement = substitutedReplacement.substr(0, i + 1) + substitutedReplacement.substr(i + 2);
300 continue;
301 } else if (ref == '&') {
302 backrefStart = ovector[0];
303 backrefLength = ovector[1] - backrefStart;
304 } else if (ref == '`') {
305 backrefStart = 0;
306 backrefLength = ovector[0];
307 } else if (ref == '\'') {
308 backrefStart = ovector[1];
309 backrefLength = source.size() - backrefStart;
310 } else if (ref >= '0' && ref <= '9') {
311 // 1- and 2-digit back references are allowed
312 unsigned backrefIndex = ref - '0';
313 if (backrefIndex > (unsigned)reg->subPatterns())
314 continue;
315 if (substitutedReplacement.size() > i + 2) {
316 ref = substitutedReplacement[i+2].unicode();
317 if (ref >= '0' && ref <= '9') {
318 backrefIndex = 10 * backrefIndex + ref - '0';
319 if (backrefIndex > (unsigned)reg->subPatterns())
320 backrefIndex = backrefIndex / 10; // Fall back to the 1-digit reference
321 else
322 advance = 1;
323 }
324 }
325 backrefStart = ovector[2 * backrefIndex];
326 backrefLength = ovector[2 * backrefIndex + 1] - backrefStart;
327 } else
328 continue;
329
330 substitutedReplacement = substitutedReplacement.substr(0, i) + source.substr(backrefStart, backrefLength) + substitutedReplacement.substr(i + 2 + advance);
331 i += backrefLength - 1; // - 1 offsets 'i + 1'
332 }
333
334 return substitutedReplacement;
335}
336
337static inline int localeCompare(const UString& a, const UString& b)
338{
339#if PLATFORM(WIN_OS)
340 int retval = CompareStringW(LOCALE_USER_DEFAULT, 0,
341 reinterpret_cast<LPCWSTR>(a.data()), a.size(),
342 reinterpret_cast<LPCWSTR>(b.data()), b.size());
343 return !retval ? retval : retval - 2;
344#elif PLATFORM(CF)
345 CFStringRef sa = CFStringCreateWithCharactersNoCopy(kCFAllocatorDefault, reinterpret_cast<const UniChar*>(a.data()), a.size(), kCFAllocatorNull);
346 CFStringRef sb = CFStringCreateWithCharactersNoCopy(kCFAllocatorDefault, reinterpret_cast<const UniChar*>(b.data()), b.size(), kCFAllocatorNull);
347
348 int retval = CFStringCompare(sa, sb, kCFCompareLocalized);
349
350 CFRelease(sa);
351 CFRelease(sb);
352
353 return retval;
354#else
355 // ### use as fallback only. implement locale aware version.
356 // ### other browsers have more detailed return values than -1, 0 and 1
357 return compare(a, b);
358#endif
359}
360
361static JSValue *replace(ExecState *exec, const UString &source, JSValue *pattern, JSValue *replacement)
362{
363 JSObject *replacementFunction = 0;
364 UString replacementString;
365
366 if (replacement->isObject() && replacement->toObject(exec)->implementsCall())
367 replacementFunction = replacement->toObject(exec);
368 else
369 replacementString = replacement->toString(exec);
370
371 if (pattern->isObject() && static_cast<JSObject *>(pattern)->inherits(&RegExpImp::info)) {
372 RegExp *reg = static_cast<RegExpImp *>(pattern)->regExp();
373 bool global = reg->flags() & RegExp::Global;
374
375 RegExpObjectImp* regExpObj = static_cast<RegExpObjectImp*>(exec->lexicalInterpreter()->builtinRegExp());
376
377 int matchIndex = 0;
378 int lastIndex = 0;
379 int startPosition = 0;
380
381 UString::Range *sourceRanges = 0;
382 int sourceRangeCount = 0;
383 int sourceRangeCapacity = 0;
384 UString *replacements = 0;
385 int replacementCount = 0;
386 int replacementCapacity = 0;
387
388 // This is either a loop (if global is set) or a one-way (if not).
389 RegExpStringContext ctx(source);
390 do {
391 int *ovector;
392 UString matchString = regExpObj->performMatch(reg, exec, ctx, source, startPosition, &matchIndex, &ovector);
393 if (matchIndex == -1)
394 break;
395 int matchLen = matchString.size();
396
397 pushSourceRange(sourceRanges, sourceRangeCount, sourceRangeCapacity, UString::Range(lastIndex, matchIndex - lastIndex));
398 UString substitutedReplacement;
399 if (replacementFunction) {
400 int completeMatchStart = ovector[0];
401 List args;
402
403 args.append(jsString(matchString));
404
405 for (unsigned i = 0; i < reg->subPatterns(); i++) {
406 int matchStart = ovector[(i + 1) * 2];
407 int matchLen = ovector[(i + 1) * 2 + 1] - matchStart;
408
409 args.append(jsString(source.substr(matchStart, matchLen)));
410 }
411
412 args.append(jsNumber(completeMatchStart));
413 args.append(jsString(source));
414
415 substitutedReplacement = replacementFunction->call(exec, exec->dynamicInterpreter()->globalObject(),
416 args)->toString(exec);
417 } else {
418 substitutedReplacement = substituteBackreferences(replacementString, source, ovector, reg);
419 }
420 pushReplacement(replacements, replacementCount, replacementCapacity, substitutedReplacement);
421
422 lastIndex = matchIndex + matchLen;
423 startPosition = lastIndex;
424
425 // special case of empty match
426 if (matchLen == 0) {
427 startPosition++;
428 if (startPosition > source.size())
429 break;
430 }
431 } while (global);
432
433
434 if (lastIndex < source.size())
435 pushSourceRange(sourceRanges, sourceRangeCount, sourceRangeCapacity, UString::Range(lastIndex, source.size() - lastIndex));
436
437 UString result;
438 if (sourceRanges)
439 result = source.spliceSubstringsWithSeparators(sourceRanges, sourceRangeCount, replacements, replacementCount);
440
441 delete [] sourceRanges;
442 delete [] replacements;
443
444 return jsString(result);
445 }
446
447 // First arg is a string
448 UString patternString = pattern->toString(exec);
449 int matchPos = source.find(patternString);
450 int matchLen = patternString.size();
451 // Do the replacement
452 if (matchPos == -1)
453 return jsString(source);
454
455 if (replacementFunction) {
456 List args;
457
458 args.append(jsString(source.substr(matchPos, matchLen)));
459 args.append(jsNumber(matchPos));
460 args.append(jsString(source));
461
462 replacementString = replacementFunction->call(exec, exec->dynamicInterpreter()->globalObject(),
463 args)->toString(exec);
464 }
465
466 return jsString(source.substr(0, matchPos) + replacementString + source.substr(matchPos + matchLen));
467}
468
469static UnicodeSupport::StringConversionFunction toUpperF = Unicode::toUpper;
470static UnicodeSupport::StringConversionFunction toLowerF = Unicode::toLower;
471
472void StringProtoFunc::setToLowerFunction(UnicodeSupport::StringConversionFunction f)
473{
474 toLowerF = f;
475}
476
477void StringProtoFunc::setToUpperFunction(UnicodeSupport::StringConversionFunction f)
478{
479 toUpperF = f;
480}
481
482// ECMA 15.5.4.2 - 15.5.4.20
483JSValue *StringProtoFunc::callAsFunction(ExecState* exec, JSObject* thisObj, const List& args)
484{
485 JSValue* result = NULL;
486
487 // toString and valueOf are no generic function.
488 if (id == ToString || id == ValueOf) {
489 if (!thisObj || !thisObj->inherits(&StringInstance::info))
490 return throwError(exec, TypeError);
491
492 return jsString(static_cast<StringInstance*>(thisObj)->internalValue()->toString(exec));
493 }
494
495 UString u, u2, u3;
496 int pos, p0, i;
497 double dpos;
498 double d = 0.0;
499
500 UString s = thisObj->toString(exec);
501
502 int len = s.size();
503 JSValue *a0 = args[0];
504 JSValue *a1 = args[1];
505
506 switch (id) {
507 case ToString:
508 case ValueOf:
509 // handled above
510 break;
511 case CharAt:
512 // Other browsers treat an omitted parameter as 0 rather than NaN.
513 // That doesn't match the ECMA standard, but is needed for site compatibility.
514 dpos = a0->isUndefined() ? 0 : a0->toInteger(exec);
515 if (dpos >= 0 && dpos < len) // false for NaN
516 u = s.substr(static_cast<int>(dpos), 1);
517 else
518 u = "";
519 result = jsString(u);
520 break;
521 case CharCodeAt:
522 // Other browsers treat an omitted parameter as 0 rather than NaN.
523 // That doesn't match the ECMA standard, but is needed for site compatibility.
524 dpos = a0->isUndefined() ? 0 : a0->toInteger(exec);
525 if (dpos >= 0 && dpos < len) // false for NaN
526 result = jsNumber(s[static_cast<int>(dpos)].unicode());
527 else
528 result = jsNaN();
529 break;
530 case Concat: {
531 ListIterator it = args.begin();
532 for ( ; it != args.end() ; ++it) {
533 s += it->toString(exec);
534 }
535 result = jsString(s);
536 break;
537 }
538 case IndexOf:
539 u2 = a0->toString(exec);
540 if (a1->isUndefined())
541 dpos = 0;
542 else {
543 dpos = a1->toInteger(exec);
544 if (dpos >= 0) { // false for NaN
545 if (dpos > len)
546 dpos = len;
547 } else
548 dpos = 0;
549 }
550 result = jsNumber(s.find(u2, static_cast<int>(dpos)));
551 break;
552 case LastIndexOf:
553 u2 = a0->toString(exec);
554 d = a1->toNumber(exec);
555 if (a1->isUndefined() || KJS::isNaN(d))
556 dpos = len;
557 else {
558 dpos = a1->toInteger(exec);
559 if (dpos >= 0) { // false for NaN
560 if (dpos > len)
561 dpos = len;
562 } else
563 dpos = 0;
564 }
565 result = jsNumber(s.rfind(u2, static_cast<int>(dpos)));
566 break;
567 case Match:
568 case Search: {
569 u = s;
570 RegExp *reg, *tmpReg = 0;
571 RegExpImp *imp = 0;
572 if (a0->isObject() && static_cast<JSObject *>(a0)->inherits(&RegExpImp::info)) {
573 reg = static_cast<RegExpImp *>(a0)->regExp();
574 } else {
575 /*
576 * ECMA 15.5.4.12 String.prototype.search (regexp)
577 * If regexp is not an object whose [[Class]] property is "RegExp", it is
578 * replaced with the result of the expression new RegExp(regexp).
579 */
580 reg = tmpReg = new RegExp(a0->toString(exec), RegExp::None);
581 }
582 if (!reg->isValid()) {
583 delete tmpReg;
584 return throwError(exec, SyntaxError, "Invalid regular expression");
585 }
586 RegExpObjectImp* regExpObj = static_cast<RegExpObjectImp*>(exec->lexicalInterpreter()->builtinRegExp());
587
588 RegExpStringContext ctx(u);
589 UString mstr = regExpObj->performMatch(reg, exec, ctx, u, 0, &pos);
590
591 if (id == Search) {
592 result = jsNumber(pos);
593 } else {
594 // Exec
595 if ((reg->flags() & RegExp::Global) == 0) {
596 // case without 'g' flag is handled like RegExp.prototype.exec
597 if (mstr.isNull()) {
598 result = jsNull();
599 } else {
600 result = regExpObj->arrayOfMatches(exec,mstr);
601 }
602 } else {
603 // return array of matches
604 List list;
605 int lastIndex = 0;
606 while (pos >= 0) {
607 if (mstr.isNull())
608 list.append(jsUndefined());
609 else
610 list.append(jsString(mstr));
611 lastIndex = pos;
612 pos += mstr.isEmpty() ? 1 : mstr.size();
613 mstr = regExpObj->performMatch(reg, exec, ctx, u, pos, &pos);
614 }
615 if (imp)
616 imp->put(exec, "lastIndex", jsNumber(lastIndex), DontDelete|DontEnum);
617 if (list.isEmpty()) {
618 // if there are no matches at all, it's important to return
619 // Null instead of an empty array, because this matches
620 // other browsers and because Null is a false value.
621 result = jsNull();
622 } else {
623 result = exec->lexicalInterpreter()->builtinArray()->construct(exec, list);
624 }
625 }
626 }
627
628 delete tmpReg;
629 break;
630 }
631 case Replace:
632 result = replace(exec, s, a0, a1);
633 break;
634 case Slice:
635 {
636 // The arg processing is very much like ArrayProtoFunc::Slice
637 double start = a0->toInteger(exec);
638 double end = a1->isUndefined() ? len : a1->toInteger(exec);
639 double from = start < 0 ? len + start : start;
640 double to = end < 0 ? len + end : end;
641 if (to > from && to > 0 && from < len) {
642 if (from < 0)
643 from = 0;
644 if (to > len)
645 to = len;
646 result = jsString(s.substr(static_cast<int>(from), static_cast<int>(to - from)));
647 } else {
648 result = jsString("");
649 }
650 break;
651 }
652 case Split: {
653 JSObject *constructor = exec->lexicalInterpreter()->builtinArray();
654 JSObject *res = static_cast<JSObject *>(constructor->construct(exec,List::empty()));
655 result = res;
656 u = s;
657 i = p0 = 0;
658 uint32_t limit = a1->isUndefined() ? 0xFFFFFFFFU : a1->toUInt32(exec);
659 if (a0->isObject() && static_cast<JSObject *>(a0)->inherits(&RegExpImp::info)) {
660 RegExp *reg = static_cast<RegExpImp *>(a0)->regExp();
661
662 RegExpStringContext ctx(u);
663 bool error = false;
664 if (u.isEmpty() && !reg->match(ctx, u, &error, 0).isNull()) {
665
666 // empty string matched by regexp -> empty array
667 res->put(exec, exec->propertyNames().length, jsNumber(0));
668 break;
669 }
670 pos = 0;
671 while (!error && static_cast<uint32_t>(i) != limit && pos < u.size()) {
672 // TODO: back references
673 int mpos;
674 int *ovector = 0L;
675 UString mstr = reg->match(ctx, u, &error, pos, &mpos, &ovector);
676 delete [] ovector; ovector = 0L;
677 if (mpos < 0)
678 break;
679 pos = mpos + (mstr.isEmpty() ? 1 : mstr.size());
680 if (mpos != p0 || !mstr.isEmpty()) {
681 res->put(exec,i, jsString(u.substr(p0, mpos-p0)));
682 p0 = mpos + mstr.size();
683 i++;
684 }
685 }
686
687 if (error)
688 RegExpObjectImp::throwRegExpError(exec);
689
690 } else {
691 u2 = a0->toString(exec);
692 if (u2.isEmpty()) {
693 if (u.isEmpty()) {
694 // empty separator matches empty string -> empty array
695 put(exec, exec->propertyNames().length, jsNumber(0));
696 break;
697 } else {
698 while (static_cast<uint32_t>(i) != limit && i < u.size()-1)
699 res->put(exec, i++, jsString(u.substr(p0++, 1)));
700 }
701 } else {
702 while (static_cast<uint32_t>(i) != limit && (pos = u.find(u2, p0)) >= 0) {
703 res->put(exec, i, jsString(u.substr(p0, pos-p0)));
704 p0 = pos + u2.size();
705 i++;
706 }
707 }
708 }
709 // add remaining string, if any
710 if (static_cast<uint32_t>(i) != limit)
711 res->put(exec, i++, jsString(u.substr(p0)));
712 res->put(exec, exec->propertyNames().length, jsNumber(i));
713 }
714 break;
715 case Substr: { //B.2.3
716 // Note: NaN is effectively handled as 0 here for both length
717 // and start, hence toInteger does fine, and removes worries
718 // about weird comparison results below.
719 int len = s.size();
720 double start = a0->toInteger(exec);
721 double length = a1->isUndefined() ? len : a1->toInteger(exec);
722
723 if (start >= len)
724 return jsString("");
725 if (length < 0)
726 return jsString("");
727 if (start < 0) {
728 start += s.size();
729 if (start < 0)
730 start = 0;
731 }
732
733 if (length > len - start)
734 length = len - start;
735
736 result = jsString(s.substr(static_cast<int>(start), static_cast<int>(length)));
737 break;
738 }
739 case Substring: {
740 double start = a0->toNumber(exec);
741 double end = a1->toNumber(exec);
742 if (isNaN(start))
743 start = 0;
744 if (isNaN(end))
745 end = 0;
746 if (start < 0)
747 start = 0;
748 if (end < 0)
749 end = 0;
750 if (start > len)
751 start = len;
752 if (end > len)
753 end = len;
754 if (a1->isUndefined())
755 end = len;
756 if (start > end) {
757 double temp = end;
758 end = start;
759 start = temp;
760 }
761 result = jsString(s.substr((int)start, (int)end-(int)start));
762 }
763 break;
764 case ToLowerCase:
765 case ToLocaleLowerCase: { // FIXME: See http://www.unicode.org/Public/UNIDATA/SpecialCasing.txt for locale-sensitive mappings that aren't implemented.
766 u = s;
767 u.copyForWriting();
768 uint16_t* dataPtr = reinterpret_cast<uint16_t*>(u.rep()->data());
769 uint16_t* destIfNeeded;
770
771 int len = toLowerF(dataPtr, u.size(), destIfNeeded);
772 if (len >= 0)
773 result = jsString(UString(reinterpret_cast<UChar*>(destIfNeeded ? destIfNeeded : dataPtr), len));
774 else
775 result = jsString(s);
776
777 free(destIfNeeded);
778 break;
779 }
780 case ToUpperCase:
781 case ToLocaleUpperCase: { // FIXME: See http://www.unicode.org/Public/UNIDATA/SpecialCasing.txt for locale-sensitive mappings that aren't implemented.
782 u = s;
783 u.copyForWriting();
784 uint16_t* dataPtr = reinterpret_cast<uint16_t*>(u.rep()->data());
785 uint16_t* destIfNeeded;
786
787 int len = toUpperF(dataPtr, u.size(), destIfNeeded);
788 if (len >= 0)
789 result = jsString(UString(reinterpret_cast<UChar *>(destIfNeeded ? destIfNeeded : dataPtr), len));
790 else
791 result = jsString(s);
792
793 free(destIfNeeded);
794 break;
795 }
796 case LocaleCompare:
797 if (args.size() < 1)
798 return jsNumber(0);
799 return jsNumber(localeCompare(s, a0->toString(exec)));
800 case Trim:
801 case TrimRight:
802 case TrimLeft: {
803 const uint16_t* dataPtr = reinterpret_cast<uint16_t*>(s.rep()->data());
804
805 const int size = s.size();
806 int left = 0;
807 if (id != TrimRight)
808 while (left < size && CommonUnicode::isStrWhiteSpace(dataPtr[left]))
809 left++;
810
811 int right = size;
812 if (id != TrimLeft)
813 while (right > left && CommonUnicode::isStrWhiteSpace(dataPtr[right-1]))
814 right--;
815
816 result = jsString(s.substr(left, right-left));
817 break;
818 }
819#ifndef KJS_PURE_ECMA
820 case Big:
821 result = jsString("<big>" + s + "</big>");
822 break;
823 case Small:
824 result = jsString("<small>" + s + "</small>");
825 break;
826 case Blink:
827 result = jsString("<blink>" + s + "</blink>");
828 break;
829 case Bold:
830 result = jsString("<b>" + s + "</b>");
831 break;
832 case Fixed:
833 result = jsString("<tt>" + s + "</tt>");
834 break;
835 case Italics:
836 result = jsString("<i>" + s + "</i>");
837 break;
838 case Strike:
839 result = jsString("<strike>" + s + "</strike>");
840 break;
841 case Sub:
842 result = jsString("<sub>" + s + "</sub>");
843 break;
844 case Sup:
845 result = jsString("<sup>" + s + "</sup>");
846 break;
847 case Fontcolor:
848 result = jsString("<font color=\"" + a0->toString(exec) + "\">" + s + "</font>");
849 break;
850 case Fontsize:
851 result = jsString("<font size=\"" + a0->toString(exec) + "\">" + s + "</font>");
852 break;
853 case Anchor:
854 result = jsString("<a name=\"" + a0->toString(exec) + "\">" + s + "</a>");
855 break;
856 case Link:
857 result = jsString("<a href=\"" + a0->toString(exec) + "\">" + s + "</a>");
858 break;
859#endif
860 }
861
862 return result;
863}
864
865// ------------------------------ StringObjectImp ------------------------------
866
867StringObjectImp::StringObjectImp(ExecState* exec,
868 FunctionPrototype* funcProto,
869 StringPrototype* stringProto)
870 : InternalFunctionImp(funcProto)
871{
872 // ECMA 15.5.3.1 String.prototype
873 putDirect(exec->propertyNames().prototype, stringProto, DontEnum|DontDelete|ReadOnly);
874
875 putDirectFunction(new StringObjectFuncImp(exec, funcProto, exec->propertyNames().fromCharCode), DontEnum);
876
877 // no. of arguments for constructor
878 putDirect(exec->propertyNames().length, jsNumber(1), ReadOnly|DontDelete|DontEnum);
879}
880
881
882bool StringObjectImp::implementsConstruct() const
883{
884 return true;
885}
886
887// ECMA 15.5.2
888JSObject *StringObjectImp::construct(ExecState *exec, const List &args)
889{
890 JSObject *proto = exec->lexicalInterpreter()->builtinStringPrototype();
891 if (args.size() == 0)
892 return new StringInstance(proto);
893 return new StringInstance(proto, args.begin()->toString(exec));
894}
895
896// ECMA 15.5.1
897JSValue *StringObjectImp::callAsFunction(ExecState *exec, JSObject* /*thisObj*/, const List &args)
898{
899 if (args.isEmpty())
900 return jsString("");
901 else {
902 JSValue *v = args[0];
903 return jsString(v->toString(exec));
904 }
905}
906
907// ------------------------------ StringObjectFuncImp --------------------------
908
909// ECMA 15.5.3.2 fromCharCode()
910StringObjectFuncImp::StringObjectFuncImp(ExecState* exec, FunctionPrototype* funcProto, const Identifier& name)
911 : InternalFunctionImp(funcProto, name)
912{
913 putDirect(exec->propertyNames().length, jsNumber(1), DontDelete|ReadOnly|DontEnum);
914}
915
916JSValue *StringObjectFuncImp::callAsFunction(ExecState *exec, JSObject* /*thisObj*/, const List &args)
917{
918 UString s;
919 if (args.size()) {
920 UChar *buf = static_cast<UChar *>(fastMalloc(args.size() * sizeof(UChar)));
921 UChar *p = buf;
922 ListIterator it = args.begin();
923 while (it != args.end()) {
924 unsigned short u = it->toUInt16(exec);
925 *p++ = UChar(u);
926 it++;
927 }
928 s = UString(buf, args.size(), false);
929 } else
930 s = "";
931
932 return jsString(s);
933}
934
935}
936