diff --git a/include/yaml-cpp/emittermanip.h b/include/yaml-cpp/emittermanip.h index 010d860..73541c3 100644 --- a/include/yaml-cpp/emittermanip.h +++ b/include/yaml-cpp/emittermanip.h @@ -25,6 +25,7 @@ enum EMITTER_MANIP { SingleQuoted, DoubleQuoted, Literal, + Folded, // null manipulators LowerNull, diff --git a/src/emitter.cpp b/src/emitter.cpp index cf093e0..99367b6 100644 --- a/src/emitter.cpp +++ b/src/emitter.cpp @@ -695,7 +695,7 @@ Emitter& Emitter::Write(const std::string& str) { Utils::ComputeStringFormat(str, m_pState->GetStringFormat(), m_pState->CurGroupFlowType(), escapeNonAscii); - if (strFormat == StringFormat::Literal) + if (strFormat == StringFormat::Literal || strFormat == StringFormat::Folded) m_pState->SetMapKeyFormat(YAML::LongKey, FmtScope::Local); PrepareNode(EmitterNodeType::Scalar); @@ -713,6 +713,9 @@ Emitter& Emitter::Write(const std::string& str) { case StringFormat::Literal: Utils::WriteLiteralString(m_stream, str, m_pState->CurIndent() + m_pState->GetIndent()); + case StringFormat::Folded: + Utils::WriteFoldedString(m_stream, str, + m_pState->CurIndent() + m_pState->GetIndent()); break; } diff --git a/src/emitterstate.cpp b/src/emitterstate.cpp index 40497f7..b803228 100644 --- a/src/emitterstate.cpp +++ b/src/emitterstate.cpp @@ -244,6 +244,7 @@ bool EmitterState::SetStringFormat(EMITTER_MANIP value, FmtScope::value scope) { case SingleQuoted: case DoubleQuoted: case Literal: + case Folded: _Set(m_strFmt, value, scope); return true; default: diff --git a/src/emitterutils.cpp b/src/emitterutils.cpp index 0410f93..e52f02d 100644 --- a/src/emitterutils.cpp +++ b/src/emitterutils.cpp @@ -275,6 +275,11 @@ StringFormat::value ComputeStringFormat(const std::string& str, return StringFormat::Literal; } return StringFormat::DoubleQuoted; + case Folded: + if (IsValidLiteralScalar(str, flowType, escapeNonAscii)) { + return StringFormat::Folded; + } + return StringFormat::DoubleQuoted; default: break; } @@ -362,6 +367,22 @@ bool WriteLiteralString(ostream_wrapper& out, const std::string& str, return true; } +bool WriteFoldedString(ostream_wrapper& out, const std::string& str, + std::size_t indent) { + out << ">\n"; + out << IndentTo(indent); + int codePoint; + for (std::string::const_iterator i = str.begin(); + GetNextCodePointAndAdvance(codePoint, i, str.end());) { + if (codePoint == '\n') { + out << "\n" << IndentTo(indent); + } else { + WriteCodePoint(out, codePoint); + } + } + return true; +} + bool WriteChar(ostream_wrapper& out, char ch) { if (('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z')) { out << ch; diff --git a/src/emitterutils.h b/src/emitterutils.h index 6cc7319..2ee7992 100644 --- a/src/emitterutils.h +++ b/src/emitterutils.h @@ -21,7 +21,7 @@ namespace YAML { class Binary; struct StringFormat { - enum value { Plain, SingleQuoted, DoubleQuoted, Literal }; + enum value { Plain, SingleQuoted, DoubleQuoted, Literal, Folded }; }; namespace Utils { @@ -35,6 +35,8 @@ bool WriteDoubleQuotedString(ostream_wrapper& out, const std::string& str, bool escapeNonAscii); bool WriteLiteralString(ostream_wrapper& out, const std::string& str, std::size_t indent); +bool WriteFoldedString(ostream_wrapper& out, const std::string& str, + std::size_t indent); bool WriteChar(ostream_wrapper& out, char ch); bool WriteComment(ostream_wrapper& out, const std::string& str, std::size_t postCommentIndent);