From cf062e09037c7e54a821b05ef50b3e86683090f8 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Thu, 26 Jan 2023 13:59:35 -0700 Subject: Improve/fix documentation and comments --- reencode.go | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'reencode.go') diff --git a/reencode.go b/reencode.go index b20a503..1bcfc74 100644 --- a/reencode.go +++ b/reencode.go @@ -30,6 +30,10 @@ type speculation struct { // The memory use of a ReEncoder is O( (CompactIfUnder+1)^2 + depth). type ReEncoder struct { // The output stream to write the re-encoded JSON to. + // + // A ReEncoder tends to make many small writes; if Out.Write + // calls are syscalls, then you may want to wrap Out in a + // bufio.Writer. Out io.Writer // A JSON document is specified to be a single JSON element; @@ -99,14 +103,14 @@ type ReEncoder struct { // public API ////////////////////////////////////////////////////////////////// -// Write implements io.Writer; it does what you'd expect, mostly. +// Write implements io.Writer; it does what you'd expect. // -// Rather than returning the number of bytes written to the output -// stream, it returns the nubmer of bytes from p that it successfully -// handled. This distinction is because *ReEncoder transforms the -// data written to it, and the number of bytes written may be wildly -// different than the number of bytes handled; and that would break -// virtually all users of io.Writer. +// It is worth noting that Write returns the number of bytes consumed +// from p, not number of bytes written to the output stream. This +// distinction that most io.Writer implementations don't need to make, +// but *ReEncoder does because it transforms the data written to it, +// and the number of bytes written may be wildly different than the +// number of bytes handled. func (enc *ReEncoder) Write(p []byte) (int, error) { if len(p) == 0 { return 0, nil @@ -163,7 +167,7 @@ func (enc *ReEncoder) Close() error { return nil } -// WriteRune write a single Unicode code point, returning the number +// WriteRune writes a single Unicode code point, returning the number // of bytes written to the output stream and any error. // // Even when there is no error, the number of bytes written may be @@ -245,7 +249,7 @@ func (enc *ReEncoder) handleRune(c rune, t internal.RuneType) error { } } else { // speculating - // conCompress is whether we're 1-up from the leaf; + // canCompress is whether we're 1-up from the leaf; // set this *before* the calls to .handleRune. canCompress := enc.handleRuneState.specu.indentFmt.handleRuneState.specu == nil -- cgit v1.2.3-2-g168b From 690b0cbb2a220ef4179a0f9f34725328fb8c73f8 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Thu, 26 Jan 2023 23:36:55 -0700 Subject: .golangci.yml: Turn on 'godot', fix --- reencode.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'reencode.go') diff --git a/reencode.go b/reencode.go index 1bcfc74..6b9c336 100644 --- a/reencode.go +++ b/reencode.go @@ -290,8 +290,8 @@ func (enc *ReEncoder) handleRuneNoSpeculation(c rune, t internal.RuneType) error return enc.handleRuneMain(c, t) } -// handle buffered things that need to happen before the new rune -// itself is handled. +// handleRunePre handles buffered things that need to happen before +// the new rune itself is handled. func (enc *ReEncoder) handleRunePre(c rune, t internal.RuneType) (error, bool) { // emit newlines between top-level values if enc.handleRuneState.lastNonSpace == internal.RuneTypeEOF { @@ -384,7 +384,7 @@ func (enc *ReEncoder) handleRunePre(c rune, t internal.RuneType) (error, bool) { return nil, true } -// handle the new rune itself, not buffered things +// handleRuneMain handles the new rune itself, not buffered things. func (enc *ReEncoder) handleRuneMain(c rune, t internal.RuneType) error { defer func() { if t != internal.RuneTypeSpace { -- cgit v1.2.3-2-g168b