From e06757c87e317e6bb102bd39fbe4cdcfed277fa9 Mon Sep 17 00:00:00 2001 From: glankk Date: Thu, 22 Apr 2021 20:47:05 +0200 Subject: Outputformatter fixes (#117) --- ZAPD/OutputFormatter.cpp | 104 +++++++++++++++++++++++++++-------------------- 1 file changed, 60 insertions(+), 44 deletions(-) (limited to 'ZAPD/OutputFormatter.cpp') diff --git a/ZAPD/OutputFormatter.cpp b/ZAPD/OutputFormatter.cpp index 324a12f..33fcb54 100644 --- a/ZAPD/OutputFormatter.cpp +++ b/ZAPD/OutputFormatter.cpp @@ -1,60 +1,80 @@ #include "OutputFormatter.h" +void OutputFormatter::Flush() +{ + if (col > lineLimit) + { + str.append(1, '\n'); + str.append(currentIndent, ' '); + + int newCol = currentIndent + (wordP - word); + + for (int i = 0; i < wordNests; i++) + nestIndent[nest - i] -= col - newCol; + + col = newCol; + } + else + { + str.append(space, spaceP - space); + } + spaceP = space; + + str.append(word, wordP - word); + wordP = word; + wordNests = 0; +} + int OutputFormatter::Write(const char* buf, int count) { for (int i = 0; i < count; i++) { char c = buf[i]; - if (c == '\n') + if (c == ' ' || c == '\t' || c == '\n') { - col = 0; + if (wordP - word != 0) + { + Flush(); + } + + if (c == '\n') + { + col = 0; + *spaceP++ = c; + } + else if (c == '\t') + { + int n = tabSize - (col % tabSize); + col += n; + for (int j = 0; j < n; j++) + *spaceP++ = ' '; + } + else + { + col++; + *spaceP++ = c; + } + currentIndent = nestIndent[nest]; } - else if (c == '\t') - { - int n = tabSize - (col % tabSize); - for (int j = 0; j < n - 1; j++) - *spaceP++ = ' '; - c = ' '; - col += n; - } else { col++; - } - - if (c == '(') - { - nest++; - nestIndent[nest] = col; - currentIndent = col; - } - else if (c == ')') - { - nest--; - } - if (c == ' ' || c == '\t' || c == '\n') - { - str.append(word, wordP - word); - wordP = word; - - *spaceP++ = c; - } - else - { - if (col > lineLimit) + if (c == '(') { - str.append(1, '\n'); - str.append(currentIndent, ' '); - col = currentIndent + 1 + (wordP - word); + nest++; + nestIndent[nest] = col; + wordNests++; } - else + else if (c == ')') { - str.append(space, spaceP - space); + if (nest > 0) + nest--; + if (wordNests > 0) + wordNests--; } - spaceP = space; *wordP++ = c; } @@ -78,17 +98,13 @@ int (*OutputFormatter::StaticWriter())(const char* buf, int count) OutputFormatter::OutputFormatter(int tabSize, int defaultIndent, int lineLimit) : tabSize{tabSize}, defaultIndent{defaultIndent}, lineLimit{lineLimit}, col{0}, nest{0}, - nestIndent{defaultIndent}, currentIndent{defaultIndent}, wordP{word}, spaceP{space} + nestIndent{defaultIndent}, currentIndent{defaultIndent}, wordNests(0), wordP{word}, spaceP{space} { } std::string OutputFormatter::GetOutput() { - str.append(space, spaceP - space); - spaceP = space; - - str.append(word, wordP - word); - wordP = word; + Flush(); return std::move(str); } -- cgit v1.2.3