Skip to content

Commit

Permalink
Merge pull request #46 from ninedraft/use-strings-builder
Browse files Browse the repository at this point in the history
Use `strings.Builder` if it's available
  • Loading branch information
huandu authored Jun 10, 2020
2 parents ee63bf7 + ac7d5db commit 52197b1
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 31 deletions.
8 changes: 2 additions & 6 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@

package xstrings

import (
"bytes"
)

const bufferMaxInitGrowSize = 2048

// Lazy initialize a buffer.
func allocBuffer(orig, cur string) *bytes.Buffer {
output := &bytes.Buffer{}
func allocBuffer(orig, cur string) *stringBuilder {
output := &stringBuilder{}
maxSize := len(orig) * 4

// Avoid to reserve too much memory at once.
Expand Down
15 changes: 7 additions & 8 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package xstrings

import (
"bytes"
"math/rand"
"unicode"
"unicode/utf8"
Expand All @@ -23,7 +22,7 @@ func ToCamelCase(str string) string {
return ""
}

buf := &bytes.Buffer{}
buf := &stringBuilder{}
var r0, r1 rune
var size int

Expand Down Expand Up @@ -112,7 +111,7 @@ func camelCaseToLowerCase(str string, connector rune) string {
return ""
}

buf := &bytes.Buffer{}
buf := &stringBuilder{}
wt, word, remaining := nextWord(str)

for len(remaining) > 0 {
Expand Down Expand Up @@ -374,7 +373,7 @@ func nextValidRune(str string, prev rune) (r rune, size int) {
return
}

func toLower(buf *bytes.Buffer, wt wordType, str string, connector rune) {
func toLower(buf *stringBuilder, wt wordType, str string, connector rune) {
buf.Grow(buf.Len() + len(str))

if wt != upperCaseWord && wt != connectorWord {
Expand All @@ -401,7 +400,7 @@ func SwapCase(str string) string {
var r rune
var size int

buf := &bytes.Buffer{}
buf := &stringBuilder{}

for len(str) > 0 {
r, size = utf8.DecodeRuneInString(str)
Expand Down Expand Up @@ -435,7 +434,7 @@ func FirstRuneToUpper(str string) string {
return str
}

buf := &bytes.Buffer{}
buf := &stringBuilder{}
buf.WriteRune(unicode.ToUpper(r))
buf.WriteString(str[size:])
return buf.String()
Expand All @@ -453,7 +452,7 @@ func FirstRuneToLower(str string) string {
return str
}

buf := &bytes.Buffer{}
buf := &stringBuilder{}
buf.WriteRune(unicode.ToLower(r))
buf.WriteString(str[size:])
return buf.String()
Expand Down Expand Up @@ -566,7 +565,7 @@ func Successor(str string) string {

// Needs to add one character for carry.
if i < 0 && carry != ' ' {
buf := &bytes.Buffer{}
buf := &stringBuilder{}
buf.Grow(l + 4) // Reserve enough space for write.

if lastAlphanumeric != 0 {
Expand Down
8 changes: 4 additions & 4 deletions count_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ func TestWordCount(t *testing.T) {
"中文": "0",
"你好,sekai!": "1",
"oh, it's super-fancy!!a": "4",
"": "0",
"-": "0",
"it's-'s": "1",
"": "0",
"-": "0",
"it's-'s": "1",
})
}

Expand All @@ -45,7 +45,7 @@ func TestWidth(t *testing.T) {
runTestCases(t, runner, _M{
"abcd\t0123\n7890": "12",
"中zh英eng文混排": "15",
"": "0",
"": "0",
})
}

Expand Down
13 changes: 6 additions & 7 deletions format.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package xstrings

import (
"bytes"
"unicode/utf8"
)

Expand All @@ -28,7 +27,7 @@ func ExpandTabs(str string, tabSize int) string {

var r rune
var i, size, column, expand int
var output *bytes.Buffer
var output *stringBuilder

orig := str

Expand All @@ -43,7 +42,7 @@ func ExpandTabs(str string, tabSize int) string {
}

for i = 0; i < expand; i++ {
output.WriteByte(byte(' '))
output.WriteRune(' ')
}

column += expand
Expand Down Expand Up @@ -88,7 +87,7 @@ func LeftJustify(str string, length int, pad string) string {
remains := length - l
padLen := Len(pad)

output := &bytes.Buffer{}
output := &stringBuilder{}
output.Grow(len(str) + (remains/padLen+1)*len(pad))
output.WriteString(str)
writePadString(output, pad, padLen, remains)
Expand All @@ -114,7 +113,7 @@ func RightJustify(str string, length int, pad string) string {
remains := length - l
padLen := Len(pad)

output := &bytes.Buffer{}
output := &stringBuilder{}
output.Grow(len(str) + (remains/padLen+1)*len(pad))
writePadString(output, pad, padLen, remains)
output.WriteString(str)
Expand All @@ -140,15 +139,15 @@ func Center(str string, length int, pad string) string {
remains := length - l
padLen := Len(pad)

output := &bytes.Buffer{}
output := &stringBuilder{}
output.Grow(len(str) + (remains/padLen+1)*len(pad))
writePadString(output, pad, padLen, remains/2)
output.WriteString(str)
writePadString(output, pad, padLen, (remains+1)/2)
return output.String()
}

func writePadString(output *bytes.Buffer, pad string, padLen, remains int) {
func writePadString(output *stringBuilder, pad string, padLen, remains int) {
var r rune
var size int

Expand Down
5 changes: 2 additions & 3 deletions manipulate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package xstrings

import (
"bytes"
"strings"
"unicode/utf8"
)
Expand Down Expand Up @@ -131,7 +130,7 @@ func Insert(dst, src string, index int) string {
// Scrub scrubs invalid utf8 bytes with repl string.
// Adjacent invalid bytes are replaced only once.
func Scrub(str, repl string) string {
var buf *bytes.Buffer
var buf *stringBuilder
var r rune
var size, pos int
var hasError bool
Expand All @@ -144,7 +143,7 @@ func Scrub(str, repl string) string {
if r == utf8.RuneError {
if !hasError {
if buf == nil {
buf = &bytes.Buffer{}
buf = &stringBuilder{}
}

buf.WriteString(origin[:pos])
Expand Down
7 changes: 7 additions & 0 deletions stringbuilder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//+build go1.10

package xstrings

import "strings"

type stringBuilder = strings.Builder
9 changes: 9 additions & 0 deletions stringbuilder_go110.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//+build !go1.10

package xstrings

import "bytes"

type stringBuilder struct {
bytes.Buffer
}
5 changes: 2 additions & 3 deletions translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package xstrings

import (
"bytes"
"unicode"
"unicode/utf8"
)
Expand Down Expand Up @@ -303,7 +302,7 @@ func (tr *Translator) Translate(str string) string {

orig := str

var output *bytes.Buffer
var output *stringBuilder

for len(str) > 0 {
r, size = utf8.DecodeRuneInString(str)
Expand Down Expand Up @@ -500,7 +499,7 @@ func Squeeze(str, pattern string) string {
var size int
var skipSqueeze, matched bool
var tr *Translator
var output *bytes.Buffer
var output *stringBuilder

orig := str
last = -1
Expand Down

0 comments on commit 52197b1

Please sign in to comment.