src/jdk/nashorn/internal/objects/NativeString.java

Print this page




 824      */
 825     @SpecializedFunction
 826     public static Object slice(final Object self, final double start, final double end) {
 827         return slice(self, (int)start, (int)end);
 828     }
 829 
 830     /**
 831      * ECMA 15.5.4.14 String.prototype.split (separator, limit)
 832      *
 833      * @param self      self reference
 834      * @param separator separator for split
 835      * @param limit     limit for splits
 836      * @return array object in which splits have been placed
 837      */
 838     @Function(attributes = Attribute.NOT_ENUMERABLE)
 839     public static Object split(final Object self, final Object separator, final Object limit) {
 840         final String str = checkObjectToString(self);
 841         final long lim = (limit == UNDEFINED) ? JSType.MAX_UINT : JSType.toUint32(limit);
 842 
 843         if (separator == UNDEFINED) {
 844             return new NativeArray(new Object[]{str});
 845         }
 846 
 847         if (separator instanceof NativeRegExp) {
 848             return ((NativeRegExp) separator).split(str, lim);
 849         }
 850 
 851         // when separator is a string, it is treated as a literal search string to be used for splitting.
 852         return splitString(str, JSType.toString(separator), lim);
 853     }
 854 
 855     private static Object splitString(String str, String separator, long limit) {
 856         if (separator.isEmpty()) {
 857             final Object[] array = new Object[str.length()];
 858             for (int i = 0; i < array.length; i++) {

 859                 array[i] = String.valueOf(str.charAt(i));
 860             }
 861             return new NativeArray(array);
 862         }
 863 
 864         final List<String> elements = new LinkedList<>();
 865         final int strLength = str.length();
 866         final int sepLength = separator.length();
 867         int pos = 0;
 868         int n = 0;
 869 
 870         while (pos < strLength && n < limit) {
 871             int found = str.indexOf(separator, pos);
 872             if (found == -1) {
 873                 break;
 874             }
 875             elements.add(str.substring(pos, found));
 876             n++;
 877             pos = found + sepLength;
 878         }




 824      */
 825     @SpecializedFunction
 826     public static Object slice(final Object self, final double start, final double end) {
 827         return slice(self, (int)start, (int)end);
 828     }
 829 
 830     /**
 831      * ECMA 15.5.4.14 String.prototype.split (separator, limit)
 832      *
 833      * @param self      self reference
 834      * @param separator separator for split
 835      * @param limit     limit for splits
 836      * @return array object in which splits have been placed
 837      */
 838     @Function(attributes = Attribute.NOT_ENUMERABLE)
 839     public static Object split(final Object self, final Object separator, final Object limit) {
 840         final String str = checkObjectToString(self);
 841         final long lim = (limit == UNDEFINED) ? JSType.MAX_UINT : JSType.toUint32(limit);
 842 
 843         if (separator == UNDEFINED) {
 844             return limit == 0 ? new NativeArray() : new NativeArray(new Object[]{str});
 845         }
 846 
 847         if (separator instanceof NativeRegExp) {
 848             return ((NativeRegExp) separator).split(str, lim);
 849         }
 850 
 851         // when separator is a string, it is treated as a literal search string to be used for splitting.
 852         return splitString(str, JSType.toString(separator), lim);
 853     }
 854 
 855     private static Object splitString(String str, String separator, long limit) {
 856         if (separator.isEmpty()) {
 857             final int length = (int) Math.min(str.length(), limit);
 858             final Object[] array = new Object[length];
 859             for (int i = 0; i < length; i++) {
 860                 array[i] = String.valueOf(str.charAt(i));
 861             }
 862             return new NativeArray(array);
 863         }
 864 
 865         final List<String> elements = new LinkedList<>();
 866         final int strLength = str.length();
 867         final int sepLength = separator.length();
 868         int pos = 0;
 869         int n = 0;
 870 
 871         while (pos < strLength && n < limit) {
 872             int found = str.indexOf(separator, pos);
 873             if (found == -1) {
 874                 break;
 875             }
 876             elements.add(str.substring(pos, found));
 877             n++;
 878             pos = found + sepLength;
 879         }