< prev index next >

src/java.base/share/classes/java/lang/StringLatin1.java

Print this page
rev 51726 : 8202442: String::unescape
Reviewed-by: smarks, rriggs, sherman


 840 
 841         @Override
 842         public boolean tryAdvance(IntConsumer action) {
 843             if (action == null)
 844                 throw new NullPointerException();
 845             if (index >= 0 && index < fence) {
 846                 action.accept(array[index++] & 0xff);
 847                 return true;
 848             }
 849             return false;
 850         }
 851 
 852         @Override
 853         public long estimateSize() { return (long)(fence - index); }
 854 
 855         @Override
 856         public int characteristics() {
 857             return cs;
 858         }
 859     }















































































 860 }


 840 
 841         @Override
 842         public boolean tryAdvance(IntConsumer action) {
 843             if (action == null)
 844                 throw new NullPointerException();
 845             if (index >= 0 && index < fence) {
 846                 action.accept(array[index++] & 0xff);
 847                 return true;
 848             }
 849             return false;
 850         }
 851 
 852         @Override
 853         public long estimateSize() { return (long)(fence - index); }
 854 
 855         @Override
 856         public int characteristics() {
 857             return cs;
 858         }
 859     }
 860 
 861     static String unescape(byte[] value) throws IllegalArgumentException {
 862         int length = value.length;
 863         byte[] chars = new byte[length];
 864         int from = 0;
 865         int to = 0;
 866         while (from < length) {
 867             char ch = getChar(value, from++);
 868             if (ch == '\\' && from < length) {
 869                 ch = getChar(value, from++);
 870                 if (ch == 'u') {
 871                     while (from < length && getChar(value, from) == 'u') {
 872                         from++;
 873                     }
 874                     if (length <= from + 3) {
 875                         throw new IllegalArgumentException("unicode escape sequence truncated at end of string, pos = " + from);
 876                     }
 877                     int code = (Character.digit(getChar(value, from + 0), 16) << 12) |
 878                                (Character.digit(getChar(value, from + 1), 16) <<  8) |
 879                                (Character.digit(getChar(value, from + 2), 16) <<  4) |
 880                                 Character.digit(getChar(value, from + 3), 16);
 881                     if (code < 0) {
 882                         throw new IllegalArgumentException("unicode escape sequence contains non hexadecimal digits, pos = " + from);
 883                     }
 884                     if (canEncode(code)) {
 885                         ch = (char)code;
 886                         from += 4;
 887                     } else {
 888                         return StringUTF16.unescape(inflate(value, 0, length));
 889                     }
 890                 } else {
 891                     switch (ch) {
 892                         case 'b':
 893                             ch = '\b';
 894                             break;
 895                         case 'f':
 896                             ch = '\f';
 897                             break;
 898                         case 'n':
 899                             ch = '\n';
 900                             break;
 901                         case 'r':
 902                             ch = '\r';
 903                             break;
 904                         case 't':
 905                             ch = '\t';
 906                             break;
 907                         case 'u':
 908                             chars[to++] = (byte)'\\';
 909                             break;
 910                         case '\\':
 911                             ch = '\\';
 912                             break;
 913                         case '0': case '1': case '2': case '3':
 914                         case '4': case '5': case '6': case '7':
 915                             int code = ch - '0';
 916                             for (int i = 0; i < 2 && from < length; i++) {
 917                                 int digit = Character.digit(getChar(value, from), 8);
 918                                 if (digit < 0) {
 919                                     break;
 920                                 }
 921                                 from++;
 922                                 code = code << 3 | digit;
 923                             }
 924                             if (0377 < code) {
 925                                 throw new IllegalArgumentException("octal escape sequence value is too large, pos = " + from);
 926                             }
 927                             ch = (char)code;
 928                             break;
 929                         default:
 930                             throw new IllegalArgumentException("unrecognized escape sequence, pos = " + from);
 931                     }
 932                 }
 933             }
 934             chars[to++] = (byte)ch;
 935         }
 936         return new String(Arrays.copyOfRange(chars, 0, to), LATIN1);
 937     }
 938 
 939 }
< prev index next >