From 9ee3928754693565fa561e2b6fda4058948d1d56 Mon Sep 17 00:00:00 2001 From: theamarin <36608650+theamarin@users.noreply.github.com> Date: Mon, 5 Jul 2021 05:30:04 +0200 Subject: [PATCH] Prevent trailing spaces when emitting literal strings (#1005) --- src/emitterutils.cpp | 4 ++-- test/integration/emitter_test.cpp | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/emitterutils.cpp b/src/emitterutils.cpp index 0113c45..c6ad5e5 100644 --- a/src/emitterutils.cpp +++ b/src/emitterutils.cpp @@ -366,13 +366,13 @@ bool WriteDoubleQuotedString(ostream_wrapper& out, const std::string& str, bool WriteLiteralString(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); + out << "\n"; } else { + out<< IndentTo(indent); WriteCodePoint(out, codePoint); } } diff --git a/test/integration/emitter_test.cpp b/test/integration/emitter_test.cpp index 66198a6..f1d6308 100644 --- a/test/integration/emitter_test.cpp +++ b/test/integration/emitter_test.cpp @@ -382,6 +382,20 @@ TEST_F(EmitterTest, ScalarFormat) { "crazy\tsymbols that we like"); } +TEST_F(EmitterTest, LiteralWithoutTrailingSpaces) { + out << YAML::BeginMap; + out << YAML::Key << "key"; + out << YAML::Value << YAML::Literal; + out << "expect that with two newlines\n\n" + "no spaces are emitted in the empty line"; + out << YAML::EndMap; + + ExpectEmit( + "key: |\n" + " expect that with two newlines\n\n" + " no spaces are emitted in the empty line"); +} + TEST_F(EmitterTest, AutoLongKeyScalar) { out << BeginMap; out << Key << Literal << "multi-line\nscalar";