[PATCH] D83808: [flang] Refine CR handling

Peter Klausler via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 14 14:15:33 PDT 2020


This revision was automatically updated to reflect the committed changes.
Closed by commit rGaf6b8d51390d: [flang] Refine CR handling (authored by klausler).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D83808/new/

https://reviews.llvm.org/D83808

Files:
  flang/include/flang/Parser/char-buffer.h
  flang/lib/Parser/char-buffer.cpp
  flang/lib/Parser/source.cpp


Index: flang/lib/Parser/source.cpp
===================================================================
--- flang/lib/Parser/source.cpp
+++ flang/lib/Parser/source.cpp
@@ -85,10 +85,19 @@
       break;
     }
     std::size_t chunk = crcp - p;
+    auto advance{chunk + 1};
+    if (chunk + 1 >= bytes || crcp[1] == '\n') {
+      // CR followed by LF or EOF: omit
+    } else if ((chunk == 0 && p == buf.data()) || crcp[-1] == '\n') {
+      // CR preceded by LF or BOF: omit
+    } else {
+      // CR in line: retain
+      ++chunk;
+    }
     std::memmove(buffer + wrote, p, chunk);
     wrote += chunk;
-    p += chunk + 1;
-    bytes -= chunk + 1;
+    p += advance;
+    bytes -= advance;
   }
   return wrote;
 }
Index: flang/lib/Parser/char-buffer.cpp
===================================================================
--- flang/lib/Parser/char-buffer.cpp
+++ flang/lib/Parser/char-buffer.cpp
@@ -65,26 +65,4 @@
   CHECK(result.size() == bytes_);
   return result;
 }
-
-std::string CharBuffer::MarshalNormalized() const {
-  std::string result;
-  std::size_t bytes{bytes_};
-  result.reserve(bytes + 1 /* for terminal line feed */);
-  char ch{'\0'};
-  for (const Block &block : blocks_) {
-    std::size_t chunk{std::min(bytes, Block::capacity)};
-    for (std::size_t j{0}; j < chunk; ++j) {
-      ch = block.data[j];
-      if (ch != '\r') {
-        result += ch;
-      }
-    }
-    bytes -= chunk;
-  }
-  if (ch != '\n') {
-    result += '\n';
-  }
-  result.shrink_to_fit();
-  return result;
-}
 } // namespace Fortran::parser
Index: flang/include/flang/Parser/char-buffer.h
===================================================================
--- flang/include/flang/Parser/char-buffer.h
+++ flang/include/flang/Parser/char-buffer.h
@@ -58,9 +58,6 @@
 
   std::string Marshal() const;
 
-  // Removes carriage returns ('\r') and ensures a final line feed ('\n').
-  std::string MarshalNormalized() const;
-
 private:
   struct Block {
     static constexpr std::size_t capacity{1 << 20};


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D83808.277972.patch
Type: text/x-patch
Size: 2012 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200714/715b51fb/attachment.bin>


More information about the llvm-commits mailing list