Skip to content

Commit

Permalink
Merge pull request #29 from knz/20230524-improvements
Browse files Browse the repository at this point in the history
Multiple ergonomic improvements + stop removing trailing spaces
  • Loading branch information
knz committed Jun 12, 2023
2 parents ea921e1 + 7f31255 commit b7e4556
Show file tree
Hide file tree
Showing 7 changed files with 736 additions and 261 deletions.
49 changes: 37 additions & 12 deletions builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,41 @@ UUU
}

func TestMixedWrites(t *testing.T) {
var b StringBuilder
b.SafeString("safe")
b.WriteString("unsafe")
b.SafeString("")
b.WriteByte('U')
b.SafeString("")
b.WriteRune('U')
actual := b.RedactableString()
const expected = `safe‹unsafe›‹U›‹U›`
if actual != expected {
t.Errorf("expected:\n%s\n\ngot:\n%s", expected, actual)
}
t.Run("oneline", func(t *testing.T) {
var b StringBuilder
b.SafeString("safe")
b.WriteString("unsafe")
b.SafeString("")
b.WriteByte('U')
b.SafeString("")
b.WriteRune('U')
actual := b.RedactableString()
const expected = "safe‹unsafeUU›"
if actual != expected {
t.Errorf("expected:\n%s\n\ngot:\n%s", expected, actual)
}
const expectedWithoutMarkers = "safeunsafeUU"
if ractual := actual.StripMarkers(); ractual != expectedWithoutMarkers {
t.Errorf("expected:\n%s\n\ngot:\n%s", expectedWithoutMarkers, ractual)
}
})

t.Run("multiline", func(t *testing.T) {
var b StringBuilder
b.SafeString("\nsafe\n")
b.WriteString("\nunsafe\n")
b.SafeString("\n")
b.WriteByte('\n')
b.SafeString("\n")
b.WriteRune('\n')
actual := b.RedactableString()
const expected = "\nsafe\n\n‹unsafe›\n\n\n\n\n"
if actual != expected {
t.Errorf("expected:\n%q\n\ngot:\n%q", expected, actual)
}
const expectedWithoutMarkers = "\nsafe\n\nunsafe\n\n\n\n\n"
if ractual := actual.StripMarkers(); ractual != expectedWithoutMarkers {
t.Errorf("expected:\n%q\n\ngot:\n%q", expectedWithoutMarkers, ractual)
}
})
}
36 changes: 27 additions & 9 deletions internal/buffer/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package buffer

import (
"bytes"
origFmt "fmt"
"unicode/utf8"
"unsafe"
Expand Down Expand Up @@ -197,11 +198,16 @@ func (b *Buffer) endRedactable() {
if len(b.buf) == 0 {
return
}
p, ok := b.tryGrowByReslice(len(m.EndS))
if !ok {
p = b.grow(len(m.EndS))
if bytes.HasSuffix(b.buf, m.StartBytes) {
// Special case: remove a trailing open marker and call it a day.
b.buf = b.buf[:len(b.buf)-m.StartLen]
} else {
p, ok := b.tryGrowByReslice(m.EndLen)
if !ok {
p = b.grow(m.EndLen)
}
copy(b.buf[p:], m.EndS)
}
copy(b.buf[p:], m.EndS)
b.markerOpen = false
}

Expand All @@ -214,18 +220,23 @@ func (b *Buffer) startWrite() {

// endRedactable adds the closing redaction marker.
func (b *Buffer) startRedactable() {
p, ok := b.tryGrowByReslice(len(m.StartS))
if !ok {
p = b.grow(len(m.StartS))
if bytes.HasSuffix(b.buf, m.EndBytes) {
// Special case: remove a trailing closing marker and call it a day.
b.buf = b.buf[:len(b.buf)-m.EndLen]
} else {
p, ok := b.tryGrowByReslice(len(m.StartS))
if !ok {
p = b.grow(len(m.StartS))
}
copy(b.buf[p:], m.StartS)
}
copy(b.buf[p:], m.StartS)
b.markerOpen = true
}

// escapeToEnd escapes occurrences of redaction markers in
// b.buf[b.validUntil:] and advances b.validUntil until the end.
func (b *Buffer) escapeToEnd(breakNewLines bool) {
b.buf = escape.InternalEscapeBytes(b.buf, b.validUntil, breakNewLines, breakNewLines)
b.buf = escape.InternalEscapeBytes(b.buf, b.validUntil, breakNewLines, false /* trim */)
b.validUntil = len(b.buf)
}

Expand Down Expand Up @@ -313,6 +324,13 @@ func (b *Buffer) Grow(n int) {
b.buf = b.buf[:m]
}

// clone is used in tests.
func (b *Buffer) clone() *Buffer {
c := *b
c.buf = append([]byte(nil), b.buf...)
return &c
}

// makeSlice allocates a slice of size n. If the allocation fails, it panics
// with ErrTooLarge.
func makeSlice(n int) []byte {
Expand Down
Loading

0 comments on commit b7e4556

Please sign in to comment.