diff --git a/src/emitterutils.cpp b/src/emitterutils.cpp index 694b961..cddf5eb 100644 --- a/src/emitterutils.cpp +++ b/src/emitterutils.cpp @@ -83,7 +83,7 @@ namespace YAML } else { // TODO: for the common escaped characters, give their usual symbol std::stringstream str; - str << "\\x" << std::hex << std::setfill('0') << std::setw(2) << static_cast (ch); + str << "\\x" << std::hex << std::setfill('0') << std::setw(2) << static_cast(static_cast(ch)); out << str.str(); } } diff --git a/src/exp.cpp b/src/exp.cpp index bbb294f..b5cb815 100644 --- a/src/exp.cpp +++ b/src/exp.cpp @@ -83,7 +83,7 @@ namespace YAML // now do the slash (we're not gonna check if it's a slash - you better pass one!) switch(ch) { - case '0': return "\0"; + case '0': return std::string("\x00", 1); case 'a': return "\x07"; case 'b': return "\x08"; case 't': @@ -97,8 +97,9 @@ namespace YAML case '\"': return "\""; case '\'': return "\'"; case '\\': return "\\"; - case 'N': return "\xC2\x85"; // NEL (#x85) - case '_': return "\xC2\xA0"; // #xA0 + case '/': return "/"; + case 'N': return "\x85"; + case '_': return "\xA0"; case 'L': return "\xE2\x80\xA8"; // LS (#x2028) case 'P': return "\xE2\x80\xA9"; // PS (#x2029) case 'x': return Escape(in, 2); diff --git a/yaml-reader/spectests.cpp b/yaml-reader/spectests.cpp index 16f395c..af82b57 100644 --- a/yaml-reader/spectests.cpp +++ b/yaml-reader/spectests.cpp @@ -793,6 +793,45 @@ namespace Test { "}"); 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() @@ -827,6 +866,8 @@ namespace Test { RunSpecTest(&Spec::QuotedScalarIndicators, "5.8", "Quoted Scalar Indicators", passed); RunSpecTest(&Spec::LineBreakCharacters, "5.11", "Line Break Characters", 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; }