Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: parse files with '\r' symbols as line ending correctly #1312

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,24 @@ char Stream::get() {
AdvanceCurrent();
m_mark.column++;

if (ch == '\n') {
// if line ending symbol is unknown, set it to the first
// encountered line ending.
// if line ending '\r' set ending symbol to '\r'
// other wise set it to '\n'
if (!m_lineEndingSymbol) {
if (ch == '\n') { // line ending is '\n'
m_lineEndingSymbol = '\n';
} else if (ch == '\r') {
auto ch2 = peek();
if (ch2 == '\n') { // line ending is '\r\n'
m_lineEndingSymbol = '\n';
} else { // line ending is '\r'
m_lineEndingSymbol = '\r';
}
}
}

if (ch == m_lineEndingSymbol) {
m_mark.column = 0;
m_mark.line++;
}
Expand Down
1 change: 1 addition & 0 deletions src/stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class Stream {
Mark m_mark;

CharacterSet m_charSet;
char m_lineEndingSymbol{}; // 0 means it is not determined yet, must be '\n' or '\r'
mutable std::deque<char> m_readahead;
unsigned char* const m_pPrefetched;
mutable size_t m_nPrefetchedAvailable;
Expand Down
16 changes: 16 additions & 0 deletions test/integration/load_node_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,5 +360,21 @@ TEST(LoadNodeTest, BlockCRNLEncoded) {
EXPECT_EQ(1, node["followup"].as<int>());
}

TEST(LoadNodeTest, BlockCREncoded) {
Node node = Load(
"blockText: |\r"
" some arbitrary text \r"
" spanning some \r"
" lines, that are split \r"
" by CR and NL\r"
"followup: 1");
EXPECT_EQ(
"some arbitrary text \nspanning some \nlines, that are split \nby CR and "
"NL\n",
node["blockText"].as<std::string>());
EXPECT_EQ(1, node["followup"].as<int>());
}


} // namespace
} // namespace YAML