]> granicus.if.org Git - re2c/commitdiff
Consistently convert all newlines in the generated file to Unix-style LF.
authorUlya Trofimovich <skvadrik@gmail.com>
Wed, 6 Mar 2019 11:52:53 +0000 (11:52 +0000)
committerUlya Trofimovich <skvadrik@gmail.com>
Wed, 6 Mar 2019 12:00:23 +0000 (12:00 +0000)
Some newlines originate in user-defined code (including semantic actions
and code fragments in configurations and directives), some are generated
by re2c itself. In order to maintain consistency we convert all newlines
to LF when writing output to file.

re2c/src/codegen/output.cc

index 7b33d38045bb3494c1022263551d0db64b1755c4..c1bb4c552365d8bc986daeb803062c9c68ea9020 100644 (file)
@@ -32,18 +32,28 @@ OutputFragment::~OutputFragment()
     }
 }
 
-uint32_t OutputFragment::count_lines () const
+static uint32_t write_converting_newlines(const std::string &str, FILE *f)
 {
+    const char *s = str.c_str(), *e = s + str.length();
     uint32_t lines = 0;
-    const std::string content = stream.str ();
-    const char * p = content.c_str ();
-    for (uint32_t i = 0; i < content.size (); ++i)
-    {
-        if (p[i] == '\n')
-        {
+
+    // In order to maintain consistency we convert all newlines to LF when
+    // writing output to file. Some newlines originate in user-defined code
+    // (including semantic actions and code fragments in configurations and
+    // directives), and some are generated by re2c itself.
+    for (const char *p = s;; ++p) {
+        size_t l = static_cast<size_t>(p - s);
+        if (p == e) {
+            fwrite(s, 1, l, f);
+            break;
+        } else if (*p == '\n') {
             ++lines;
+            if (p > s && p[-1] == '\r') --l;
+            fwrite(s, 1, l, f);
+            s = p;
         }
     }
+
     return lines;
 }
 
@@ -117,19 +127,7 @@ Output &Output::wraw(const char *s, const char *e)
             code = !isspace(*p);
         }
 
-        // convert CR LF to LF
-        std::ostream &o = stream();
-        for (const char *p = s;; ++p) {
-            std::streamsize l = p - s;
-            if (p == e) {
-                o.write(s, l);
-                break;
-            } else if (*p == '\n') {
-                if (p > s && p[-1] == '\r') --l;
-                o.write(s, l);
-                s = p;
-            }
-        }
+        stream().write(s, e - s);
     }
     return *this;
 }
@@ -543,9 +541,7 @@ bool Output::emit_blocks(const std::string &fname, blocks_t &blocks,
                 break;
             }
 
-            std::string content = o.str();
-            fwrite(content.c_str(), 1, content.size(), file);
-            line_count += f.count_lines();
+            line_count += write_converting_newlines(o.str(), file);
         }
     }