public static UnescapedCharSequence discardEscapeChar(CharSequence input) throws ParseException {
// Create char array to hold unescaped char sequence
char[] output = new char[input.length()];
boolean[] wasEscaped = new boolean[input.length()];
// The length of the output can be less than the input
// due to discarded escape chars. This variable holds
// the actual length of the output
int length = 0;
// We remember whether the last processed character was
// an escape character
boolean lastCharWasEscapeChar = false;
// The multiplier the current unicode digit must be multiplied with.
// E. g. the first digit must be multiplied with 16^3, the second with
// 16^2...
int codePointMultiplier = 0;
// Used to calculate the codepoint of the escaped unicode character
int codePoint = 0;
for (int i = 0; i < input.length(); i++) {
char curChar = input.charAt(i);
if (codePointMultiplier > 0) {
codePoint += hexToInt(curChar) * codePointMultiplier;
codePointMultiplier > > >= 4;
if (codePointMultiplier == 0) {
output[length++] = (char) codePoint;
codePoint = 0;
}
} else if (lastCharWasEscapeChar) {
if (curChar == 'u') {
// found an escaped unicode character
codePointMultiplier = 16 * 16 * 16;
} else {
// this character was escaped
output[length] = curChar;
wasEscaped[length] = true;
length++;
}
lastCharWasEscapeChar = false;
} else {
if (curChar == '\\') {
lastCharWasEscapeChar = true;
} else {
output[length] = curChar;
length++;
}
}
}
if (codePointMultiplier > 0) {
throw new ParseException(new MessageImpl(
QueryParserMessages.INVALID_SYNTAX_ESCAPE_UNICODE_TRUNCATION));
}
if (lastCharWasEscapeChar) {
throw new ParseException(new MessageImpl(
QueryParserMessages.INVALID_SYNTAX_ESCAPE_CHARACTER));
}
return new UnescapedCharSequence(output, wasEscaped, 0, length);
}
Returns a String where the escape char has been removed, or kept only once
if there was a double escape.
Supports escaped unicode characters, e. g. translates A to
A . |