< prev index next >

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

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


1531         return charPos;
1532     }
1533     // End of trusted methods.
1534 
1535     public static void checkIndex(int off, byte[] val) {
1536         String.checkIndex(off, length(val));
1537     }
1538 
1539     public static void checkOffset(int off, byte[] val) {
1540         String.checkOffset(off, length(val));
1541     }
1542 
1543     public static void checkBoundsBeginEnd(int begin, int end, byte[] val) {
1544         String.checkBoundsBeginEnd(begin, end, length(val));
1545     }
1546 
1547     public static void checkBoundsOffCount(int offset, int count, byte[] val) {
1548         String.checkBoundsOffCount(offset, count, length(val));
1549     }
1550 









































































1551 }


1531         return charPos;
1532     }
1533     // End of trusted methods.
1534 
1535     public static void checkIndex(int off, byte[] val) {
1536         String.checkIndex(off, length(val));
1537     }
1538 
1539     public static void checkOffset(int off, byte[] val) {
1540         String.checkOffset(off, length(val));
1541     }
1542 
1543     public static void checkBoundsBeginEnd(int begin, int end, byte[] val) {
1544         String.checkBoundsBeginEnd(begin, end, length(val));
1545     }
1546 
1547     public static void checkBoundsOffCount(int offset, int count, byte[] val) {
1548         String.checkBoundsOffCount(offset, count, length(val));
1549     }
1550 
1551     static String unescape(byte[] value) throws IllegalArgumentException {
1552         int length = value.length >>> 1;
1553         char[] chars = new char[length];
1554         int from = 0;
1555         int to = 0;
1556         while (from < length) {
1557             char ch = getChar(value, from++);
1558             if (ch == '\\' && from < length) {
1559                 ch = getChar(value, from++);
1560                 if (ch == 'u') {
1561                     while (from < length && getChar(value, from) == 'u') {
1562                         from++;
1563                     }
1564                     if (length <= from + 3) {
1565                         throw new IllegalArgumentException("unicode escape sequence truncated at end of string, pos = " + from);
1566                     }
1567                     int code = (Character.digit(getChar(value, from + 0), 16) << 12) |
1568                                (Character.digit(getChar(value, from + 1), 16) <<  8) |
1569                                (Character.digit(getChar(value, from + 2), 16) <<  4) |
1570                                 Character.digit(getChar(value, from + 3), 16);
1571                     if (code < 0) {
1572                         throw new IllegalArgumentException("unicode escape sequence contains non hexadecimal digits, pos = " + from);
1573                     }
1574                     ch = (char)code;
1575                     from += 4;
1576                 } else {
1577                     switch (ch) {
1578                         case 'b':
1579                             ch = '\b';
1580                             break;
1581                         case 'f':
1582                             ch = '\f';
1583                             break;
1584                         case 'n':
1585                             ch = '\n';
1586                             break;
1587                         case 'r':
1588                             ch = '\r';
1589                             break;
1590                         case 't':
1591                             ch = '\t';
1592                             break;
1593                         case 'u':
1594                             chars[to++] = '\\';
1595                             break;
1596                         case '\\':
1597                             ch = '\\';
1598                             break;
1599                         case '0': case '1': case '2': case '3':
1600                         case '4': case '5': case '6': case '7':
1601                             int code = ch - '0';
1602                             for (int i = 0; i < 2 && from < length; i++) {
1603                                 int digit = Character.digit(getChar(value, from), 8);
1604                                 if (digit < 0) {
1605                                     break;
1606                                 }
1607                                 from++;
1608                                 code = code << 3 | digit;
1609                             }
1610                             if (0377 < code) {
1611                                 throw new IllegalArgumentException("octal escape sequence value is too large, pos = " + from);
1612                             }
1613                             ch = (char)code;
1614                             break;
1615                         default:
1616                             throw new IllegalArgumentException("unrecognized escape sequence, pos = " + from);
1617                     }
1618                 }
1619             }
1620             chars[to++] = ch;
1621         }
1622         return new String(chars, 0, to);
1623     }
1624 }
< prev index next >