From f84945fb51896091d287023c239e7fa00cb112c7 Mon Sep 17 00:00:00 2001 From: Jacob Dufault Date: Mon, 27 Nov 2017 17:53:14 -0800 Subject: [PATCH] Make SafeGetLine work the same across different file-ending styles. --- src/utils.cc | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/utils.cc b/src/utils.cc index be83fa04..128a10b0 100644 --- a/src/utils.cc +++ b/src/utils.cc @@ -198,22 +198,22 @@ std::istream& SafeGetline(std::istream& is, std::string& t) { t.clear(); // 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. - // Code that uses streambuf this way must be guarded by a sentry object. - // The sentry object performs various tasks, - // such as thread synchronization and updating the stream state. + // That is faster than reading them one-by-one using the std::istream. Code + // that uses streambuf this way must be guarded by a sentry object. The sentry + // object performs various tasks, such as thread synchronization and updating + // the stream state. std::istream::sentry se(is, true); std::streambuf* sb = is.rdbuf(); for (;;) { + bool had_r = false; int c = sb->sbumpc(); switch (c) { - case '\n': - return is; case '\r': - if (sb->sgetc() == '\n') - sb->sbumpc(); + had_r = true; + break; + case '\n': return is; case EOF: // 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); return is; default: + if (had_r) { + t += '\r'; + had_r = false; + } t += (char)c; } } @@ -306,7 +310,7 @@ std::unordered_map ParseTestExpectation( in_output = true; } else if (in_output) - active_output_contents += line + "\n"; + active_output_contents += line + "\r\n"; } if (in_output)