int (String text, int offset) { final int textLength = text.length(); int escapeStart = -1; outer: while (true) { escapeStart = text.indexOf('\\', escapeStart + 1); if (escapeStart < 0) { break; } if (escapeStart < offset - 4 || escapeStart >= textLength - 1 || text.charAt(escapeStart + 1) == '\\') { continue; } boolean isEscape = true; int previousChar = escapeStart - 1; while (previousChar >= 0 && text.charAt(previousChar) == '\\') { isEscape = !isEscape; previousChar--; } if (!isEscape) { continue; } int length = 1; // see JLS 3.10.6. Escape Sequences for Character and String Literals boolean zeroToThree = false; while (escapeStart + length < textLength) { final char c = text.charAt(escapeStart + length); if (length == 1 && (c == '0' || c == '1' || c == '2' || c == '3')) { zeroToThree = true; } if (c < '0' || c > '7' || length > 3 || (length > 2 && !zeroToThree)) { if (offset <= escapeStart + length && length > 1) { return escapeStart; } continue outer; } length++; } return escapeStart; } return -1; }