diff options
author | Charlie Stanton <charlie@shtanton.xyz> | 2023-04-25 09:56:00 +0100 |
---|---|---|
committer | Charlie Stanton <charlie@shtanton.xyz> | 2023-04-25 09:56:00 +0100 |
commit | cce21232cc83060a53ecb3a7c30d7f6fbfd7a529 (patch) | |
tree | 5c562f71845615f324e3d450b4d4daeaad8dd54a /walk | |
parent | 6ec9d0a831849ffaf7d46b2eb4db7d56260809cf (diff) | |
download | stred-go-cce21232cc83060a53ecb3a7c30d7f6fbfd7a529.tar |
Now uses a buffered output for writing to improve performance
Diffstat (limited to 'walk')
-rw-r--r-- | walk/walk.go | 41 |
1 files changed, 22 insertions, 19 deletions
diff --git a/walk/walk.go b/walk/walk.go index 0719d1c..f9bac2a 100644 --- a/walk/walk.go +++ b/walk/walk.go @@ -511,10 +511,11 @@ const ( type JSONOut struct { structure []JSONOutStructure + writer *bufio.Writer } func (out *JSONOut) indent(adjust int) { - fmt.Print(strings.Repeat("\t", len(out.structure) - 1 + adjust)) + fmt.Fprint(out.writer, strings.Repeat("\t", len(out.structure) - 1 + adjust)) } func (out *JSONOut) atomOut(key string, atom Atom) { @@ -525,46 +526,46 @@ func (out *JSONOut) atomOut(key string, atom Atom) { case AtomNull, AtomBool, AtomNumber: out.indent(0) if state == JSONOutMap { - fmt.Printf("%q: ", key) + fmt.Fprintf(out.writer, "%q: ", key) } - fmt.Print(atom.String()) + fmt.Fprint(out.writer, atom.String()) out.structure = append(out.structure, JSONOutValueEnd) case AtomStringTerminal: out.indent(0) if state == JSONOutMap { - fmt.Printf("%q: ", key) + fmt.Fprintf(out.writer, "%q: ", key) } - fmt.Print("\"") + fmt.Fprint(out.writer, "\"") out.structure = append(out.structure, JSONOutString) case AtomTerminal: switch TerminalValue(atom.data) { case MapBegin: out.indent(0) if state == JSONOutMap { - fmt.Printf("%q: ", key) + fmt.Fprintf(out.writer, "%q: ", key) } - fmt.Print("{\n") + fmt.Fprint(out.writer, "{\n") out.structure = append(out.structure, JSONOutMap) case ArrayBegin: out.indent(0) if state == JSONOutMap { - fmt.Printf("%q: ", key) + fmt.Fprintf(out.writer, "%q: ", key) } - fmt.Print("[\n") + fmt.Fprint(out.writer, "[\n") out.structure = append(out.structure, JSONOutArray) case MapEnd: out.indent(-1) if state != JSONOutMap { panic("Map ended while not inside a map") } - fmt.Print("}") + fmt.Fprint(out.writer, "}") out.structure[len(out.structure) - 1] = JSONOutValueEnd case ArrayEnd: out.indent(-1) if state != JSONOutArray { panic("Array ended while not inside a array") } - fmt.Print("]") + fmt.Fprint(out.writer, "]") out.structure[len(out.structure) - 1] = JSONOutValueEnd default: panic("Invalid TerminalValue") @@ -576,27 +577,27 @@ func (out *JSONOut) atomOut(key string, atom Atom) { out.structure = out.structure[:len(out.structure) - 1] underState := out.structure[len(out.structure) - 1] if underState == JSONOutMap && atom.Typ == AtomTerminal && TerminalValue(atom.data) == MapEnd { - fmt.Print("\n") + fmt.Fprint(out.writer, "\n") out.indent(-1) - fmt.Print("}") + fmt.Fprint(out.writer, "}") out.structure[len(out.structure) - 1] = JSONOutValueEnd } else if underState == JSONOutArray && atom.Typ == AtomTerminal && TerminalValue(atom.data) == ArrayEnd { - fmt.Print("\n") + fmt.Fprint(out.writer, "\n") out.indent(-1) - fmt.Print("]") + fmt.Fprint(out.writer, "]") out.structure[len(out.structure) - 1] = JSONOutValueEnd } else if underState == JSONOutRoot { panic("Tried to output JSON after root value has concluded") } else { - fmt.Print(",\n") + fmt.Fprint(out.writer, ",\n") out.atomOut(key, atom) } case JSONOutString: if atom.Typ == AtomStringTerminal { - fmt.Print("\"") + fmt.Fprint(out.writer, "\"") out.structure[len(out.structure) - 1] = JSONOutValueEnd } else { - fmt.Print(atom.String()) + fmt.Fprint(out.writer, atom.String()) } default: panic("Invalid JSONOutState") @@ -615,14 +616,16 @@ func (out *JSONOut) Print(path Path, values []Atom) { } func (out *JSONOut) AssertDone() { + out.writer.Flush() if len(out.structure) != 2 || out.structure[0] != JSONOutRoot || out.structure[1] != JSONOutValueEnd { panic("Program ended with incomplete JSON output") } } -func NewJSONOut() JSONOut { +func NewJSONOut(writer *bufio.Writer) JSONOut { return JSONOut { structure: []JSONOutStructure{JSONOutRoot}, + writer: writer, } } |