String (String s) { if (s.length() >= 2 && ((s.charAt(0) == '\'' && s.charAt(s.length() - 1) == '\'') || (s.charAt(0) == '\"' && s.charAt(s.length() - 1) == '\"'))) { s = s.substring(1, s.length() - 1); } StringBuilder builder = new StringBuilder(); for (int i = 0; i < s.length(); i++) { char c1 = s.charAt(i); if (c1 == '\\') { assert i != s.length() - 1 : "Trailing backslash"; char c2 = s.charAt(++i); switch (c2) { case ' ', '\'', '\"', '\\' -> builder.append(c2); case 'n' -> builder.append('\n'); case 't' -> builder.append('\t'); case '\r' -> builder.append('\r'); // Only \\uXXXX escape is legal, so character always resides inside BMP case 'u' -> { assert i < s.length() - 4 : "Incomplete unicode escape sequence: " + s.substring(i - 1); builder.append((char)Integer.parseInt(s.substring(i + 1, i + 5), 16)); i += 4; } default -> throw new AssertionError("Illegal escape at " + s.substring(i)); } } else { builder.append(c1); } } return builder.toString(); }