Skip to content

Commit

Permalink
swap context string buffer for a slice
Browse files Browse the repository at this point in the history
  • Loading branch information
ionous committed Dec 23, 2023
1 parent 8cbb624 commit 5858f17
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions note/context.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
package note

import "strings"
import (
"strings"

"github.com/ionous/tell/runes"
)

// provides communication between note takers:
// each in progress document decoder should have its own unique context.
// concrete instances shouldn't be copied.
type Context struct {
buf strings.Builder
}
type Context []string

func (ctx *Context) writeInto(out *strings.Builder) {
if str := ctx.buf.String(); len(str) > 0 {
out.WriteString(str)
ctx.buf.Reset()
}
func (ctx *Context) pending() bool {
return len(*ctx) > 0
}

func (ctx *Context) append(str string) {
appendLine(&ctx.buf, str)
(*ctx) = append(*ctx, str)
}

func (ctx *Context) pending() bool {
return ctx.buf.Len() > 0
func (ctx *Context) writeInto(out *strings.Builder) {
if ctx.pending() {
for i, el := range *ctx {
if i > 0 {
out.WriteRune(runes.Newline)
}
out.WriteString(el)
}
*ctx = (*ctx)[:0]
}
}

0 comments on commit 5858f17

Please sign in to comment.