emul/src/main/java/java/lang/String.java
branchemul
changeset 61 4b334950499d
parent 54 e7160b348c34
child 64 55745bf6696c
     1.1 --- a/emul/src/main/java/java/lang/String.java	Sat Sep 29 06:41:48 2012 +0200
     1.2 +++ b/emul/src/main/java/java/lang/String.java	Sat Sep 29 08:13:32 2012 +0200
     1.3 @@ -27,12 +27,7 @@
     1.4  
     1.5  import java.io.ObjectStreamField;
     1.6  import java.io.UnsupportedEncodingException;
     1.7 -import java.nio.charset.Charset;
     1.8 -import java.util.ArrayList;
     1.9 -import java.util.Arrays;
    1.10  import java.util.Comparator;
    1.11 -import java.util.Formatter;
    1.12 -import java.util.Locale;
    1.13  import java.util.regex.Matcher;
    1.14  import java.util.regex.Pattern;
    1.15  import java.util.regex.PatternSyntaxException;
    1.16 @@ -169,7 +164,7 @@
    1.17              // String itself.  Perhaps this constructor is being called
    1.18              // in order to trim the baggage, so make a copy of the array.
    1.19              int off = original.offset;
    1.20 -            v = Arrays.copyOfRange(originalValue, off, off+size);
    1.21 +            v = copyOfRange(originalValue, off, off+size);
    1.22          } else {
    1.23              // The array representing the String is the same
    1.24              // size as the String, so no point in making a copy.
    1.25 @@ -193,7 +188,7 @@
    1.26          int size = value.length;
    1.27          this.offset = 0;
    1.28          this.count = size;
    1.29 -        this.value = Arrays.copyOf(value, size);
    1.30 +        this.value = copyOf(value, size);
    1.31      }
    1.32  
    1.33      /**
    1.34 @@ -230,7 +225,7 @@
    1.35          }
    1.36          this.offset = 0;
    1.37          this.count = count;
    1.38 -        this.value = Arrays.copyOfRange(value, offset, offset+count);
    1.39 +        this.value = copyOfRange(value, offset, offset+count);
    1.40      }
    1.41  
    1.42      /**
    1.43 @@ -484,6 +479,7 @@
    1.44       *
    1.45       * @since  1.6
    1.46       */
    1.47 +    /* don't want dependnecy on Charset
    1.48      public String(byte bytes[], int offset, int length, Charset charset) {
    1.49          if (charset == null)
    1.50              throw new NullPointerException("charset");
    1.51 @@ -493,6 +489,7 @@
    1.52          this.count = v.length;
    1.53          this.value = v;
    1.54      }
    1.55 +    */
    1.56  
    1.57      /**
    1.58       * Constructs a new {@code String} by decoding the specified array of bytes
    1.59 @@ -543,9 +540,11 @@
    1.60       *
    1.61       * @since  1.6
    1.62       */
    1.63 +    /* don't want dep on Charset
    1.64      public String(byte bytes[], Charset charset) {
    1.65          this(bytes, 0, bytes.length, charset);
    1.66      }
    1.67 +    */
    1.68  
    1.69      /**
    1.70       * Constructs a new {@code String} by decoding the specified subarray of
    1.71 @@ -972,10 +971,12 @@
    1.72       *
    1.73       * @since  1.6
    1.74       */
    1.75 +    /* don't want dep on Charset
    1.76      public byte[] getBytes(Charset charset) {
    1.77          if (charset == null) throw new NullPointerException();
    1.78          return StringCoding.encode(charset, value, offset, count);
    1.79      }
    1.80 +    */
    1.81  
    1.82      /**
    1.83       * Encodes this {@code String} into a sequence of bytes using the
    1.84 @@ -2325,7 +2326,7 @@
    1.85                ((ch-'A')|('Z'-ch)) < 0)) &&
    1.86              (ch < Character.MIN_HIGH_SURROGATE ||
    1.87               ch > Character.MAX_LOW_SURROGATE))
    1.88 -        {
    1.89 +        {/*
    1.90              int off = 0;
    1.91              int next = 0;
    1.92              boolean limited = limit > 0;
    1.93 @@ -2356,7 +2357,7 @@
    1.94                      resultSize--;
    1.95              String[] result = new String[resultSize];
    1.96              return list.subList(0, resultSize).toArray(result);
    1.97 -        }
    1.98 +        */}
    1.99          return Pattern.compile(regex).split(this, limit);
   1.100      }
   1.101  
   1.102 @@ -2454,99 +2455,99 @@
   1.103       * @see     java.lang.String#toUpperCase(Locale)
   1.104       * @since   1.1
   1.105       */
   1.106 -    public String toLowerCase(Locale locale) {
   1.107 -        if (locale == null) {
   1.108 -            throw new NullPointerException();
   1.109 -        }
   1.110 -
   1.111 -        int     firstUpper;
   1.112 -
   1.113 -        /* Now check if there are any characters that need to be changed. */
   1.114 -        scan: {
   1.115 -            for (firstUpper = 0 ; firstUpper < count; ) {
   1.116 -                char c = value[offset+firstUpper];
   1.117 -                if ((c >= Character.MIN_HIGH_SURROGATE) &&
   1.118 -                    (c <= Character.MAX_HIGH_SURROGATE)) {
   1.119 -                    int supplChar = codePointAt(firstUpper);
   1.120 -                    if (supplChar != Character.toLowerCase(supplChar)) {
   1.121 -                        break scan;
   1.122 -                    }
   1.123 -                    firstUpper += Character.charCount(supplChar);
   1.124 -                } else {
   1.125 -                    if (c != Character.toLowerCase(c)) {
   1.126 -                        break scan;
   1.127 -                    }
   1.128 -                    firstUpper++;
   1.129 -                }
   1.130 -            }
   1.131 -            return this;
   1.132 -        }
   1.133 -
   1.134 -        char[]  result = new char[count];
   1.135 -        int     resultOffset = 0;  /* result may grow, so i+resultOffset
   1.136 -                                    * is the write location in result */
   1.137 -
   1.138 -        /* Just copy the first few lowerCase characters. */
   1.139 -        System.arraycopy(value, offset, result, 0, firstUpper);
   1.140 -
   1.141 -        String lang = locale.getLanguage();
   1.142 -        boolean localeDependent =
   1.143 -            (lang == "tr" || lang == "az" || lang == "lt");
   1.144 -        char[] lowerCharArray;
   1.145 -        int lowerChar;
   1.146 -        int srcChar;
   1.147 -        int srcCount;
   1.148 -        for (int i = firstUpper; i < count; i += srcCount) {
   1.149 -            srcChar = (int)value[offset+i];
   1.150 -            if ((char)srcChar >= Character.MIN_HIGH_SURROGATE &&
   1.151 -                (char)srcChar <= Character.MAX_HIGH_SURROGATE) {
   1.152 -                srcChar = codePointAt(i);
   1.153 -                srcCount = Character.charCount(srcChar);
   1.154 -            } else {
   1.155 -                srcCount = 1;
   1.156 -            }
   1.157 -            if (localeDependent || srcChar == '\u03A3') { // GREEK CAPITAL LETTER SIGMA
   1.158 -                lowerChar = ConditionalSpecialCasing.toLowerCaseEx(this, i, locale);
   1.159 -            } else if (srcChar == '\u0130') { // LATIN CAPITAL LETTER I DOT
   1.160 -                lowerChar = Character.ERROR;
   1.161 -            } else {
   1.162 -                lowerChar = Character.toLowerCase(srcChar);
   1.163 -            }
   1.164 -            if ((lowerChar == Character.ERROR) ||
   1.165 -                (lowerChar >= Character.MIN_SUPPLEMENTARY_CODE_POINT)) {
   1.166 -                if (lowerChar == Character.ERROR) {
   1.167 -                     if (!localeDependent && srcChar == '\u0130') {
   1.168 -                         lowerCharArray =
   1.169 -                             ConditionalSpecialCasing.toLowerCaseCharArray(this, i, Locale.ENGLISH);
   1.170 -                     } else {
   1.171 -                        lowerCharArray =
   1.172 -                            ConditionalSpecialCasing.toLowerCaseCharArray(this, i, locale);
   1.173 -                     }
   1.174 -                } else if (srcCount == 2) {
   1.175 -                    resultOffset += Character.toChars(lowerChar, result, i + resultOffset) - srcCount;
   1.176 -                    continue;
   1.177 -                } else {
   1.178 -                    lowerCharArray = Character.toChars(lowerChar);
   1.179 -                }
   1.180 -
   1.181 -                /* Grow result if needed */
   1.182 -                int mapLen = lowerCharArray.length;
   1.183 -                if (mapLen > srcCount) {
   1.184 -                    char[] result2 = new char[result.length + mapLen - srcCount];
   1.185 -                    System.arraycopy(result, 0, result2, 0,
   1.186 -                        i + resultOffset);
   1.187 -                    result = result2;
   1.188 -                }
   1.189 -                for (int x=0; x<mapLen; ++x) {
   1.190 -                    result[i+resultOffset+x] = lowerCharArray[x];
   1.191 -                }
   1.192 -                resultOffset += (mapLen - srcCount);
   1.193 -            } else {
   1.194 -                result[i+resultOffset] = (char)lowerChar;
   1.195 -            }
   1.196 -        }
   1.197 -        return new String(0, count+resultOffset, result);
   1.198 -    }
   1.199 +//    public String toLowerCase(Locale locale) {
   1.200 +//        if (locale == null) {
   1.201 +//            throw new NullPointerException();
   1.202 +//        }
   1.203 +//
   1.204 +//        int     firstUpper;
   1.205 +//
   1.206 +//        /* Now check if there are any characters that need to be changed. */
   1.207 +//        scan: {
   1.208 +//            for (firstUpper = 0 ; firstUpper < count; ) {
   1.209 +//                char c = value[offset+firstUpper];
   1.210 +//                if ((c >= Character.MIN_HIGH_SURROGATE) &&
   1.211 +//                    (c <= Character.MAX_HIGH_SURROGATE)) {
   1.212 +//                    int supplChar = codePointAt(firstUpper);
   1.213 +//                    if (supplChar != Character.toLowerCase(supplChar)) {
   1.214 +//                        break scan;
   1.215 +//                    }
   1.216 +//                    firstUpper += Character.charCount(supplChar);
   1.217 +//                } else {
   1.218 +//                    if (c != Character.toLowerCase(c)) {
   1.219 +//                        break scan;
   1.220 +//                    }
   1.221 +//                    firstUpper++;
   1.222 +//                }
   1.223 +//            }
   1.224 +//            return this;
   1.225 +//        }
   1.226 +//
   1.227 +//        char[]  result = new char[count];
   1.228 +//        int     resultOffset = 0;  /* result may grow, so i+resultOffset
   1.229 +//                                    * is the write location in result */
   1.230 +//
   1.231 +//        /* Just copy the first few lowerCase characters. */
   1.232 +//        System.arraycopy(value, offset, result, 0, firstUpper);
   1.233 +//
   1.234 +//        String lang = locale.getLanguage();
   1.235 +//        boolean localeDependent =
   1.236 +//            (lang == "tr" || lang == "az" || lang == "lt");
   1.237 +//        char[] lowerCharArray;
   1.238 +//        int lowerChar;
   1.239 +//        int srcChar;
   1.240 +//        int srcCount;
   1.241 +//        for (int i = firstUpper; i < count; i += srcCount) {
   1.242 +//            srcChar = (int)value[offset+i];
   1.243 +//            if ((char)srcChar >= Character.MIN_HIGH_SURROGATE &&
   1.244 +//                (char)srcChar <= Character.MAX_HIGH_SURROGATE) {
   1.245 +//                srcChar = codePointAt(i);
   1.246 +//                srcCount = Character.charCount(srcChar);
   1.247 +//            } else {
   1.248 +//                srcCount = 1;
   1.249 +//            }
   1.250 +//            if (localeDependent || srcChar == '\u03A3') { // GREEK CAPITAL LETTER SIGMA
   1.251 +//                lowerChar = ConditionalSpecialCasing.toLowerCaseEx(this, i, locale);
   1.252 +//            } else if (srcChar == '\u0130') { // LATIN CAPITAL LETTER I DOT
   1.253 +//                lowerChar = Character.ERROR;
   1.254 +//            } else {
   1.255 +//                lowerChar = Character.toLowerCase(srcChar);
   1.256 +//            }
   1.257 +//            if ((lowerChar == Character.ERROR) ||
   1.258 +//                (lowerChar >= Character.MIN_SUPPLEMENTARY_CODE_POINT)) {
   1.259 +//                if (lowerChar == Character.ERROR) {
   1.260 +//                     if (!localeDependent && srcChar == '\u0130') {
   1.261 +//                         lowerCharArray =
   1.262 +//                             ConditionalSpecialCasing.toLowerCaseCharArray(this, i, Locale.ENGLISH);
   1.263 +//                     } else {
   1.264 +//                        lowerCharArray =
   1.265 +//                            ConditionalSpecialCasing.toLowerCaseCharArray(this, i, locale);
   1.266 +//                     }
   1.267 +//                } else if (srcCount == 2) {
   1.268 +//                    resultOffset += Character.toChars(lowerChar, result, i + resultOffset) - srcCount;
   1.269 +//                    continue;
   1.270 +//                } else {
   1.271 +//                    lowerCharArray = Character.toChars(lowerChar);
   1.272 +//                }
   1.273 +//
   1.274 +//                /* Grow result if needed */
   1.275 +//                int mapLen = lowerCharArray.length;
   1.276 +//                if (mapLen > srcCount) {
   1.277 +//                    char[] result2 = new char[result.length + mapLen - srcCount];
   1.278 +//                    System.arraycopy(result, 0, result2, 0,
   1.279 +//                        i + resultOffset);
   1.280 +//                    result = result2;
   1.281 +//                }
   1.282 +//                for (int x=0; x<mapLen; ++x) {
   1.283 +//                    result[i+resultOffset+x] = lowerCharArray[x];
   1.284 +//                }
   1.285 +//                resultOffset += (mapLen - srcCount);
   1.286 +//            } else {
   1.287 +//                result[i+resultOffset] = (char)lowerChar;
   1.288 +//            }
   1.289 +//        }
   1.290 +//        return new String(0, count+resultOffset, result);
   1.291 +//    }
   1.292  
   1.293      /**
   1.294       * Converts all of the characters in this <code>String</code> to lower
   1.295 @@ -2619,6 +2620,7 @@
   1.296       * @see     java.lang.String#toLowerCase(Locale)
   1.297       * @since   1.1
   1.298       */
   1.299 +    /* not for javascript 
   1.300      public String toUpperCase(Locale locale) {
   1.301          if (locale == null) {
   1.302              throw new NullPointerException();
   1.303 @@ -2626,7 +2628,7 @@
   1.304  
   1.305          int     firstLower;
   1.306  
   1.307 -        /* Now check if there are any characters that need to be changed. */
   1.308 +        // Now check if there are any characters that need to be changed. 
   1.309          scan: {
   1.310              for (firstLower = 0 ; firstLower < count; ) {
   1.311                  int c = (int)value[offset+firstLower];
   1.312 @@ -2648,11 +2650,11 @@
   1.313              return this;
   1.314          }
   1.315  
   1.316 -        char[]  result       = new char[count]; /* may grow */
   1.317 +        char[]  result       = new char[count]; /* may grow *
   1.318          int     resultOffset = 0;  /* result may grow, so i+resultOffset
   1.319 -                                    * is the write location in result */
   1.320 +                                    * is the write location in result *
   1.321  
   1.322 -        /* Just copy the first few upperCase characters. */
   1.323 +        /* Just copy the first few upperCase characters. *
   1.324          System.arraycopy(value, offset, result, 0, firstLower);
   1.325  
   1.326          String lang = locale.getLanguage();
   1.327 @@ -2692,7 +2694,7 @@
   1.328                      upperCharArray = Character.toChars(upperChar);
   1.329                  }
   1.330  
   1.331 -                /* Grow result if needed */
   1.332 +                /* Grow result if needed *
   1.333                  int mapLen = upperCharArray.length;
   1.334                  if (mapLen > srcCount) {
   1.335                      char[] result2 = new char[result.length + mapLen - srcCount];
   1.336 @@ -2710,6 +2712,7 @@
   1.337          }
   1.338          return new String(0, count+resultOffset, result);
   1.339      }
   1.340 +    */
   1.341  
   1.342      /**
   1.343       * Converts all of the characters in this <code>String</code> to upper
   1.344 @@ -2731,7 +2734,7 @@
   1.345       * @see     java.lang.String#toUpperCase(Locale)
   1.346       */
   1.347      public String toUpperCase() {
   1.348 -        return toUpperCase(Locale.getDefault());
   1.349 +        throw new UnsupportedOperationException();
   1.350      }
   1.351  
   1.352      /**
   1.353 @@ -2884,9 +2887,9 @@
   1.354       * @see  java.util.Formatter
   1.355       * @since  1.5
   1.356       */
   1.357 -    public static String format(Locale l, String format, Object ... args) {
   1.358 -        return new Formatter(l).format(format, args).toString();
   1.359 -    }
   1.360 +//    public static String format(Locale l, String format, Object ... args) {
   1.361 +//        return new Formatter(l).format(format, args).toString();
   1.362 +//    }
   1.363  
   1.364      /**
   1.365       * Returns the string representation of the <code>Object</code> argument.
   1.366 @@ -3073,4 +3076,21 @@
   1.367       */
   1.368      public native String intern();
   1.369  
   1.370 +    static char[] copyOfRange(char[] original, int from, int to) {
   1.371 +        int newLength = to - from;
   1.372 +        if (newLength < 0) {
   1.373 +            throw new IllegalArgumentException(from + " > " + to);
   1.374 +        }
   1.375 +        char[] copy = new char[newLength];
   1.376 +        System.arraycopy(original, from, copy, 0,
   1.377 +            Math.min(original.length - from, newLength));
   1.378 +        return copy;
   1.379 +    }
   1.380 +    static char[] copyOf(char[] original, int newLength) {
   1.381 +        char[] copy = new char[newLength];
   1.382 +        System.arraycopy(original, 0, copy, 0,
   1.383 +            Math.min(original.length, newLength));
   1.384 +        return copy;
   1.385 +    }
   1.386 +
   1.387  }