Skip to content

Commit 992d9c4

Browse files
committed
feat: literal string output 'chomping indicator'
log: fix literal string outputs 'chomping indicator' if required
1 parent b9fd92b commit 992d9c4

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/emitterutils.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,28 @@ bool WriteDoubleQuotedString(ostream_wrapper& out, const char* str, std::size_t
366366

367367
bool WriteLiteralString(ostream_wrapper& out, const char* str, std::size_t size,
368368
std::size_t indent) {
369-
out << "|\n";
369+
// depending on the numbers of new lines at the end of the string
370+
// we need to use 'clip (-)', 'strip (default)' or 'keep' (+) annotation.
371+
// if there is no newline at the end, we need 'clip'
372+
// if there is single new line at the end, we need 'strip'
373+
// otherwise 'keep'
374+
//
375+
// see YAML spec 1.2 chapter '8.1.1.2 Block Chomping Indicator' for more information
376+
// https://yaml.org/spec/1.2.2/#8112-block-chomping-indicator
377+
378+
379+
// The chomping depends on the number of new lines.
380+
// The output following this 'WriteLiteralString' call will add an additional '\n',
381+
// because of this we need to remove one in the case of 'strip' and 'keep'.
382+
if (size == 0 || str[size-1] != '\n') { // clip
383+
out << "|-\n";
384+
} else if (size == 1 || str[size-2] != '\n') { // strip
385+
out << "|\n";
386+
size -= 1;
387+
} else { // 'keep'
388+
out << "|+\n";
389+
size -= 1;
390+
}
370391
int codePoint;
371392
for (const char* i = str;
372393
GetNextCodePointAndAdvance(codePoint, i, str + size);) {

0 commit comments

Comments
 (0)