diff --git a/mlir/include/mlir/Support/IndentedOstream.h b/mlir/include/mlir/Support/IndentedOstream.h --- a/mlir/include/mlir/Support/IndentedOstream.h +++ b/mlir/include/mlir/Support/IndentedOstream.h @@ -113,17 +113,13 @@ inline raw_indented_ostream & mlir::raw_indented_ostream::printReindented(StringRef str, StringRef extraPrefix) { - // Before splitting on \n, remove Windows \r characters from \r\n endings. - std::string normalizedStr = str.str(); - normalizedStr.erase( - std::remove(normalizedStr.begin(), normalizedStr.end(), '\r'), - normalizedStr.cend()); - StringRef output(normalizedStr); - + StringRef output = str; // Skip empty lines. while (!output.empty()) { auto split = output.split('\n'); - size_t indent = split.first.find_first_not_of(" \t"); + // Trim Windows \r characters from \r\n line endings. + auto firstTrimmed = split.first.rtrim('\r'); + size_t indent = firstTrimmed.find_first_not_of(" \t"); if (indent != StringRef::npos) { // Set an initial value. leadingWs = indent; diff --git a/mlir/unittests/Support/IndentedOstreamTest.cpp b/mlir/unittests/Support/IndentedOstreamTest.cpp --- a/mlir/unittests/Support/IndentedOstreamTest.cpp +++ b/mlir/unittests/Support/IndentedOstreamTest.cpp @@ -121,6 +121,6 @@ "\r\n\r\n\r\n First line\r\n second line"; ros.printReindented(desc); ros.flush(); - const auto *expected = "First line\n second line"; + const auto *expected = "First line\r\n second line"; EXPECT_THAT(os.str(), StrEq(expected)); }