aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosh Bleecher Snyder <josharian@gmail.com>2019-03-01 14:24:35 -0800
committeralandonovan <adonovan@google.com>2019-03-01 17:24:35 -0500
commit8cb25c8162478e5f38d1e77f93d90e8db4cc6259 (patch)
tree4b35a94382a7f2e03c9d348104f932cf2bcacb11
parent1174b2613e8224c5fe76d0fc8eb8de4a1384a89f (diff)
downloadstarlark-go-8cb25c8162478e5f38d1e77f93d90e8db4cc6259.tar.gz
all: use strings.Builder (#163)
Fixes #95
-rw-r--r--starlark/eval.go37
-rw-r--r--starlark/library.go22
-rw-r--r--starlark/value.go7
-rw-r--r--starlarkstruct/struct.go4
-rw-r--r--starlarktest/starlarktest.go6
-rw-r--r--syntax/quote.go5
-rw-r--r--syntax/scan.go3
7 files changed, 40 insertions, 44 deletions
diff --git a/starlark/eval.go b/starlark/eval.go
index 8268270..11ebbe1 100644
--- a/starlark/eval.go
+++ b/starlark/eval.go
@@ -5,7 +5,6 @@
package starlark
import (
- "bytes"
"fmt"
"io"
"io/ioutil"
@@ -88,14 +87,14 @@ func (d StringDict) Keys() []string {
}
func (d StringDict) String() string {
- var buf bytes.Buffer
+ buf := new(strings.Builder)
buf.WriteByte('{')
sep := ""
for _, name := range d.Keys() {
buf.WriteString(sep)
buf.WriteString(name)
buf.WriteString(": ")
- writeValue(&buf, d[name], nil)
+ writeValue(buf, d[name], nil)
sep = ", "
}
buf.WriteByte('}')
@@ -175,14 +174,14 @@ func (e *EvalError) Error() string { return e.Msg }
// Backtrace returns a user-friendly error message describing the stack
// of calls that led to this error.
func (e *EvalError) Backtrace() string {
- var buf bytes.Buffer
- e.Frame.WriteBacktrace(&buf)
- fmt.Fprintf(&buf, "Error: %s", e.Msg)
+ buf := new(strings.Builder)
+ e.Frame.WriteBacktrace(buf)
+ fmt.Fprintf(buf, "Error: %s", e.Msg)
return buf.String()
}
// WriteBacktrace writes a user-friendly description of the stack to buf.
-func (fr *Frame) WriteBacktrace(out *bytes.Buffer) {
+func (fr *Frame) WriteBacktrace(out *strings.Builder) {
fmt.Fprintf(out, "Traceback (most recent call last):\n")
var print func(fr *Frame)
print = func(fr *Frame) {
@@ -1302,7 +1301,7 @@ func (is *intset) len() int {
// https://github.com/google/starlark-go/blob/master/doc/spec.md#string-interpolation
func interpolate(format string, x Value) (Value, error) {
- var buf bytes.Buffer
+ buf := new(strings.Builder)
index := 0
nargs := 1
if tuple, ok := x.(Tuple); ok {
@@ -1367,7 +1366,7 @@ func interpolate(format string, x Value) (Value, error) {
if str, ok := AsString(arg); ok && c == 's' {
buf.WriteString(str)
} else {
- writeValue(&buf, arg, nil)
+ writeValue(buf, arg, nil)
}
case 'd', 'i', 'o', 'x', 'X':
i, err := NumberToInt(arg)
@@ -1376,13 +1375,13 @@ func interpolate(format string, x Value) (Value, error) {
}
switch c {
case 'd', 'i':
- fmt.Fprintf(&buf, "%d", i)
+ fmt.Fprintf(buf, "%d", i)
case 'o':
- fmt.Fprintf(&buf, "%o", i)
+ fmt.Fprintf(buf, "%o", i)
case 'x':
- fmt.Fprintf(&buf, "%x", i)
+ fmt.Fprintf(buf, "%x", i)
case 'X':
- fmt.Fprintf(&buf, "%X", i)
+ fmt.Fprintf(buf, "%X", i)
}
case 'e', 'f', 'g', 'E', 'F', 'G':
f, ok := AsFloat(arg)
@@ -1391,17 +1390,17 @@ func interpolate(format string, x Value) (Value, error) {
}
switch c {
case 'e':
- fmt.Fprintf(&buf, "%e", f)
+ fmt.Fprintf(buf, "%e", f)
case 'f':
- fmt.Fprintf(&buf, "%f", f)
+ fmt.Fprintf(buf, "%f", f)
case 'g':
- fmt.Fprintf(&buf, "%g", f)
+ fmt.Fprintf(buf, "%g", f)
case 'E':
- fmt.Fprintf(&buf, "%E", f)
+ fmt.Fprintf(buf, "%E", f)
case 'F':
- fmt.Fprintf(&buf, "%F", f)
+ fmt.Fprintf(buf, "%F", f)
case 'G':
- fmt.Fprintf(&buf, "%G", f)
+ fmt.Fprintf(buf, "%G", f)
}
case 'c':
switch arg := arg.(type) {
diff --git a/starlark/library.go b/starlark/library.go
index 5b7faad..7f5444d 100644
--- a/starlark/library.go
+++ b/starlark/library.go
@@ -10,7 +10,6 @@ package starlark
// mutable types such as lists and dicts.
import (
- "bytes"
"fmt"
"log"
"math/big"
@@ -837,7 +836,7 @@ func print(thread *Thread, fn *Builtin, args Tuple, kwargs []Tuple) (Value, erro
if err := UnpackArgs("print", nil, kwargs, "sep?", &sep); err != nil {
return nil, err
}
- var buf bytes.Buffer
+ buf := new(strings.Builder)
for i, v := range args {
if i > 0 {
buf.WriteString(sep)
@@ -845,14 +844,15 @@ func print(thread *Thread, fn *Builtin, args Tuple, kwargs []Tuple) (Value, erro
if s, ok := AsString(v); ok {
buf.WriteString(s)
} else {
- writeValue(&buf, v, nil)
+ writeValue(buf, v, nil)
}
}
+ s := buf.String()
if thread.Print != nil {
- thread.Print(thread, buf.String())
+ thread.Print(thread, s)
} else {
- fmt.Fprintln(os.Stderr, &buf)
+ fmt.Fprintln(os.Stderr, s)
}
return None, nil
}
@@ -1474,7 +1474,7 @@ func string_capitalize(fnname string, recv Value, args Tuple, kwargs []Tuple) (V
return nil, err
}
s := string(recv.(String))
- var res bytes.Buffer
+ res := new(strings.Builder)
res.Grow(len(s))
for i, r := range s {
if i == 0 {
@@ -1657,7 +1657,7 @@ func string_find(fnname string, recv Value, args Tuple, kwargs []Tuple) (Value,
func string_format(fnname string, recv_ Value, args Tuple, kwargs []Tuple) (Value, error) {
format := string(recv_.(String))
var auto, manual bool // kinds of positional indexing used
- var buf bytes.Buffer
+ buf := new(strings.Builder)
index := 0
for {
literal := format
@@ -1782,10 +1782,10 @@ func string_format(fnname string, recv_ Value, args Tuple, kwargs []Tuple) (Valu
if str, ok := AsString(arg); ok {
buf.WriteString(str)
} else {
- writeValue(&buf, arg, nil)
+ writeValue(buf, arg, nil)
}
case "r":
- writeValue(&buf, arg, nil)
+ writeValue(buf, arg, nil)
default:
return nil, fmt.Errorf("unknown conversion %q", conv)
}
@@ -1823,7 +1823,7 @@ func string_join(fnname string, recv_ Value, args Tuple, kwargs []Tuple) (Value,
}
iter := iterable.Iterate()
defer iter.Done()
- var buf bytes.Buffer
+ buf := new(strings.Builder)
var x Value
for i := 0; iter.Next(&x); i++ {
if i > 0 {
@@ -1984,7 +1984,7 @@ func string_title(fnname string, recv Value, args Tuple, kwargs []Tuple) (Value,
// Python semantics differ from x==strings.{To,}Title(x) in Go:
// "uppercase characters may only follow uncased characters and
// lowercase characters only cased ones."
- var buf bytes.Buffer
+ buf := new(strings.Builder)
buf.Grow(len(s))
var prevCased bool
for _, r := range s {
diff --git a/starlark/value.go b/starlark/value.go
index f697442..116c8e9 100644
--- a/starlark/value.go
+++ b/starlark/value.go
@@ -67,7 +67,6 @@ package starlark // import "go.starlark.net/starlark"
// This file defines the data types of Starlark and their basic operations.
import (
- "bytes"
"fmt"
"math"
"math/big"
@@ -980,8 +979,8 @@ func (s *Set) Union(iter Iterator) (Value, error) {
// toString returns the string form of value v.
// It may be more efficient than v.String() for larger values.
func toString(v Value) string {
- var buf bytes.Buffer
- writeValue(&buf, v, nil)
+ buf := new(strings.Builder)
+ writeValue(buf, v, nil)
return buf.String()
}
@@ -992,7 +991,7 @@ func toString(v Value) string {
// (These are the only potentially cyclic structures.)
// Callers should generally pass a temporary slice of length zero but non-zero capacity.
// It is safe to re-use the same path slice for multiple calls.
-func writeValue(out *bytes.Buffer, x Value, path []Value) {
+func writeValue(out *strings.Builder, x Value, path []Value) {
switch x := x.(type) {
case nil:
out.WriteString("<nil>") // indicates a bug
diff --git a/starlarkstruct/struct.go b/starlarkstruct/struct.go
index ac9c955..1982cc0 100644
--- a/starlarkstruct/struct.go
+++ b/starlarkstruct/struct.go
@@ -23,9 +23,9 @@ package starlarkstruct // import "go.starlark.net/starlarkstruct"
// would be, and, like the Go struct, requires only a single allocation.
import (
- "bytes"
"fmt"
"sort"
+ "strings"
"go.starlark.net/starlark"
"go.starlark.net/syntax"
@@ -131,7 +131,7 @@ func (s *Struct) ToStringDict(d starlark.StringDict) {
}
func (s *Struct) String() string {
- var buf bytes.Buffer
+ buf := new(strings.Builder)
if s.constructor == Default {
// NB: The Java implementation always prints struct
// even for Bazel provider instances.
diff --git a/starlarktest/starlarktest.go b/starlarktest/starlarktest.go
index 5436d22..c93eaf9 100644
--- a/starlarktest/starlarktest.go
+++ b/starlarktest/starlarktest.go
@@ -13,11 +13,11 @@
package starlarktest // import "go.starlark.net/starlarktest"
import (
- "bytes"
"fmt"
"go/build"
"path/filepath"
"regexp"
+ "strings"
"sync"
"go.starlark.net/starlark"
@@ -104,8 +104,8 @@ func error_(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, k
if len(args) != 1 {
return nil, fmt.Errorf("error: got %d arguments, want 1", len(args))
}
- var buf bytes.Buffer
- thread.Caller().WriteBacktrace(&buf)
+ buf := new(strings.Builder)
+ thread.Caller().WriteBacktrace(buf)
buf.WriteString("Error: ")
if s, ok := starlark.AsString(args[0]); ok {
buf.WriteString(s)
diff --git a/syntax/quote.go b/syntax/quote.go
index d31eb2a..cc9a8d0 100644
--- a/syntax/quote.go
+++ b/syntax/quote.go
@@ -7,7 +7,6 @@ package syntax
// Starlark quoted string utilities.
import (
- "bytes"
"fmt"
"strconv"
"strings"
@@ -95,7 +94,7 @@ func unquote(quoted string) (s string, triple bool, err error) {
// Otherwise process quoted string.
// Each iteration processes one escape sequence along with the
// plain text leading up to it.
- var buf bytes.Buffer
+ buf := new(strings.Builder)
for {
// Remove prefix before escape sequence.
i := strings.IndexAny(quoted, unquoteChars)
@@ -204,7 +203,7 @@ func quote(unquoted string, triple bool) string {
q = `"""`
}
- var buf bytes.Buffer
+ buf := new(strings.Builder)
buf.WriteString(q)
for i := 0; i < len(unquoted); i++ {
diff --git a/syntax/scan.go b/syntax/scan.go
index 7fbc5d6..5b3fcbc 100644
--- a/syntax/scan.go
+++ b/syntax/scan.go
@@ -7,7 +7,6 @@ package syntax
// A lexical scanner for Starlark.
import (
- "bytes"
"fmt"
"io"
"io/ioutil"
@@ -832,7 +831,7 @@ func (sc *scanner) scanString(val *tokenValue, quote rune) Token {
// A triple-quoted string literal may span multiple
// gulps of REPL input; it is the only such token.
// Thus we must avoid {start,end}Token.
- var raw bytes.Buffer
+ raw := new(strings.Builder)
// Copy the prefix, e.g. r''' or """ (see startToken).
raw.Write(sc.token[:len(sc.token)-len(sc.rest)])