Skip to content

Commit 7bb845b

Browse files
committed
Added io.writer interface
1 parent 95acc48 commit 7bb845b

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ All notable changes to **ValueStringBuilder** will be documented in this file. T
66

77
## [Unreleased]
88

9+
### Added
10+
11+
- Implemented the `io.Writer` interface so it can be used with the `fmt` package
12+
913
## [0.5.3] - 2022-12-28
1014

1115
### Changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,30 @@ Also more advanced use cases where you want to insert an arbitrary word at an ar
3636
sb := NewStringBuilderFromString("Hello World")
3737
sb.Insert(5, " my dear")
3838
output := sb.ToString() // Hello my dear World
39+
```
40+
41+
The `StringBuilder` also implements the `io.Writer` interface so it can be use with `fmt.Fprintf` and friends:
42+
```golang
43+
s := &StringBuilder{}
44+
45+
for i := 3; i >= 1; i-- {
46+
fmt.Fprintf(s, "%d...", i)
47+
}
48+
49+
s.Append("lift off")
50+
fmt.PrintLn(s.ToString()) // Prints 3...2...1...lift off
51+
```
52+
53+
## Benchmark
54+
Check out the implementation of the benchmark in the corresponding file. Here are some results:
55+
```no-class
56+
goos: darwin
57+
goarch: arm64
58+
pkg: github.com/linkdotnet/golang-stringbuilder
59+
BenchmarkStringBuilderConcat
60+
BenchmarkStringBuilderConcat-10 343710 3394 ns/op 4352 B/op 5 allocs/op
61+
BenchmarkStringConcat
62+
BenchmarkStringConcat-10 1000000 1113 ns/op 6720 B/op 24 allocs/op
63+
BenchmarkGoStringBuilderConcat
64+
BenchmarkGoStringBuilderConcat-10 4204142 278.4 ns/op 1448 B/op 6 allocs/op
3965
```

stringbuilder.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,15 @@ func (s *StringBuilder) Replace(oldValue string, newValue string) {
184184
}
185185
}
186186

187+
// Implements the io.Writer interface so the StringBuilder can be used with fmt.Printf
188+
func (s *StringBuilder) Write(p []byte) (int, error) {
189+
before := s.Len()
190+
s.Append(string(p))
191+
delta := before - s.Len()
192+
193+
return delta, nil
194+
}
195+
187196
func (s *StringBuilder) grow(lenToAdd int) {
188197
// Grow times 2 until lenToAdd fits
189198
newLen := len(s.data)

stringbuilder_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package Text
22

33
import (
4+
"fmt"
45
"reflect"
56
"testing"
67
)
@@ -334,6 +335,19 @@ func TestReplace(t *testing.T) {
334335
}
335336
}
336337

338+
func TestWrite(t *testing.T) {
339+
const want = "3...2...1..."
340+
s := &StringBuilder{}
341+
342+
for i := 3; i >= 1; i-- {
343+
fmt.Fprintf(s, "%d...", i)
344+
}
345+
346+
if got := s.ToString(); got != want {
347+
t.Errorf("StringBuilder.Write() = %v, want %v", got, want)
348+
}
349+
}
350+
337351
func slicesEqual(a []int, b []int) bool {
338352
if len(a) != len(b) {
339353
return false

0 commit comments

Comments
 (0)