TextRange (String s) { if (s == null) { return null; } int off = 0; int len = s.length(); int startSpaces = -1; while (off < len) { char aChar = s.charAt(off++); if (aChar == '\\') { if (startSpaces == -1) startSpaces = off-1; aChar = s.charAt(off++); if (aChar == 'u') { // Read the xxxx int value = 0; boolean error = false; for (int i = 0; i < 4; i++) { aChar = off < s.length() ? s.charAt(off++) : 0; switch (aChar) { case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' -> value = (value << 4) + aChar - '0'; case 'a', 'b', 'c', 'd', 'e', 'f' -> value = (value << 4) + 10 + aChar - 'a'; case 'A', 'B', 'C', 'D', 'E', 'F' -> value = (value << 4) + 10 + aChar - 'A'; default -> { int start = off - i - 1; int end = Math.min(start + 4, s.length()); i = 4; error = true; off = end; startSpaces = -1; } } } if (!error) { if (Character.isWhitespace(value)) { if (startSpaces == -1) { startSpaces = off-1; } } else { startSpaces = -1; } } } else if (aChar == '\n') { // escaped linebreak: skip whitespace in the beginning of next line while (off < len && (s.charAt(off) == ' ' || s.charAt(off) == '\t')) { off++; } } else if (aChar == 't' || aChar == 'r') { if (startSpaces == -1) startSpaces = off; } else { if (aChar == 'n' || aChar == 'f') { if (startSpaces == -1) startSpaces = off; } else { if (Character.isWhitespace(aChar)) { if (startSpaces == -1) { startSpaces = off-1; } } else { startSpaces = -1; } } } } else { if (Character.isWhitespace(aChar)) { if (startSpaces == -1) { startSpaces = off-1; } } else { startSpaces = -1; } } } return startSpaces == -1 ? null : new TextRange(startSpaces, len); }