diff --git a/func.go b/func.go index 4945d66..bcfee55 100644 --- a/func.go +++ b/func.go @@ -10,8 +10,18 @@ import ( "unicode" ) +// Defined an interface of stringBuilder that compatible with +// strings.Builder(go 1.10) and bytes.Buffer(< go 1.10) +type stringBuilder interface { + WriteRune(r rune) (n int, err error) + WriteString(s string) (int, error) + Reset() + Grow(n int) + String() string +} + var builderPool = sync.Pool{New: func() interface{} { - return &strings.Builder{} + return newStringBuilder() }} // The XPath function list. @@ -357,7 +367,7 @@ func normalizespaceFunc(q query, t iterator) interface{} { } m = node.Value() } - var b = builderPool.Get().(*strings.Builder) + var b = builderPool.Get().(stringBuilder) b.Grow(len(m)) runeStr := []rune(strings.TrimSpace(m)) @@ -524,7 +534,7 @@ func notFunc(q query, t iterator) interface{} { // concat( string1 , string2 [, stringn]* ) func concatFunc(args ...query) func(query, iterator) interface{} { return func(q query, t iterator) interface{} { - b := builderPool.Get().(*strings.Builder) + b := builderPool.Get().(stringBuilder) for _, v := range args { v = functionArgs(v) diff --git a/func_go110.go b/func_go110.go index 500880f..6df30d3 100644 --- a/func_go110.go +++ b/func_go110.go @@ -2,8 +2,15 @@ package xpath -import "math" +import ( + "math" + "strings" +) func round(f float64) int { return int(math.Round(f)) } + +func newStringBuilder() stringBuilder{ + return &strings.Builder{} +} diff --git a/func_pre_go110.go b/func_pre_go110.go index 043616b..335141f 100644 --- a/func_pre_go110.go +++ b/func_pre_go110.go @@ -2,7 +2,10 @@ package xpath -import "math" +import ( + "bytes" + "math" +) // math.Round() is supported by Go 1.10+, // This method just compatible for version <1.10. @@ -13,3 +16,7 @@ func round(f float64) int { } return int(f + math.Copysign(0.5, f)) } + +func newStringBuilder() stringBuilder { + return &bytes.Buffer{} +}