summaryrefslogtreecommitdiff
path: root/ZAPD/OutputFormatter.cpp
diff options
context:
space:
mode:
authorglankk <glankk@users.noreply.github.com>2021-04-22 20:47:05 +0200
committerGitHub <noreply@github.com>2021-04-22 14:47:05 -0400
commite06757c87e317e6bb102bd39fbe4cdcfed277fa9 (patch)
tree607a0457e3ceb2eea34e27fbe64e0a11ae168525 /ZAPD/OutputFormatter.cpp
parent21ba65d4c3eb3dbbf196f8b70c61add08b491658 (diff)
Outputformatter fixes (#117)
Diffstat (limited to 'ZAPD/OutputFormatter.cpp')
-rw-r--r--ZAPD/OutputFormatter.cpp104
1 files changed, 60 insertions, 44 deletions
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);
}