in smithy-model/src/main/java/software/amazon/smithy/model/loader/IdlTextParser.java [65:153]
private static String parseStringContents(IdlModelParser parser, String lexeme, boolean triple) {
lexeme = normalizeLineEndings(lexeme);
// Format the text block and remove incidental whitespace.
if (triple) {
lexeme = formatTextBlock(parser, lexeme);
}
StringBuilder result = new StringBuilder(lexeme.length());
State state = State.NORMAL;
int hexCount = 0;
int unicode = 0;
// Skip quotes from the start and end.
for (int i = 0; i < lexeme.length(); i++) {
char c = lexeme.charAt(i);
switch (state) {
case NORMAL:
if (c == '\\') {
state = State.AFTER_ESCAPE;
} else {
result.append(c);
}
break;
case AFTER_ESCAPE:
state = State.NORMAL;
switch (c) {
case '"':
result.append('"');
continue;
case '\\':
result.append('\\');
break;
case '/':
result.append('/');
break;
case 'b':
result.append('\b');
break;
case 'f':
result.append('\f');
break;
case 'n':
result.append('\n');
break;
case 'r':
result.append('\r');
break;
case 't':
result.append('\t');
break;
case 'u':
state = State.UNICODE;
break;
case '\n':
// Skip writing the escaped new line.
break;
default:
throw parser.syntax("Invalid escape found in string: `\\" + c + "`");
}
break;
case UNICODE:
if (c >= '0' && c <= '9') {
unicode = (unicode << 4) | (c - '0');
} else if (c >= 'a' && c <= 'f') {
unicode = (unicode << 4) | (10 + c - 'a');
} else if (c >= 'A' && c <= 'F') {
unicode = (unicode << 4) | (10 + c - 'A');
} else {
throw parser.syntax("Invalid unicode escape character: `" + c + "`");
}
if (++hexCount == 4) {
result.append((char) unicode);
hexCount = 0;
state = State.NORMAL;
}
break;
default:
throw new IllegalStateException("Unreachable");
}
}
if (state == State.UNICODE) {
throw parser.syntax("Invalid unclosed unicode escape found in string");
}
return result.toString();
}