Skip to content

Commit 14b2246

Browse files
committed
Utf16Reader: Improve line ending handling and add PeekChar method
1 parent 3013092 commit 14b2246

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

src/Explorer/UTF16Stream.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,13 @@ bool Utf16Reader::getline(std::wstring& line)
112112
break;
113113
}
114114
if (ch == L'\r') {
115+
// CR encountered - check if followed by LF
115116
wchar_t next;
116-
if (ReadChar(next)) {
117-
if (next == L'\n') {
118-
// Found \r\n - line ending
119-
break;
120-
}
121-
// \r followed by something else - include only the next char
122-
line += next;
117+
if (PeekChar(next) && next == L'\n') {
118+
// CRLF sequence - consume the LF
119+
ReadChar(next);
123120
}
124-
// Single \r as line ending
121+
// CR alone or CRLF - line ending (LF already consumed if present)
125122
break;
126123
}
127124
else {
@@ -136,7 +133,10 @@ bool Utf16Reader::eof() const
136133
return file_.eof();
137134
}
138135

139-
void Utf16Reader::close() { file_.close(); }
136+
void Utf16Reader::close()
137+
{
138+
file_.close();
139+
}
140140

141141
bool Utf16Reader::ReadChar(wchar_t& ch)
142142
{
@@ -148,6 +148,14 @@ bool Utf16Reader::ReadChar(wchar_t& ch)
148148
return true;
149149
}
150150

151+
bool Utf16Reader::PeekChar(wchar_t& ch)
152+
{
153+
ch = file_.peek();
154+
if (ch == WEOF) {
155+
return false;
156+
}
157+
return true;
158+
}
151159

152160

153161
Utf16Writer::Utf16Writer(const std::filesystem::path& filename)

src/Explorer/UTF16Stream.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Utf16Reader {
1313

1414
private:
1515
bool ReadChar(wchar_t& ch);
16+
bool PeekChar(wchar_t& ch);
1617
std::ifstream file_;
1718
};
1819

0 commit comments

Comments
 (0)