Fixed double quote escapes (e.g., \n is now that instead of \x0a)

This commit is contained in:
Jesse Beder
2012-05-22 17:45:31 -05:00
parent ad275901b2
commit c664d50d5c
3 changed files with 20 additions and 18 deletions

View File

@@ -275,18 +275,23 @@ namespace YAML
GetNextCodePointAndAdvance(codePoint, i, str.end()); GetNextCodePointAndAdvance(codePoint, i, str.end());
) )
{ {
if (codePoint == '\"') switch(codePoint) {
out << "\\\""; case '\"': out << "\\\""; break;
else if (codePoint == '\\') case '\\': out << "\\\\"; break;
out << "\\\\"; case '\n': out << "\\n"; break;
else if (codePoint < 0x20 || (codePoint >= 0x80 && codePoint <= 0xA0)) // Control characters and non-breaking space case '\t': out << "\\t"; break;
WriteDoubleQuoteEscapeSequence(out, codePoint); case '\r': out << "\\r"; break;
else if (codePoint == 0xFEFF) // Byte order marks (ZWNS) should be escaped (YAML 1.2, sec. 5.2) case '\b': out << "\\b"; break;
WriteDoubleQuoteEscapeSequence(out, codePoint); default:
else if (escapeNonAscii && codePoint > 0x7E) if(codePoint < 0x20 || (codePoint >= 0x80 && codePoint <= 0xA0)) // Control characters and non-breaking space
WriteDoubleQuoteEscapeSequence(out, codePoint); WriteDoubleQuoteEscapeSequence(out, codePoint);
else else if (codePoint == 0xFEFF) // Byte order marks (ZWNS) should be escaped (YAML 1.2, sec. 5.2)
WriteCodePoint(out, codePoint); WriteDoubleQuoteEscapeSequence(out, codePoint);
else if (escapeNonAscii && codePoint > 0x7E)
WriteDoubleQuoteEscapeSequence(out, codePoint);
else
WriteCodePoint(out, codePoint);
}
} }
out << "\""; out << "\"";
return true; return true;

View File

@@ -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::Literal << "literal scalar\nthat may span\nmany, many\nlines and have \"whatever\" crazy\tsymbols that we like";
out << YAML::EndSeq; 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) void AutoLongKeyScalar(YAML::Emitter& out, std::string& desiredOutput)
@@ -942,7 +942,7 @@ namespace Test
{ {
out << YAML::SingleQuoted << "Hello\nWorld"; out << YAML::SingleQuoted << "Hello\nWorld";
desiredOutput = "Hello\\nWorld"; desiredOutput = "\"Hello\\nWorld\"";
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////

View File

@@ -4,10 +4,7 @@
int main() int main()
{ {
YAML::Emitter out; YAML::Emitter out;
out << YAML::Flow; out << YAML::DoubleQuoted << "Hello, World!\n";
out << YAML::BeginSeq;
out << "a" << "b";
out << YAML::EndSeq;
std::cout << out.c_str() << "\n"; std::cout << out.c_str() << "\n";
return 0; return 0;