From c664d50d5c0ec5c483bf89d4a8236dc919c9609f Mon Sep 17 00:00:00 2001 From: Jesse Beder Date: Tue, 22 May 2012 17:45:31 -0500 Subject: [PATCH] Fixed double quote escapes (e.g., \n is now that instead of \x0a) --- src/emitterutils.cpp | 29 +++++++++++++++++------------ test/emittertests.cpp | 4 ++-- util/sandbox.cpp | 5 +---- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/emitterutils.cpp b/src/emitterutils.cpp index f17e350..5dfaec9 100644 --- a/src/emitterutils.cpp +++ b/src/emitterutils.cpp @@ -275,18 +275,23 @@ namespace YAML GetNextCodePointAndAdvance(codePoint, i, str.end()); ) { - if (codePoint == '\"') - out << "\\\""; - else if (codePoint == '\\') - out << "\\\\"; - else if (codePoint < 0x20 || (codePoint >= 0x80 && codePoint <= 0xA0)) // Control characters and non-breaking space - WriteDoubleQuoteEscapeSequence(out, codePoint); - else if (codePoint == 0xFEFF) // Byte order marks (ZWNS) should be escaped (YAML 1.2, sec. 5.2) - WriteDoubleQuoteEscapeSequence(out, codePoint); - else if (escapeNonAscii && codePoint > 0x7E) - WriteDoubleQuoteEscapeSequence(out, codePoint); - else - WriteCodePoint(out, codePoint); + switch(codePoint) { + case '\"': out << "\\\""; break; + case '\\': out << "\\\\"; break; + case '\n': out << "\\n"; break; + case '\t': out << "\\t"; break; + case '\r': out << "\\r"; break; + case '\b': out << "\\b"; break; + default: + if(codePoint < 0x20 || (codePoint >= 0x80 && codePoint <= 0xA0)) // Control characters and non-breaking space + WriteDoubleQuoteEscapeSequence(out, codePoint); + else if (codePoint == 0xFEFF) // Byte order marks (ZWNS) should be escaped (YAML 1.2, sec. 5.2) + WriteDoubleQuoteEscapeSequence(out, codePoint); + else if (escapeNonAscii && codePoint > 0x7E) + WriteDoubleQuoteEscapeSequence(out, codePoint); + else + WriteCodePoint(out, codePoint); + } } out << "\""; return true; diff --git a/test/emittertests.cpp b/test/emittertests.cpp index 9920496..3c438f6 100644 --- a/test/emittertests.cpp +++ b/test/emittertests.cpp @@ -219,7 +219,7 @@ namespace Test out << YAML::Literal << "literal scalar\nthat may span\nmany, many\nlines and have \"whatever\" crazy\tsymbols that we like"; out << YAML::EndSeq; - desiredOutput = "- simple scalar\n- 'explicit single-quoted scalar'\n- \"explicit double-quoted scalar\"\n- \"auto-detected\\x0adouble-quoted scalar\"\n- a non-\"auto-detected\" double-quoted scalar\n- |\n literal scalar\n that may span\n many, many\n lines and have \"whatever\" crazy\tsymbols that we like"; + desiredOutput = "- simple scalar\n- 'explicit single-quoted scalar'\n- \"explicit double-quoted scalar\"\n- \"auto-detected\\ndouble-quoted scalar\"\n- a non-\"auto-detected\" double-quoted scalar\n- |\n literal scalar\n that may span\n many, many\n lines and have \"whatever\" crazy\tsymbols that we like"; } void AutoLongKeyScalar(YAML::Emitter& out, std::string& desiredOutput) @@ -942,7 +942,7 @@ namespace Test { out << YAML::SingleQuoted << "Hello\nWorld"; - desiredOutput = "Hello\\nWorld"; + desiredOutput = "\"Hello\\nWorld\""; } //////////////////////////////////////////////////////////////////////////////// diff --git a/util/sandbox.cpp b/util/sandbox.cpp index 83956d2..854e26d 100644 --- a/util/sandbox.cpp +++ b/util/sandbox.cpp @@ -4,10 +4,7 @@ int main() { YAML::Emitter out; - out << YAML::Flow; - out << YAML::BeginSeq; - out << "a" << "b"; - out << YAML::EndSeq; + out << YAML::DoubleQuoted << "Hello, World!\n"; std::cout << out.c_str() << "\n"; return 0;