Debian Codegraph @

rhino/src/org/mozilla/javascript/NativeString.java

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 package org.mozilla.javascript;
8
9 import org.mozilla.javascript.regexp.NativeRegExp;
10
11 import java.text.Collator;
12 import java.text.Normalizer;
13
14 import static org.mozilla.javascript.ScriptRuntime.rangeError;
15 import static org.mozilla.javascript.ScriptRuntimeES6.requireObjectCoercible;
16
17 /**
18 * This class implements the String native object.
19 *
20 * See ECMA 15.5.
21 *
22 * String methods for dealing with regular expressions are
23 * ported directly from C. Latest port is from version 1.40.12.19
24 * in the JSFUN13_BRANCH.
25 *
26 * @author Mike McCabe
27 * @author Norris Boyd
28 */
29 final class NativeString extends IdScriptableObject
30 {
31 static final long serialVersionUID = 920268368584188687L;
32
33 private static final Object STRING_TAG = "String";
34
35 static void init(Scriptable scope, boolean sealed)
36 {
37 NativeString obj = new NativeString("");
38 obj.exportAsJSClass(MAX_PROTOTYPE_ID, scope, sealed);
39 }
40
41 NativeString(CharSequence s) {
42 string = s;
43 }
44
45 @Override
46 public String getClassName() {
47 return "String";
48 }
49
50 private static final int
51 Id_length = 1,
52 MAX_INSTANCE_ID = 1;
53
54 @Override
55 protected int getMaxInstanceId()
56 {
57 return MAX_INSTANCE_ID;
58 }
59
60 @Override
61 protected int findInstanceIdInfo(String s)
62 {
63 if (s.equals("length")) {
64 return instanceIdInfo(DONTENUM | READONLY | PERMANENT, Id_length);
65 }
66 return super.findInstanceIdInfo(s);
67 }
68
69 @Override
70 protected String getInstanceIdName(int id)
71 {
72 if (id == Id_length) { return "length"; }
73 return super.getInstanceIdName(id);
74 }
75
76 @Override
77 protected Object getInstanceIdValue(int id)
78 {
79 if (id == Id_length) {
80 return ScriptRuntime.wrapInt(string.length());
81 }
82 return super.getInstanceIdValue(id);
83 }
84
85 @Override
86 protected void fillConstructorProperties(IdFunctionObject ctor)
87 {
88 addIdFunctionProperty(ctor, STRING_TAG, ConstructorId_fromCharCode,
89 "fromCharCode", 1);
90 addIdFunctionProperty(ctor, STRING_TAG,
91 ConstructorId_charAt, "charAt", 2);
92 addIdFunctionProperty(ctor, STRING_TAG,
93 ConstructorId_charCodeAt, "charCodeAt", 2);
94 addIdFunctionProperty(ctor, STRING_TAG,
95 ConstructorId_indexOf, "indexOf", 2);
96 addIdFunctionProperty(ctor, STRING_TAG,
97 ConstructorId_lastIndexOf, "lastIndexOf", 2);
98 addIdFunctionProperty(ctor, STRING_TAG,
99 ConstructorId_split, "split", 3);
100 addIdFunctionProperty(ctor, STRING_TAG,
101 ConstructorId_substring, "substring", 3);
102 addIdFunctionProperty(ctor, STRING_TAG,
103 ConstructorId_toLowerCase, "toLowerCase", 1);
104 addIdFunctionProperty(ctor, STRING_TAG,
105 ConstructorId_toUpperCase, "toUpperCase", 1);
106 addIdFunctionProperty(ctor, STRING_TAG,
107 ConstructorId_substr, "substr", 3);
108 addIdFunctionProperty(ctor, STRING_TAG,
109 ConstructorId_concat, "concat", 2);
110 addIdFunctionProperty(ctor, STRING_TAG,
111 ConstructorId_slice, "slice", 3);
112 addIdFunctionProperty(ctor, STRING_TAG,
113 ConstructorId_equalsIgnoreCase, "equalsIgnoreCase", 2);
114 addIdFunctionProperty(ctor, STRING_TAG,
115 ConstructorId_match, "match", 2);
116 addIdFunctionProperty(ctor, STRING_TAG,
117 ConstructorId_search, "search", 2);
118 addIdFunctionProperty(ctor, STRING_TAG,
119 ConstructorId_replace, "replace", 2);
120 addIdFunctionProperty(ctor, STRING_TAG,
121 ConstructorId_localeCompare, "localeCompare", 2);
122 addIdFunctionProperty(ctor, STRING_TAG,
123 ConstructorId_toLocaleLowerCase, "toLocaleLowerCase", 1);
124 super.fillConstructorProperties(ctor);
125 }
126
127 @Override
128 protected void initPrototypeId(int id)
129 {
130 if (id == SymbolId_iterator) {
131 initPrototypeMethod(STRING_TAG, id, SymbolKey.ITERATOR, "[Symbol.iterator]", 0);
132 return;
133 }
134
135 String s, fnName = null;
136 int arity;
137 switch (id) {
138 case Id_constructor: arity=1; s="constructor"; break;
139 case Id_toString: arity=0; s="toString"; break;
140 case Id_toSource: arity=0; s="toSource"; break;
141 case Id_valueOf: arity=0; s="valueOf"; break;
142 case Id_charAt: arity=1; s="charAt"; break;
143 case Id_charCodeAt: arity=1; s="charCodeAt"; break;
144 case Id_indexOf: arity=1; s="indexOf"; break;
145 case Id_lastIndexOf: arity=1; s="lastIndexOf"; break;
146 case Id_split: arity=2; s="split"; break;
147 case Id_substring: arity=2; s="substring"; break;
148 case Id_toLowerCase: arity=0; s="toLowerCase"; break;
149 case Id_toUpperCase: arity=0; s="toUpperCase"; break;
150 case Id_substr: arity=2; s="substr"; break;
151 case Id_concat: arity=1; s="concat"; break;
152 case Id_slice: arity=2; s="slice"; break;
153 case Id_bold: arity=0; s="bold"; break;
154 case Id_italics: arity=0; s="italics"; break;
155 case Id_fixed: arity=0; s="fixed"; break;
156 case Id_strike: arity=0; s="strike"; break;
157 case Id_small: arity=0; s="small"; break;
158 case Id_big: arity=0; s="big"; break;
159 case Id_blink: arity=0; s="blink"; break;
160 case Id_sup: arity=0; s="sup"; break;
161 case Id_sub: arity=0; s="sub"; break;
162 case Id_fontsize: arity=0; s="fontsize"; break;
163 case Id_fontcolor: arity=0; s="fontcolor"; break;
164 case Id_link: arity=0; s="link"; break;
165 case Id_anchor: arity=0; s="anchor"; break;
166 case Id_equals: arity=1; s="equals"; break;
167 case Id_equalsIgnoreCase: arity=1; s="equalsIgnoreCase"; break;
168 case Id_match: arity=1; s="match"; break;
169 case Id_search: arity=1; s="search"; break;
170 case Id_replace: arity=2; s="replace"; break;
171 case Id_localeCompare: arity=1; s="localeCompare"; break;
172 case Id_toLocaleLowerCase: arity=0; s="toLocaleLowerCase"; break;
173 case Id_toLocaleUpperCase: arity=0; s="toLocaleUpperCase"; break;
174 case Id_trim: arity=0; s="trim"; break;
175 case Id_trimLeft: arity=0; s="trimLeft"; break;
176 case Id_trimRight: arity=0; s="trimRight"; break;
177 case Id_includes: arity=1; s="includes"; break;
178 case Id_startsWith: arity=1; s="startsWith"; break;
179 case Id_endsWith: arity=1; s="endsWith"; break;
180 case Id_normalize: arity=0; s="normalize"; break;
181 case Id_repeat: arity=1; s="repeat"; break;
182 case Id_codePointAt: arity=1; s="codePointAt"; break;
183 default: throw new IllegalArgumentException(String.valueOf(id));
184 }
185 initPrototypeMethod(STRING_TAG, id, s, fnName, arity);
186 }
187
188 @Override
189 public Object execIdCall(IdFunctionObject f, Context cx, Scriptable scope,
190 Scriptable thisObj, Object[] args)
191 {
192 if (!f.hasTag(STRING_TAG)) {
193 return super.execIdCall(f, cx, scope, thisObj, args);
194 }
195 int id = f.methodId();
196 again:
197 for(;;) {
198 switch (id) {
199 case ConstructorId_charAt:
200 case ConstructorId_charCodeAt:
201 case ConstructorId_indexOf:
202 case ConstructorId_lastIndexOf:
203 case ConstructorId_split:
204 case ConstructorId_substring:
205 case ConstructorId_toLowerCase:
206 case ConstructorId_toUpperCase:
207 case ConstructorId_substr:
208 case ConstructorId_concat:
209 case ConstructorId_slice:
210 case ConstructorId_equalsIgnoreCase:
211 case ConstructorId_match:
212 case ConstructorId_search:
213 case ConstructorId_replace:
214 case ConstructorId_localeCompare:
215 case ConstructorId_toLocaleLowerCase: {
216 if (args.length > 0) {
217 thisObj = ScriptRuntime.toObject(cx, scope,
218 ScriptRuntime.toCharSequence(args[0]));
219 Object[] newArgs = new Object[args.length - 1];
220 for (int i = 0; i < newArgs.length; i++)
221 newArgs[i] = args[i + 1];
222 args = newArgs;
223 } else {
224 thisObj = ScriptRuntime.toObject(cx, scope,
225 ScriptRuntime.toCharSequence(thisObj));
226 }
227 id = -id;
228 continue again;
229 }
230
231 case ConstructorId_fromCharCode: {
232 int N = args.length;
233 if (N < 1)
234 return "";
235 StringBuilder sb = new StringBuilder(N);
236 for (int i = 0; i != N; ++i) {
237 sb.append(ScriptRuntime.toUint16(args[i]));
238 }
239 return sb.toString();
240 }
241
242 case Id_constructor: {
243 CharSequence s;
244 if (args.length == 0) {
245 s = "";
246 } else if
247 (ScriptRuntime.isSymbol(args[0]) &&
248 (thisObj != null)) {
249 // 19.4.3.2 et.al. Convert a symbol to a string with String() but not new String()
250 s = args[0].toString();
251 } else {
252 s = ScriptRuntime.toCharSequence(args[0]);
253 }
254 if (thisObj == null) {
255 // new String(val) creates a new String object.
256 return new NativeString(s);
257 }
258 // String(val) converts val to a string value.
259 return s instanceof String ? s : s.toString();
260 }
261
262 case Id_toString:
263 case Id_valueOf:
264 // ECMA 15.5.4.2: 'the toString function is not generic.
265 CharSequence cs = realThis(thisObj, f).string;
266 return cs instanceof String ? cs : cs.toString();
267
268 case Id_toSource: {
269 CharSequence s = realThis(thisObj, f).string;
270 return "(new String(\"" + ScriptRuntime.escapeString(s.toString()) + "\"))";
271 }
272
273 case Id_charAt:
274 case Id_charCodeAt: {
275 // See ECMA 15.5.4.[4,5]
276 CharSequence target = ScriptRuntime.toCharSequence(thisObj);
277 double pos = ScriptRuntime.toInteger(args, 0);
278 if (pos < 0 || pos >= target.length()) {
279 if (id == Id_charAt) return "";
280 else return ScriptRuntime.NaNobj;
281 }
282 char c = target.charAt((int) pos);
283 if (id == Id_charAt) return String.valueOf(c);
284 else return ScriptRuntime.wrapInt(c);
285 }
286
287 case Id_indexOf:
288 return ScriptRuntime.wrapInt(js_indexOf(Id_indexOf, ScriptRuntime.toString(thisObj), args));
289
290 case Id_includes:
291 case Id_startsWith:
292 case Id_endsWith:
293 String s = ScriptRuntime.toString(requireObjectCoercible(cx, thisObj, f));
294 if (args.length > 0 && args[0] instanceof NativeRegExp) {
295 throw ScriptRuntime.typeError2("msg.first.arg.not.regexp", String.class.getSimpleName(), f.getFunctionName());
296 }
297
298 int idx = js_indexOf(id, s, args);
299
300 if (id == Id_includes) {
301 return idx != -1;
302 } else if (id == Id_startsWith) {
303 return idx == 0;
304 } else if (id == Id_endsWith) {
305 return idx != -1;
306 }
307
308 case Id_lastIndexOf:
309 return ScriptRuntime.wrapInt(js_lastIndexOf(
310 ScriptRuntime.toString(thisObj), args));
311
312 case Id_split:
313 return ScriptRuntime.checkRegExpProxy(cx).
314 js_split(cx, scope, ScriptRuntime.toString(thisObj),
315 args);
316
317 case Id_substring:
318 return js_substring(cx, ScriptRuntime.toCharSequence(thisObj), args);
319
320 case Id_toLowerCase:
321 // See ECMA 15.5.4.11
322 return ScriptRuntime.toString(thisObj).toLowerCase(
323 ScriptRuntime.ROOT_LOCALE);
324
325 case Id_toUpperCase:
326 // See ECMA 15.5.4.12
327 return ScriptRuntime.toString(thisObj).toUpperCase(
328 ScriptRuntime.ROOT_LOCALE);
329
330 case Id_substr:
331 return js_substr(ScriptRuntime.toCharSequence(thisObj), args);
332
333 case Id_concat:
334 return js_concat(ScriptRuntime.toString(thisObj), args);
335
336 case Id_slice:
337 return js_slice(ScriptRuntime.toCharSequence(thisObj), args);
338
339 case Id_bold:
340 return tagify(thisObj, "b", null, null);
341
342 case Id_italics:
343 return tagify(thisObj, "i", null, null);
344
345 case Id_fixed:
346 return tagify(thisObj, "tt", null, null);
347
348 case Id_strike:
349 return tagify(thisObj, "strike", null, null);
350
351 case Id_small:
352 return tagify(thisObj, "small", null, null);
353
354 case Id_big:
355 return tagify(thisObj, "big", null, null);
356
357 case Id_blink:
358 return tagify(thisObj, "blink", null, null);
359
360 case Id_sup:
361 return tagify(thisObj, "sup", null, null);
362
363 case Id_sub:
364 return tagify(thisObj, "sub", null, null);
365
366 case Id_fontsize:
367 return tagify(thisObj, "font", "size", args);
368
369 case Id_fontcolor:
370 return tagify(thisObj, "font", "color", args);
371
372 case Id_link:
373 return tagify(thisObj, "a", "href", args);
374
375 case Id_anchor:
376 return tagify(thisObj, "a", "name", args);
377
378 case Id_equals:
379 case Id_equalsIgnoreCase: {
380 String s1 = ScriptRuntime.toString(thisObj);
381 String s2 = ScriptRuntime.toString(args, 0);
382 return ScriptRuntime.wrapBoolean(
383 (id == Id_equals) ? s1.equals(s2)
384 : s1.equalsIgnoreCase(s2));
385 }
386
387 case Id_match:
388 case Id_search:
389 case Id_replace: {
390 int actionType;
391 if (id == Id_match) {
392 actionType = RegExpProxy.RA_MATCH;
393 } else if (id == Id_search) {
394 actionType = RegExpProxy.RA_SEARCH;
395 } else {
396 actionType = RegExpProxy.RA_REPLACE;
397 }
398 return ScriptRuntime.checkRegExpProxy(cx).
399 action(cx, scope, thisObj, args, actionType);
400 }
401 // ECMA-262 1 5.5.4.9
402 case Id_localeCompare: {
403 // For now, create and configure a collator instance. I can't
404 // actually imagine that this'd be slower than caching them
405 // a la ClassCache, so we aren't trying to outsmart ourselves
406 // with a caching mechanism for now.
407 Collator collator = Collator.getInstance(cx.getLocale());
408 collator.setStrength(Collator.IDENTICAL);
409 collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
410 return ScriptRuntime.wrapNumber(collator.compare(
411 ScriptRuntime.toString(thisObj),
412 ScriptRuntime.toString(args, 0)));
413 }
414 case Id_toLocaleLowerCase: {
415 return ScriptRuntime.toString(thisObj)
416 .toLowerCase(cx.getLocale());
417 }
418 case Id_toLocaleUpperCase: {
419 return ScriptRuntime.toString(thisObj)
420 .toUpperCase(cx.getLocale());
421 }
422 case Id_trim: {
423 String str = ScriptRuntime.toString(requireObjectCoercible(cx, thisObj, f));
424 char[] chars = str.toCharArray();
425
426 int start = 0;
427 while (start < chars.length && ScriptRuntime.isJSWhitespaceOrLineTerminator(chars[start])) {
428 start++;
429 }
430 int end = chars.length;
431 while (end > start && ScriptRuntime.isJSWhitespaceOrLineTerminator(chars[end - 1])) {
432 end--;
433 }
434
435 return str.substring(start, end);
436 }
437 case Id_trimLeft: {
438 String str = ScriptRuntime.toString(thisObj);
439 char[] chars = str.toCharArray();
440
441 int start = 0;
442 while (start < chars.length && ScriptRuntime.isJSWhitespaceOrLineTerminator(chars[start])) {
443 start++;
444 }
445 int end = chars.length;
446
447 return str.substring(start, end);
448 }
449 case Id_trimRight:
450 {
451 String str = ScriptRuntime.toString(thisObj);
452 char[] chars = str.toCharArray();
453
454 int start = 0;
455
456 int end = chars.length;
457 while (end > start && ScriptRuntime.isJSWhitespaceOrLineTerminator(chars[end - 1])) {
458 end--;
459 }
460
461 return str.substring(start, end);
462 }
463 case Id_normalize:
464 {
465 String formStr = ScriptRuntime.toString(args, 0);
466
467 Normalizer.Form form;
468 if (Normalizer.Form.NFD.name().equals(formStr)) form = Normalizer.Form.NFD;
469 else if (Normalizer.Form.NFKC.name().equals(formStr)) form = Normalizer.Form.NFKC;
470 else if (Normalizer.Form.NFKD.name().equals(formStr)) form = Normalizer.Form.NFKD;
471 else if (Normalizer.Form.NFC.name().equals(formStr) || args.length == 0) form = Normalizer.Form.NFC;
472 else throw rangeError("The normalization form should be one of NFC, NFD, NFKC, NFKD");
473
474 return Normalizer.normalize(ScriptRuntime.toString(requireObjectCoercible(cx, thisObj, f)), form);
475 }
476
477 case Id_repeat:
478 {
479 return js_repeat(cx, thisObj, f, args);
480 }
481 case Id_codePointAt:
482 {
483 String str = ScriptRuntime.toString(requireObjectCoercible(cx, thisObj, f));
484 double cnt = ScriptRuntime.toInteger(args, 0);
485
486 return (cnt < 0 || cnt >= str.length())
487 ? Undefined.instance
488 : str.codePointAt((int) cnt);
489 }
490
491 case SymbolId_iterator:
492 return new NativeStringIterator(scope, thisObj);
493
494 }
495 throw new IllegalArgumentException("String.prototype has no method: " + f.getFunctionName());
496 }
497 }
498
499 private static NativeString realThis(Scriptable thisObj, IdFunctionObject f)
500 {
501 if (!(thisObj instanceof NativeString))
502 throw incompatibleCallError(f);
503 return (NativeString)thisObj;
504 }
505
506 /*
507 * HTML composition aids.
508 */
509 private static String tagify(Object thisObj, String tag,
510 String attribute, Object[] args)
511 {
512 String str = ScriptRuntime.toString(thisObj);
513 StringBuilder result = new StringBuilder();
514 result.append('<');
515 result.append(tag);
516 if (attribute != null) {
517 result.append(' ');
518 result.append(attribute);
519 result.append("=\"");
520 result.append(ScriptRuntime.toString(args, 0));
521 result.append('"');
522 }
523 result.append('>');
524 result.append(str);
525 result.append("</");
526 result.append(tag);
527 result.append('>');
528 return result.toString();
529 }
530
531 public CharSequence toCharSequence() {
532 return string;
533 }
534
535 @Override
536 public String toString() {
537 return string instanceof String ? (String)string : string.toString();
538 }
539
540 /* Make array-style property lookup work for strings.
541 * XXX is this ECMA? A version check is probably needed. In js too.
542 */
543 @Override
544 public Object get(int index, Scriptable start) {
545 if (0 <= index && index < string.length()) {
546 return String.valueOf(string.charAt(index));
547 }
548 return super.get(index, start);
549 }
550
551 @Override
552 public void put(int index, Scriptable start, Object value) {
553 if (0 <= index && index < string.length()) {
554 return;
555 }
556 super.put(index, start, value);
557 }
558
559 @Override
560 public boolean has(int index, Scriptable start) {
561 if (0 <= index && index < string.length()) {
562 return true;
563 }
564 return super.has(index, start);
565 }
566
567 @Override
568 protected Object[] getIds(boolean nonEnumerable, boolean getSymbols)
569 {
570 // In ES6, Strings have entries in the property map for each character.
571 Context cx = Context.getCurrentContext();
572 if ((cx != null) && (cx.getLanguageVersion() >= Context.VERSION_ES6)) {
573 Object[] sids = super.getIds(nonEnumerable, getSymbols);
574 Object[] a = new Object[sids.length + string.length()];
575 int i;
576 for (i = 0; i < string.length(); i++) {
577 a[i] = Integer.valueOf(i);
578 }
579 System.arraycopy(sids, 0, a, i, sids.length);
580 return a;
581 }
582 return super.getIds(nonEnumerable, getSymbols);
583 }
584
585 /*
586 *
587 * See ECMA 15.5.4.6. Uses Java String.indexOf()
588 * OPT to add - BMH searching from jsstr.c.
589 */
590 private static int js_indexOf(int methodId, String target, Object[] args) {
591 String searchStr = ScriptRuntime.toString(args, 0);
592 double position = ScriptRuntime.toInteger(args, 1);
593
594 if (position > target.length() && methodId != Id_startsWith && methodId != Id_endsWith) {
595 return -1;
596 } else {
597 if (position < 0) position = 0;
598 else if (position > target.length()) position = target.length();
599 else if (methodId == Id_endsWith && (position != position || position > target.length())) position = target.length();
600
601 if (Id_endsWith == methodId) {
602 if (args.length == 0 || args.length == 1 || (args.length == 2 && args[1] == Undefined.instance)) position = target.length();
603 return target.substring(0, (int)position).endsWith(searchStr) ? 0 : -1;
604 }
605 return methodId == Id_startsWith
606 ? target.startsWith(searchStr, (int)position) ? 0 : -1
607 : target.indexOf(searchStr, (int)position);
608 }
609 }
610
611 /*
612 *
613 * See ECMA 15.5.4.7
614 *
615 */
616 private static int js_lastIndexOf(String target, Object[] args) {
617 String search = ScriptRuntime.toString(args, 0);
618 double end = ScriptRuntime.toNumber(args, 1);
619
620 if (end != end || end > target.length())
621 end = target.length();
622 else if (end < 0)
623 end = 0;
624
625 return target.lastIndexOf(search, (int)end);
626 }
627
628
629 /*
630 * See ECMA 15.5.4.15
631 */
632 private static CharSequence js_substring(Context cx, CharSequence target,
633 Object[] args)
634 {
635 int length = target.length();
636 double start = ScriptRuntime.toInteger(args, 0);
637 double end;
638
639 if (start < 0)
640 start = 0;
641 else if (start > length)
642 start = length;
643
644 if (args.length <= 1 || args[1] == Undefined.instance) {
645 end = length;
646 } else {
647 end = ScriptRuntime.toInteger(args[1]);
648 if (end < 0)
649 end = 0;
650 else if (end > length)
651 end = length;
652
653 // swap if end < start
654 if (end < start) {
655 if (cx.getLanguageVersion() != Context.VERSION_1_2) {
656 double temp = start;
657 start = end;
658 end = temp;
659 } else {
660 // Emulate old JDK1.0 java.lang.String.substring()
661 end = start;
662 }
663 }
664 }
665 return target.subSequence((int)start, (int)end);
666 }
667
668 int getLength() {
669 return string.length();
670 }
671
672 /*
673 * Non-ECMA methods.
674 */
675 private static CharSequence js_substr(CharSequence target, Object[] args) {
676 if (args.length < 1)
677 return target;
678
679 double begin = ScriptRuntime.toInteger(args[0]);
680 double end;
681 int length = target.length();
682
683 if (begin < 0) {
684 begin += length;
685 if (begin < 0)
686 begin = 0;
687 } else if (begin > length) {
688 begin = length;
689 }
690
691 if (args.length == 1) {
692 end = length;
693 } else {
694 end = ScriptRuntime.toInteger(args[1]);
695 if (end < 0)
696 end = 0;
697 end += begin;
698 if (end > length)
699 end = length;
700 }
701
702 return target.subSequence((int)begin, (int)end);
703 }
704
705 /*
706 * Python-esque sequence operations.
707 */
708 private static String js_concat(String target, Object[] args) {
709 int N = args.length;
710 if (N == 0) { return target; }
711 else if (N == 1) {
712 String arg = ScriptRuntime.toString(args[0]);
713 return target.concat(arg);
714 }
715
716 // Find total capacity for the final string to avoid unnecessary
717 // re-allocations in StringBuilder
718 int size = target.length();
719 String[] argsAsStrings = new String[N];
720 for (int i = 0; i != N; ++i) {
721 String s = ScriptRuntime.toString(args[i]);
722 argsAsStrings[i] = s;
723 size += s.length();
724 }
725
726 StringBuilder result = new StringBuilder(size);
727 result.append(target);
728 for (int i = 0; i != N; ++i) {
729 result.append(argsAsStrings[i]);
730 }
731 return result.toString();
732 }
733
734 private static CharSequence js_slice(CharSequence target, Object[] args) {
735 double begin = args.length < 1 ? 0 : ScriptRuntime.toInteger(args[0]);
736 double end;
737 int length = target.length();
738 if (begin < 0) {
739 begin += length;
740 if (begin < 0)
741 begin = 0;
742 } else if (begin > length) {
743 begin = length;
744 }
745
746 if (args.length < 2 || args[1] == Undefined.instance) {
747 end = length;
748 } else {
749 end = ScriptRuntime.toInteger(args[1]);
750 if (end < 0) {
751 end += length;
752 if (end < 0)
753 end = 0;
754 } else if (end > length) {
755 end = length;
756 }
757 if (end < begin)
758 end = begin;
759 }
760 return target.subSequence((int) begin, (int) end);
761 }
762
763 private static String js_repeat(Context cx, Scriptable thisObj, IdFunctionObject f, Object[] args)
764 {
765 String str = ScriptRuntime.toString(requireObjectCoercible(cx, thisObj, f));
766 double cnt = ScriptRuntime.toInteger(args, 0);
767
768 if ((cnt < 0.0) || (cnt == Double.POSITIVE_INFINITY)) {
769 throw rangeError("Invalid count value");
770 }
771
772 if (cnt == 0.0 || str.length() == 0) {
773 return "";
774 }
775
776 long size = str.length() * (long)cnt;
777 // Check for overflow
778 if ((cnt > Integer.MAX_VALUE) || (size > Integer.MAX_VALUE)) {
779 throw rangeError("Invalid size or count value");
780 }
781
782 StringBuilder retval = new StringBuilder((int) size);
783 retval.append(str);
784
785 int i = 1;
786 int icnt = (int)cnt;
787 while (i <= (icnt / 2)) {
788 retval.append(retval);
789 i *= 2;
790 }
791 if (i < icnt) {
792 retval.append(retval.substring(0, str.length() * (icnt - i)));
793 }
794
795 return retval.toString();
796 }
797
798 @Override
799 protected int findPrototypeId(Symbol k)
800 {
801 if (SymbolKey.ITERATOR.equals(k)) {
802 return SymbolId_iterator;
803 }
804 return 0;
805 }
806
807 // #string_id_map#
808
809 @Override
810 protected int findPrototypeId(String s)
811 {
812 int id;
813 // #generated# Last update: 2016-03-04 20:51:44 GMT
814 L0: { id = 0; String X = null; int c;
815 L: switch (s.length()) {
816 case 3: c=s.charAt(2);
817 if (c=='b') { if (s.charAt(0)=='s' && s.charAt(1)=='u') {id=Id_sub; break L0;} }
818 else if (c=='g') { if (s.charAt(0)=='b' && s.charAt(1)=='i') {id=Id_big; break L0;} }
819 else if (c=='p') { if (s.charAt(0)=='s' && s.charAt(1)=='u') {id=Id_sup; break L0;} }
820 break L;
821 case 4: c=s.charAt(0);
822 if (c=='b') { X="bold";id=Id_bold; }
823 else if (c=='l') { X="link";id=Id_link; }
824 else if (c=='t') { X="trim";id=Id_trim; }
825 break L;
826 case 5: switch (s.charAt(4)) {
827 case 'd': X="fixed";id=Id_fixed; break L;
828 case 'e': X="slice";id=Id_slice; break L;
829 case 'h': X="match";id=Id_match; break L;
830 case 'k': X="blink";id=Id_blink; break L;
831 case 'l': X="small";id=Id_small; break L;
832 case 't': X="split";id=Id_split; break L;
833 } break L;
834 case 6: switch (s.charAt(1)) {
835 case 'e': c=s.charAt(0);
836 if (c=='r') { X="repeat";id=Id_repeat; }
837 else if (c=='s') { X="search";id=Id_search; }
838 break L;
839 case 'h': X="charAt";id=Id_charAt; break L;
840 case 'n': X="anchor";id=Id_anchor; break L;
841 case 'o': X="concat";id=Id_concat; break L;
842 case 'q': X="equals";id=Id_equals; break L;
843 case 't': X="strike";id=Id_strike; break L;
844 case 'u': X="substr";id=Id_substr; break L;
845 } break L;
846 case 7: switch (s.charAt(1)) {
847 case 'a': X="valueOf";id=Id_valueOf; break L;
848 case 'e': X="replace";id=Id_replace; break L;
849 case 'n': X="indexOf";id=Id_indexOf; break L;
850 case 't': X="italics";id=Id_italics; break L;
851 } break L;
852 case 8: switch (s.charAt(6)) {
853 case 'c': X="toSource";id=Id_toSource; break L;
854 case 'e': X="includes";id=Id_includes; break L;
855 case 'f': X="trimLeft";id=Id_trimLeft; break L;
856 case 'n': X="toString";id=Id_toString; break L;
857 case 't': X="endsWith";id=Id_endsWith; break L;
858 case 'z': X="fontsize";id=Id_fontsize; break L;
859 } break L;
860 case 9: switch (s.charAt(0)) {
861 case 'f': X="fontcolor";id=Id_fontcolor; break L;
862 case 'n': X="normalize";id=Id_normalize; break L;
863 case 's': X="substring";id=Id_substring; break L;
864 case 't': X="trimRight";id=Id_trimRight; break L;
865 } break L;
866 case 10: c=s.charAt(0);
867 if (c=='c') { X="charCodeAt";id=Id_charCodeAt; }
868 else if (c=='s') { X="startsWith";id=Id_startsWith; }
869 break L;
870 case 11: switch (s.charAt(2)) {
871 case 'L': X="toLowerCase";id=Id_toLowerCase; break L;
872 case 'U': X="toUpperCase";id=Id_toUpperCase; break L;
873 case 'd': X="codePointAt";id=Id_codePointAt; break L;
874 case 'n': X="constructor";id=Id_constructor; break L;
875 case 's': X="lastIndexOf";id=Id_lastIndexOf; break L;
876 } break L;
877 case 13: X="localeCompare";id=Id_localeCompare; break L;
878 case 16: X="equalsIgnoreCase";id=Id_equalsIgnoreCase; break L;
879 case 17: c=s.charAt(8);
880 if (c=='L') { X="toLocaleLowerCase";id=Id_toLocaleLowerCase; }
881 else if (c=='U') { X="toLocaleUpperCase";id=Id_toLocaleUpperCase; }
882 break L;
883 }
884 if (X!=null && X!=s && !X.equals(s)) id = 0;
885 break L0;
886 }
887 // #/generated#
888 return id;
889 }
890
891 private static final int
892 ConstructorId_fromCharCode = -1,
893
894 Id_constructor = 1,
895 Id_toString = 2,
896 Id_toSource = 3,
897 Id_valueOf = 4,
898 Id_charAt = 5,
899 Id_charCodeAt = 6,
900 Id_indexOf = 7,
901 Id_lastIndexOf = 8,
902 Id_split = 9,
903 Id_substring = 10,
904 Id_toLowerCase = 11,
905 Id_toUpperCase = 12,
906 Id_substr = 13,
907 Id_concat = 14,
908 Id_slice = 15,
909 Id_bold = 16,
910 Id_italics = 17,
911 Id_fixed = 18,
912 Id_strike = 19,
913 Id_small = 20,
914 Id_big = 21,
915 Id_blink = 22,
916 Id_sup = 23,
917 Id_sub = 24,
918 Id_fontsize = 25,
919 Id_fontcolor = 26,
920 Id_link = 27,
921 Id_anchor = 28,
922 Id_equals = 29,
923 Id_equalsIgnoreCase = 30,
924 Id_match = 31,
925 Id_search = 32,
926 Id_replace = 33,
927 Id_localeCompare = 34,
928 Id_toLocaleLowerCase = 35,
929 Id_toLocaleUpperCase = 36,
930 Id_trim = 37,
931 Id_trimLeft = 38,
932 Id_trimRight = 39,
933 Id_includes = 40,
934 Id_startsWith = 41,
935 Id_endsWith = 42,
936 Id_normalize = 43,
937 Id_repeat = 44,
938 Id_codePointAt = 45,
939 SymbolId_iterator = 46,
940 MAX_PROTOTYPE_ID = SymbolId_iterator;
941
942 // #/string_id_map#
943
944 private static final int
945 ConstructorId_charAt = -Id_charAt,
946 ConstructorId_charCodeAt = -Id_charCodeAt,
947 ConstructorId_indexOf = -Id_indexOf,
948 ConstructorId_lastIndexOf = -Id_lastIndexOf,
949 ConstructorId_split = -Id_split,
950 ConstructorId_substring = -Id_substring,
951 ConstructorId_toLowerCase = -Id_toLowerCase,
952 ConstructorId_toUpperCase = -Id_toUpperCase,
953 ConstructorId_substr = -Id_substr,
954 ConstructorId_concat = -Id_concat,
955 ConstructorId_slice = -Id_slice,
956 ConstructorId_equalsIgnoreCase = -Id_equalsIgnoreCase,
957 ConstructorId_match = -Id_match,
958 ConstructorId_search = -Id_search,
959 ConstructorId_replace = -Id_replace,
960 ConstructorId_localeCompare = -Id_localeCompare,
961 ConstructorId_toLocaleLowerCase = -Id_toLocaleLowerCase;
962
963 private CharSequence string;
964 }
965