Skip to content

Terminate literal scalars before emitting comments - #1492

Open
vzer200 wants to merge 1 commit into
jbeder:masterfrom
vzer200:fix/literal-scalar-comments
Open

Terminate literal scalars before emitting comments#1492
vzer200 wants to merge 1 commit into
jbeder:masterfrom
vzer200:fix/literal-scalar-comments

Conversation

@vzer200

@vzer200 vzer200 commented Sep 8, 2026

Copy link
Copy Markdown

Fixes #800.

Track the output position after a literal string or binary value. If a comment immediately follows, terminate the literal before writing the comment. This prevents the comment from becoming scalar data and preserves the trailing newlines expected by the literal's chomping indicator. An explicit Newline or intervening output prevents a second newline from being added.

Seven regressions cover string round trips, strip/clip/keep behavior, explicit newlines, nested maps, literal keys, binary values and an external output stream. All 1,067 tests and both CMake contract checks pass in a C++11 Debug build with Clang on Windows; changed ranges pass clang-format.

Independent review also compared 648 combinations against the existing explicit-Newline workaround, with identical output in every case. Existing blank-only/leading-whitespace/CRLF literal limitations remain outside this change.

AI-assisted implementation, independently reviewed and tested by another coding agent.

@alex-thiessen-for-siemens

Copy link
Copy Markdown
Contributor

The commit message body is empty despite a detailed PR description. (by Gemini 3.8 Flash High)

@alex-thiessen-for-siemens

alex-thiessen-for-siemens commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

One edge case to consider is an empty literal string:

out << Literal << "" << Comment("comment");

For non-empty strings, WriteLiteralString leaves the last line un-terminated so subsequent formatting can supply the newline. But for size == 0, WriteLiteralString already writes |-\n. The check in Write(const _Comment&) then adds another \n, producing an extra blank line between the header and the comment:

|-

# comment

instead of:

|-
# comment

While parsers strip the blank line for |-, it creates an unintended blank line in the output.

(by Gemini 3.8 Flash High)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Comment after literal string

2 participants