From 7c9298d8b10b5f4e69e24e3eb933e93e0d92fc37 Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Tue, 18 Jun 2019 19:49:34 -0400 Subject: StringUtil::extractLine (#989) * Added extractLine line parsing to StringUtil. Use for matching lines instead of calcLines. calcLines uses extractLine to extract lines. Fixed problems found in output of some tests- due to how a how final line is handled. Now a final line has a \r or \n\r combination, but nothing else after it, it is considered the last line (not the line after it). * Use StringUtil::extractLine in slang-generate. * Improved comment on extractLine * Remove test code from StringUtil::extractLine * Made StringUtil::extractLine act as if line terminators are 'separators'. Added unit-test-string.cpp - to check behavior. * Adding LineParser - not entirely necessary, but slightly easier to use. * Improved LineParser::Iterator end testing. Added improved tests for LineParser. * Move line comparson after termination case - to fix problem with gcc release build. * Make UnownedStringSlice handle comparison when begin is nullptr - as it uses memcmp and passing nullptr to memcmp is undefined, leading to optimizer being able to do some unfortunate optimizations on gcc. --- tools/slang-generate/main.cpp | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) (limited to 'tools/slang-generate/main.cpp') diff --git a/tools/slang-generate/main.cpp b/tools/slang-generate/main.cpp index 046b2063c..7c0e3672a 100644 --- a/tools/slang-generate/main.cpp +++ b/tools/slang-generate/main.cpp @@ -554,30 +554,26 @@ void emitSimpleText( FILE* stream, StringSpan const& span) { - char const* cursor = span.begin(); - char const* end = span.end(); - - while (cursor != end) + UnownedStringSlice content = span; + while (true) { - int c = *cursor++; - switch (c) + const auto line = StringUtil::extractLine(content); + if (line.begin() == nullptr) { - default: - fprintf(stream, "%c", c); break; + } - case '\r': case '\n': - if (cursor != end) - { - int d = *cursor; - if ((c ^ d) == ('\r' ^ '\n')) - { - cursor++; - } - fprintf(stream, "\n"); - } + // Write the line + fwrite(line.begin(), 1, line.size(), stream); + + // Specially handle the 'final line', excluding an empty line after \n. + // We can detect, as if input ends with 'cr/lf' combination, content.begin == span.end(), else if content.begin() == nullptr. + if (content.begin() == nullptr || content.begin() == span.end()) + { break; } + + fprintf(stream, "\n"); } } -- cgit v1.2.3