in sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/firestore/QueryUtils.java [249:353]
private static String unescapeFieldName(String fieldName) {
if (fieldName.isEmpty()) {
throw new IllegalArgumentException("quoted identifier cannot be empty");
}
StringBuilder buf = new StringBuilder();
for (int i = 0; i < fieldName.length(); i++) {
char c = fieldName.charAt(i);
// Roughly speaking, there are 4 cases we care about:
// - carriage returns: \r and \r\n
// - unescaped quotes: `
// - non-escape sequences
// - escape sequences
if (c == '`') {
throw new IllegalArgumentException("quoted identifier cannot contain unescaped quote");
} else if (c == '\r') {
buf.append('\n');
// Convert '\r\n' into '\n'
if (i + 1 < fieldName.length() && fieldName.charAt(i + 1) == '\n') {
i++;
}
} else if (c != '\\') {
buf.append(c);
} else if (i + 1 >= fieldName.length()) {
throw new IllegalArgumentException("illegal trailing backslash");
} else {
i++;
switch (fieldName.charAt(i)) {
case 'a':
buf.appendCodePoint(Ascii.BEL); // "Alert" control character
break;
case 'b':
buf.append('\b');
break;
case 'f':
buf.append('\f');
break;
case 'n':
buf.append('\n');
break;
case 'r':
buf.append('\r');
break;
case 't':
buf.append('\t');
break;
case 'v':
buf.appendCodePoint(Ascii.VT); // vertical tab
break;
case '?':
buf.append('?'); // artifact of ancient C grammar
break;
case '\\':
buf.append('\\');
break;
case '\'':
buf.append('\'');
break;
case '"':
buf.append('\"');
break;
case '`':
buf.append('`');
break;
case '0':
case '1':
case '2':
case '3':
if (i + 3 > fieldName.length()) {
throw new IllegalArgumentException("illegal octal escape sequence");
}
buf.appendCodePoint(unescapeOctal(fieldName.substring(i, i + 3)));
i += 3;
break;
case 'x':
case 'X':
i++;
if (i + 2 > fieldName.length()) {
throw new IllegalArgumentException("illegal hex escape sequence");
}
buf.appendCodePoint(unescapeHex(fieldName.substring(i, i + 2)));
i += 2;
break;
case 'u':
i++;
if (i + 4 > fieldName.length()) {
throw new IllegalArgumentException("illegal unicode escape sequence");
}
buf.appendCodePoint(unescapeHex(fieldName.substring(i, i + 4)));
i += 4;
break;
case 'U':
i++;
if (i + 8 > fieldName.length()) {
throw new IllegalArgumentException("illegal unicode escape sequence");
}
buf.appendCodePoint(unescapeHex(fieldName.substring(i, i + 8)));
i += 8;
break;
default:
throw new IllegalArgumentException("illegal escape");
}
}
}
return buf.toString();
}