From 18b47c0af90755e3b3881e4f594ecf8dcdaeca2a Mon Sep 17 00:00:00 2001 From: Ulya Trofimovich Date: Wed, 6 Mar 2019 11:52:53 +0000 Subject: [PATCH] Consistently convert all newlines in the generated file to Unix-style LF. 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 | 42 +++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/re2c/src/codegen/output.cc b/re2c/src/codegen/output.cc index 7b33d380..c1bb4c55 100644 --- a/re2c/src/codegen/output.cc +++ b/re2c/src/codegen/output.cc @@ -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(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); } } -- 2.50.1