20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package jdk.nashorn.internal.objects;
27
28 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
29 import static jdk.nashorn.internal.runtime.JSType.isRepresentableAsInt;
30 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
31 import static jdk.nashorn.internal.runtime.arrays.ArrayIndex.getArrayIndexNoThrow;
32 import static jdk.nashorn.internal.runtime.linker.Lookup.MH;
33
34 import java.lang.invoke.MethodHandle;
35 import java.lang.invoke.MethodHandles;
36 import java.text.Collator;
37 import java.util.ArrayList;
38 import java.util.Arrays;
39 import java.util.List;
40 import java.util.regex.Pattern;
41 import jdk.nashorn.internal.objects.annotations.Attribute;
42 import jdk.nashorn.internal.objects.annotations.Constructor;
43 import jdk.nashorn.internal.objects.annotations.Function;
44 import jdk.nashorn.internal.objects.annotations.Getter;
45 import jdk.nashorn.internal.objects.annotations.ScriptClass;
46 import jdk.nashorn.internal.objects.annotations.SpecializedConstructor;
47 import jdk.nashorn.internal.objects.annotations.SpecializedFunction;
48 import jdk.nashorn.internal.objects.annotations.Where;
49 import jdk.nashorn.internal.parser.Lexer;
50 import jdk.nashorn.internal.runtime.ConsString;
51 import jdk.nashorn.internal.runtime.JSType;
52 import jdk.nashorn.internal.runtime.ScriptFunction;
53 import jdk.nashorn.internal.runtime.ScriptObject;
54 import jdk.nashorn.internal.runtime.ScriptRuntime;
55 import jdk.nashorn.internal.runtime.arrays.ArrayIndex;
56 import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor;
57 import jdk.nashorn.internal.runtime.linker.NashornGuards;
58 import jdk.nashorn.internal.runtime.linker.PrimitiveLookup;
59 import org.dynalang.dynalink.linker.GuardedInvocation;
60
589 return new NativeArray(matches.toArray());
590 }
591
592 /**
593 * ECMA 15.5.4.11 String.prototype.replace (searchValue, replaceValue)
594 * @param self self reference
595 * @param string item to replace
596 * @param replacement item to replace it with
597 * @return string after replacement
598 */
599 @Function(attributes = Attribute.NOT_ENUMERABLE)
600 public static Object replace(final Object self, final Object string, final Object replacement) {
601 Global.checkObjectCoercible(self);
602
603 final String str = JSType.toString(self);
604
605 final NativeRegExp nativeRegExp;
606 if (string instanceof NativeRegExp) {
607 nativeRegExp = (NativeRegExp) string;
608 } else {
609 nativeRegExp = new NativeRegExp(Pattern.compile(JSType.toString(string), Pattern.LITERAL));
610 }
611
612 if (replacement instanceof ScriptFunction) {
613 return nativeRegExp.replace(str, "", (ScriptFunction)replacement);
614 }
615
616 return nativeRegExp.replace(str, JSType.toString(replacement), null);
617 }
618
619 /**
620 * ECMA 15.5.4.12 String.prototype.search (regexp)
621 *
622 * @param self self reference
623 * @param string string to search for
624 * @return offset where match occurred
625 */
626 @Function(attributes = Attribute.NOT_ENUMERABLE)
627 public static Object search(final Object self, final Object string) {
628 Global.checkObjectCoercible(self);
629
678 * @return array object in which splits have been placed
679 */
680 @Function(attributes = Attribute.NOT_ENUMERABLE)
681 public static Object split(final Object self, final Object separator, final Object limit) {
682 Global.checkObjectCoercible(self);
683
684 final String str = JSType.toString(self);
685
686 if (separator == UNDEFINED) {
687 return new NativeArray(new Object[]{str});
688 }
689
690 final long lim = (limit == UNDEFINED) ? JSType.MAX_UINT : JSType.toUint32(limit);
691
692 if (separator instanceof NativeRegExp) {
693 return ((NativeRegExp) separator).split(str, lim);
694 }
695
696 // when separator is a string, it has to be treated as a
697 // literal search string to be used for splitting.
698 return new NativeRegExp(Pattern.compile(JSType.toString(separator), Pattern.LITERAL)).split(str, lim);
699 }
700
701 /**
702 * ECMA B.2.3 String.prototype.substr (start, length)
703 *
704 * @param self self reference
705 * @param start start position
706 * @param length length of section
707 * @return substring given start and length of section
708 */
709 @Function(attributes = Attribute.NOT_ENUMERABLE)
710 public static Object substr(final Object self, final Object start, final Object length) {
711 final String str = JSType.toString(self);
712 final int strLength = str.length();
713
714 int intStart = JSType.toInteger(start);
715 if (intStart < 0) {
716 intStart = Math.max(intStart + strLength, 0);
717 }
718
|
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package jdk.nashorn.internal.objects;
27
28 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
29 import static jdk.nashorn.internal.runtime.JSType.isRepresentableAsInt;
30 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
31 import static jdk.nashorn.internal.runtime.arrays.ArrayIndex.getArrayIndexNoThrow;
32 import static jdk.nashorn.internal.runtime.linker.Lookup.MH;
33
34 import java.lang.invoke.MethodHandle;
35 import java.lang.invoke.MethodHandles;
36 import java.text.Collator;
37 import java.util.ArrayList;
38 import java.util.Arrays;
39 import java.util.List;
40 import jdk.nashorn.internal.objects.annotations.Attribute;
41 import jdk.nashorn.internal.objects.annotations.Constructor;
42 import jdk.nashorn.internal.objects.annotations.Function;
43 import jdk.nashorn.internal.objects.annotations.Getter;
44 import jdk.nashorn.internal.objects.annotations.ScriptClass;
45 import jdk.nashorn.internal.objects.annotations.SpecializedConstructor;
46 import jdk.nashorn.internal.objects.annotations.SpecializedFunction;
47 import jdk.nashorn.internal.objects.annotations.Where;
48 import jdk.nashorn.internal.parser.Lexer;
49 import jdk.nashorn.internal.runtime.ConsString;
50 import jdk.nashorn.internal.runtime.JSType;
51 import jdk.nashorn.internal.runtime.ScriptFunction;
52 import jdk.nashorn.internal.runtime.ScriptObject;
53 import jdk.nashorn.internal.runtime.ScriptRuntime;
54 import jdk.nashorn.internal.runtime.arrays.ArrayIndex;
55 import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor;
56 import jdk.nashorn.internal.runtime.linker.NashornGuards;
57 import jdk.nashorn.internal.runtime.linker.PrimitiveLookup;
58 import org.dynalang.dynalink.linker.GuardedInvocation;
59
588 return new NativeArray(matches.toArray());
589 }
590
591 /**
592 * ECMA 15.5.4.11 String.prototype.replace (searchValue, replaceValue)
593 * @param self self reference
594 * @param string item to replace
595 * @param replacement item to replace it with
596 * @return string after replacement
597 */
598 @Function(attributes = Attribute.NOT_ENUMERABLE)
599 public static Object replace(final Object self, final Object string, final Object replacement) {
600 Global.checkObjectCoercible(self);
601
602 final String str = JSType.toString(self);
603
604 final NativeRegExp nativeRegExp;
605 if (string instanceof NativeRegExp) {
606 nativeRegExp = (NativeRegExp) string;
607 } else {
608 nativeRegExp = NativeRegExp.flatRegExp(JSType.toString(string));
609 }
610
611 if (replacement instanceof ScriptFunction) {
612 return nativeRegExp.replace(str, "", (ScriptFunction)replacement);
613 }
614
615 return nativeRegExp.replace(str, JSType.toString(replacement), null);
616 }
617
618 /**
619 * ECMA 15.5.4.12 String.prototype.search (regexp)
620 *
621 * @param self self reference
622 * @param string string to search for
623 * @return offset where match occurred
624 */
625 @Function(attributes = Attribute.NOT_ENUMERABLE)
626 public static Object search(final Object self, final Object string) {
627 Global.checkObjectCoercible(self);
628
677 * @return array object in which splits have been placed
678 */
679 @Function(attributes = Attribute.NOT_ENUMERABLE)
680 public static Object split(final Object self, final Object separator, final Object limit) {
681 Global.checkObjectCoercible(self);
682
683 final String str = JSType.toString(self);
684
685 if (separator == UNDEFINED) {
686 return new NativeArray(new Object[]{str});
687 }
688
689 final long lim = (limit == UNDEFINED) ? JSType.MAX_UINT : JSType.toUint32(limit);
690
691 if (separator instanceof NativeRegExp) {
692 return ((NativeRegExp) separator).split(str, lim);
693 }
694
695 // when separator is a string, it has to be treated as a
696 // literal search string to be used for splitting.
697 return NativeRegExp.flatRegExp(JSType.toString(separator)).split(str, lim);
698 }
699
700 /**
701 * ECMA B.2.3 String.prototype.substr (start, length)
702 *
703 * @param self self reference
704 * @param start start position
705 * @param length length of section
706 * @return substring given start and length of section
707 */
708 @Function(attributes = Attribute.NOT_ENUMERABLE)
709 public static Object substr(final Object self, final Object start, final Object length) {
710 final String str = JSType.toString(self);
711 final int strLength = str.length();
712
713 int intStart = JSType.toInteger(start);
714 if (intStart < 0) {
715 intStart = Math.max(intStart + strLength, 0);
716 }
717
|