support capital X for hex escapes in string literals

pull/10757/head
Josh Humphries 2 years ago
parent 2c71df4547
commit ff976a37d7
  1. 4
      src/google/protobuf/io/tokenizer.cc
  2. 4
      src/google/protobuf/io/tokenizer_unittest.cc

@ -422,7 +422,7 @@ void Tokenizer::ConsumeString(char delimiter) {
// Possibly followed by two more octal digits, but these will // Possibly followed by two more octal digits, but these will
// just be consumed by the main loop anyway so we don't need // just be consumed by the main loop anyway so we don't need
// to do so explicitly here. // to do so explicitly here.
} else if (TryConsume('x')) { } else if (TryConsume('x') || TryConsume('X')) {
if (!TryConsumeOne<HexDigit>()) { if (!TryConsumeOne<HexDigit>()) {
AddError("Expected hex digits for escape sequence."); AddError("Expected hex digits for escape sequence.");
} }
@ -1216,7 +1216,7 @@ void Tokenizer::ParseStringAppend(const std::string& text,
} }
output->push_back(static_cast<char>(code)); output->push_back(static_cast<char>(code));
} else if (*ptr == 'x') { } else if (*ptr == 'x' || *ptr == 'X') {
// A hex escape. May zero, one, or two digits. (The zero case // A hex escape. May zero, one, or two digits. (The zero case
// will have been caught as an error earlier.) // will have been caught as an error earlier.)
int code = 0; int code = 0;

@ -1045,6 +1045,8 @@ TEST_F(TokenizerTest, ParseString) {
EXPECT_EQ("\1x\1\123\739\52\334n\3", output); EXPECT_EQ("\1x\1\123\739\52\334n\3", output);
Tokenizer::ParseString("'\\x20\\x4'", &output); Tokenizer::ParseString("'\\x20\\x4'", &output);
EXPECT_EQ("\x20\x4", output); EXPECT_EQ("\x20\x4", output);
Tokenizer::ParseString("'\\X20\\X4'", &output);
EXPECT_EQ("\x20\x4", output);
// Test invalid strings that may still be tokenized as strings. // Test invalid strings that may still be tokenized as strings.
Tokenizer::ParseString("\"\\a\\l\\v\\t", &output); // \l is invalid Tokenizer::ParseString("\"\\a\\l\\v\\t", &output); // \l is invalid
@ -1110,7 +1112,7 @@ inline std::ostream& operator<<(std::ostream& out, const ErrorCase& test_case) {
ErrorCase kErrorCases[] = { ErrorCase kErrorCases[] = {
// String errors. // String errors.
{"'\\l' foo", true, "0:2: Invalid escape sequence in string literal.\n"}, {"'\\l' foo", true, "0:2: Invalid escape sequence in string literal.\n"},
{"'\\X' foo", true, "0:2: Invalid escape sequence in string literal.\n"}, {"'\\X' foo", true, "0:3: Expected hex digits for escape sequence.\n"},
{"'\\x' foo", true, "0:3: Expected hex digits for escape sequence.\n"}, {"'\\x' foo", true, "0:3: Expected hex digits for escape sequence.\n"},
{"'foo", false, "0:4: Unexpected end of string.\n"}, {"'foo", false, "0:4: Unexpected end of string.\n"},
{"'bar\nfoo", true, {"'bar\nfoo", true,

Loading…
Cancel
Save