mirror of
https://github.com/jbeder/yaml-cpp.git
synced 2025-09-09 04:41:16 +00:00
Added single character emitting
This commit is contained in:
@@ -51,6 +51,7 @@ namespace YAML
|
||||
// overloads of write
|
||||
Emitter& Write(const std::string& str);
|
||||
Emitter& Write(bool b);
|
||||
Emitter& Write(char ch);
|
||||
Emitter& Write(const _Alias& alias);
|
||||
Emitter& Write(const _Anchor& anchor);
|
||||
Emitter& Write(const _Tag& tag);
|
||||
@@ -125,6 +126,7 @@ namespace YAML
|
||||
// overloads of insertion
|
||||
inline Emitter& operator << (Emitter& emitter, const std::string& v) { return emitter.Write(v); }
|
||||
inline Emitter& operator << (Emitter& emitter, bool v) { return emitter.Write(v); }
|
||||
inline Emitter& operator << (Emitter& emitter, char v) { return emitter.Write(v); }
|
||||
inline Emitter& operator << (Emitter& emitter, const _Alias& v) { return emitter.Write(v); }
|
||||
inline Emitter& operator << (Emitter& emitter, const _Anchor& v) { return emitter.Write(v); }
|
||||
inline Emitter& operator << (Emitter& emitter, const _Tag& v) { return emitter.Write(v); }
|
||||
|
@@ -733,6 +733,20 @@ namespace YAML
|
||||
return *this;
|
||||
}
|
||||
|
||||
Emitter& Emitter::Write(char ch)
|
||||
{
|
||||
if(!good())
|
||||
return *this;
|
||||
|
||||
PreAtomicWrite();
|
||||
EmitSeparationIfNecessary();
|
||||
|
||||
Utils::WriteChar(m_stream, ch);
|
||||
|
||||
PostAtomicWrite();
|
||||
return *this;
|
||||
}
|
||||
|
||||
Emitter& Emitter::Write(const _Alias& alias)
|
||||
{
|
||||
if(!good())
|
||||
|
@@ -268,6 +268,26 @@ namespace YAML
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WriteChar(ostream& out, char ch)
|
||||
{
|
||||
if(('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z'))
|
||||
out << ch;
|
||||
else if((0x20 <= ch && ch <= 0x7e) || ch == ' ')
|
||||
out << "\"" << ch << "\"";
|
||||
else if(ch == '\t')
|
||||
out << "\"\\t\"";
|
||||
else if(ch == '\n')
|
||||
out << "\"\\n\"";
|
||||
else if(ch == '\b')
|
||||
out << "\"\\b\"";
|
||||
else {
|
||||
out << "\"";
|
||||
WriteDoubleQuoteEscapeSequence(out, ch);
|
||||
out << "\"";
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WriteComment(ostream& out, const std::string& str, int postCommentIndent)
|
||||
{
|
||||
const unsigned curIndent = out.col();
|
||||
|
@@ -17,6 +17,7 @@ namespace YAML
|
||||
bool WriteSingleQuotedString(ostream& out, const std::string& str);
|
||||
bool WriteDoubleQuotedString(ostream& out, const std::string& str, bool escapeNonAscii);
|
||||
bool WriteLiteralString(ostream& out, const std::string& str, int indent);
|
||||
bool WriteChar(ostream& out, char ch);
|
||||
bool WriteComment(ostream& out, const std::string& str, int postCommentIndent);
|
||||
bool WriteAlias(ostream& out, const std::string& str);
|
||||
bool WriteAnchor(ostream& out, const std::string& str);
|
||||
|
@@ -861,8 +861,21 @@ namespace Test
|
||||
out << YAML::EndMap;
|
||||
desiredOutput = "key: \"\"";
|
||||
}
|
||||
|
||||
void SingleChar(YAML::Emitter& out, std::string& desiredOutput)
|
||||
{
|
||||
out << YAML::BeginSeq;
|
||||
out << 'a';
|
||||
out << ':';
|
||||
out << (char)0x10;
|
||||
out << '\n';
|
||||
out << ' ';
|
||||
out << '\t';
|
||||
out << YAML::EndSeq;
|
||||
desiredOutput = "- a\n- \":\"\n- \"\\x10\"\n- \"\\n\"\n- \" \"\n- \"\\t\"";
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// incorrect emitting
|
||||
|
||||
void ExtraEndSeq(YAML::Emitter& out, std::string& desiredError)
|
||||
@@ -1080,6 +1093,7 @@ namespace Test
|
||||
RunEmitterTest(&Emitter::DocStartAndEnd, "doc start and end", passed, total);
|
||||
RunEmitterTest(&Emitter::ImplicitDocStart, "implicit doc start", passed, total);
|
||||
RunEmitterTest(&Emitter::EmptyString, "empty string", passed, total);
|
||||
RunEmitterTest(&Emitter::SingleChar, "single char", passed, total);
|
||||
|
||||
RunEmitterErrorTest(&Emitter::ExtraEndSeq, "extra EndSeq", passed, total);
|
||||
RunEmitterErrorTest(&Emitter::ExtraEndMap, "extra EndMap", passed, total);
|
||||
|
Reference in New Issue
Block a user