boolean (@NotNull String chars, @NotNull StringBuilder outChars, int @Nullable [] sourceOffsets, boolean escapeSlash) { 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()) { outChars.append('\\'); return true; } c = chars.charAt(index++); switch (c) { case '/' -> { if (escapeSlash) { outChars.append(c); if (sourceOffsets != null) { sourceOffsets[outChars.length() - outOffset] = index; } } else { outChars.append('\\').append('/'); } } case '\n' -> { //do nothing if (sourceOffsets != null) { sourceOffsets[outChars.length() - outOffset] = index; } } 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); c = chars.charAt(index); if (c == '+' || c == '-') return false; outChars.append((char)code); index += 4; if (sourceOffsets != null) { sourceOffsets[outChars.length() - outOffset] = index; } } catch (Exception e) { return false; } } else { return false; } } default -> { outChars.append('\\').append(c); if (sourceOffsets != null) { sourceOffsets[outChars.length() - outOffset] = index; } } } } return true; }