From 5858f17efa06df3db504741c0ed08f753d217fb2 Mon Sep 17 00:00:00 2001 From: ionous Date: Sat, 23 Dec 2023 15:04:30 -0800 Subject: [PATCH] swap context string buffer for a slice --- note/context.go | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/note/context.go b/note/context.go index 4321ea3..ede980a 100644 --- a/note/context.go +++ b/note/context.go @@ -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] + } }