}
}
-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;
}
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;
}
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);
}
}