mirror of
https://github.com/MaskRay/ccls.git
synced 2025-01-31 18:00:26 +00:00
Make SafeGetLine work the same across different file-ending styles.
This commit is contained in:
parent
3ca2d0f2ad
commit
f84945fb51
22
src/utils.cc
22
src/utils.cc
@ -198,22 +198,22 @@ std::istream& SafeGetline(std::istream& is, std::string& t) {
|
|||||||
t.clear();
|
t.clear();
|
||||||
|
|
||||||
// The characters in the stream are read one-by-one using a std::streambuf.
|
// The characters in the stream are read one-by-one using a std::streambuf.
|
||||||
// That is faster than reading them one-by-one using the std::istream.
|
// That is faster than reading them one-by-one using the std::istream. Code
|
||||||
// Code that uses streambuf this way must be guarded by a sentry object.
|
// that uses streambuf this way must be guarded by a sentry object. The sentry
|
||||||
// The sentry object performs various tasks,
|
// object performs various tasks, such as thread synchronization and updating
|
||||||
// such as thread synchronization and updating the stream state.
|
// the stream state.
|
||||||
|
|
||||||
std::istream::sentry se(is, true);
|
std::istream::sentry se(is, true);
|
||||||
std::streambuf* sb = is.rdbuf();
|
std::streambuf* sb = is.rdbuf();
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
|
bool had_r = false;
|
||||||
int c = sb->sbumpc();
|
int c = sb->sbumpc();
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case '\n':
|
|
||||||
return is;
|
|
||||||
case '\r':
|
case '\r':
|
||||||
if (sb->sgetc() == '\n')
|
had_r = true;
|
||||||
sb->sbumpc();
|
break;
|
||||||
|
case '\n':
|
||||||
return is;
|
return is;
|
||||||
case EOF:
|
case EOF:
|
||||||
// Also handle the case when the last line has no line ending
|
// Also handle the case when the last line has no line ending
|
||||||
@ -221,6 +221,10 @@ std::istream& SafeGetline(std::istream& is, std::string& t) {
|
|||||||
is.setstate(std::ios::eofbit);
|
is.setstate(std::ios::eofbit);
|
||||||
return is;
|
return is;
|
||||||
default:
|
default:
|
||||||
|
if (had_r) {
|
||||||
|
t += '\r';
|
||||||
|
had_r = false;
|
||||||
|
}
|
||||||
t += (char)c;
|
t += (char)c;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -306,7 +310,7 @@ std::unordered_map<std::string, std::string> ParseTestExpectation(
|
|||||||
|
|
||||||
in_output = true;
|
in_output = true;
|
||||||
} else if (in_output)
|
} else if (in_output)
|
||||||
active_output_contents += line + "\n";
|
active_output_contents += line + "\r\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (in_output)
|
if (in_output)
|
||||||
|
Loading…
Reference in New Issue
Block a user