boolean (@NotNull String chars, @NotNull StringBuilder outChars, int @Nullable [] sourceOffsets) { assert sourceOffsets == null || sourceOffsets.length == chars.length()+1; if (chars.indexOf('\\') < 0) { outChars.append(chars); if (sourceOffsets != null) { for (int i = 0; i < sourceOffsets.length; i++) { sourceOffsets[i] = i; } } return true; } int index = 0; final int outOffset = outChars.length(); while (index < chars.length()) { char c = chars.charAt(index++); if (sourceOffsets != null) { sourceOffsets[outChars.length()-outOffset] = index - 1; sourceOffsets[outChars.length() + 1 -outOffset] = index; } if (c != '\\') { outChars.append(c); continue; } if (index == chars.length()) return false; c = chars.charAt(index++); switch (c) { case 'b' -> outChars.append('\b'); case 't' -> outChars.append('\t'); case 'n' -> outChars.append('\n'); case 'f' -> outChars.append('\f'); case 'r' -> outChars.append('\r'); case '"' -> outChars.append('\"'); case '\'' -> outChars.append('\''); case '$' -> outChars.append('$'); case '\\' -> outChars.append('\\'); case '\n' -> {} //do nothing case '0', '1', '2', '3', '4', '5', '6', '7' -> { char startC = c; int v = (int)c - '0'; if (index < chars.length()) { c = chars.charAt(index++); if ('0' <= c && c <= '7') { v <<= 3; v += c - '0'; if (startC <= '3' && index < chars.length()) { c = chars.charAt(index++); if ('0' <= c && c <= '7') { v <<= 3; v += c - '0'; } else { index--; } } } else { index--; } } outChars.append((char)v); } case 'u' -> { // uuuuu1234 is valid too while (index != chars.length() && chars.charAt(index) == 'u') { index++; } if (index + 4 <= chars.length()) { try { int code = Integer.parseInt(chars.substring(index, index + 4), 16); //line separators are invalid here if (code == 0x000a || code == 0x000d) return false; c = chars.charAt(index); if (c == '+' || c == '-') return false; outChars.append((char)code); index += 4; } catch (Exception e) { return false; } } else { return false; } } default -> { return false; } } if (sourceOffsets != null) { sourceOffsets[outChars.length()-outOffset] = index; } } return true; }