Fixed bugs in escape characters (both parsing and emitting)

This commit is contained in:
Jesse Beder
2009-09-07 16:31:23 +00:00
parent 45ac700fff
commit e3ff87ecde
3 changed files with 46 additions and 4 deletions

View File

@@ -83,7 +83,7 @@ namespace YAML
} else { } else {
// TODO: for the common escaped characters, give their usual symbol // TODO: for the common escaped characters, give their usual symbol
std::stringstream str; std::stringstream str;
str << "\\x" << std::hex << std::setfill('0') << std::setw(2) << static_cast <int>(ch); str << "\\x" << std::hex << std::setfill('0') << std::setw(2) << static_cast<unsigned int>(static_cast<unsigned char>(ch));
out << str.str(); out << str.str();
} }
} }

View File

@@ -83,7 +83,7 @@ namespace YAML
// now do the slash (we're not gonna check if it's a slash - you better pass one!) // now do the slash (we're not gonna check if it's a slash - you better pass one!)
switch(ch) { switch(ch) {
case '0': return "\0"; case '0': return std::string("\x00", 1);
case 'a': return "\x07"; case 'a': return "\x07";
case 'b': return "\x08"; case 'b': return "\x08";
case 't': case 't':
@@ -97,8 +97,9 @@ namespace YAML
case '\"': return "\""; case '\"': return "\"";
case '\'': return "\'"; case '\'': return "\'";
case '\\': return "\\"; case '\\': return "\\";
case 'N': return "\xC2\x85"; // NEL (#x85) case '/': return "/";
case '_': return "\xC2\xA0"; // #xA0 case 'N': return "\x85";
case '_': return "\xA0";
case 'L': return "\xE2\x80\xA8"; // LS (#x2028) case 'L': return "\xE2\x80\xA8"; // LS (#x2028)
case 'P': return "\xE2\x80\xA9"; // PS (#x2029) case 'P': return "\xE2\x80\xA9"; // PS (#x2029)
case 'x': return Escape(in, 2); case 'x': return Escape(in, 2);

View File

@@ -793,6 +793,45 @@ namespace Test {
"}"); "}");
return true; return true;
} }
// 5.13
TEST EscapedCharacters()
{
std::string input =
"\"Fun with \\\\\n"
"\\\" \\a \\b \\e \\f \\\n"
"\\n \\r \\t \\v \\0 \\\n"
"\\ \\_ \\N \\L \\P \\\n"
"\\x41 \\u0041 \\U00000041\"";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
YAML_ASSERT(doc == "Fun with \x5C \x22 \x07 \x08 \x1B \x0C \x0A \x0D \x09 \x0B " + std::string("\x00", 1) + " \x20 \xA0 \x85 \u2028 \u2029 A A A");
return true;
}
// 5.14
TEST InvalidEscapedCharacters()
{
std::string input =
"Bad escapes:\n"
" \"\\c\n"
" \\xq-\"";
std::stringstream stream(input);
try {
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
} catch(const YAML::ParserException& e) {
YAML_ASSERT(e.msg == YAML::ErrorMsg::INVALID_ESCAPE + "c");
return true;
}
return false;
}
} }
bool RunSpecTests() bool RunSpecTests()
@@ -827,6 +866,8 @@ namespace Test {
RunSpecTest(&Spec::QuotedScalarIndicators, "5.8", "Quoted Scalar Indicators", passed); RunSpecTest(&Spec::QuotedScalarIndicators, "5.8", "Quoted Scalar Indicators", passed);
RunSpecTest(&Spec::LineBreakCharacters, "5.11", "Line Break Characters", passed); RunSpecTest(&Spec::LineBreakCharacters, "5.11", "Line Break Characters", passed);
RunSpecTest(&Spec::TabsAndSpaces, "5.12", "Tabs and Spaces", passed); RunSpecTest(&Spec::TabsAndSpaces, "5.12", "Tabs and Spaces", passed);
RunSpecTest(&Spec::EscapedCharacters, "5.13", "Escaped Characters", passed);
RunSpecTest(&Spec::InvalidEscapedCharacters, "5.14", "Invalid Escaped Characters", passed);
return passed; return passed;
} }