Skip to content

Commit 72ba066

Browse files
committed
all: minor wins with strings.Cut and utf8.AppendRune
Both of these APIs were added in Go 1.18. Note that utf8.AppendRune already has a fast path for ASCII. Signed-off-by: Daniel Martí <[email protected]> Change-Id: Icab92b3f194e8395eaafaeb3a35398e3258ced11 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/557324 TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Paul Jolly <[email protected]> Unity-Result: CUE porcuepine <[email protected]>
1 parent d4d109e commit 72ba066

File tree

5 files changed

+6
-15
lines changed

5 files changed

+6
-15
lines changed

cmd/cue/cmd/common.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func getLang() language.Tag {
7777
if loc == "" {
7878
loc = os.Getenv("LANG")
7979
}
80-
loc = strings.Split(loc, ".")[0]
80+
loc, _, _ = strings.Cut(loc, ".")
8181
return language.Make(loc)
8282
}
8383

cue/literal/quote.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,6 @@ func (f Form) appendEscaped(buf []byte, s string) []byte {
223223
}
224224

225225
func (f *Form) appendEscapedRune(buf []byte, r rune) []byte {
226-
var runeTmp [utf8.UTFMax]byte
227226
if (!f.multiline && r == rune(f.quote)) || r == '\\' { // always backslashed
228227
buf = f.appendEscape(buf)
229228
buf = append(buf, byte(r))
@@ -235,8 +234,7 @@ func (f *Form) appendEscapedRune(buf []byte, r rune) []byte {
235234
return buf
236235
}
237236
} else if strconv.IsPrint(r) || f.graphicOnly && isInGraphicList(r) {
238-
n := utf8.EncodeRune(runeTmp[:], r)
239-
buf = append(buf, runeTmp[:n]...)
237+
buf = utf8.AppendRune(buf, r)
240238
return buf
241239
}
242240
buf = f.appendEscape(buf)

cue/literal/string.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ func (q QuoteInfo) Unquote(s string) (string, error) {
154154
}
155155
}
156156

157-
var runeTmp [utf8.UTFMax]byte
158157
buf := make([]byte, 0, 3*len(s)/2) // Try to avoid more allocations.
159158
stripNL := false
160159
wasEscapedNewline := false
@@ -220,11 +219,10 @@ func (q QuoteInfo) Unquote(s string) (string, error) {
220219
}
221220
stripNL = false
222221
wasEscapedNewline = false
223-
if c < utf8.RuneSelf || !multibyte {
222+
if !multibyte {
224223
buf = append(buf, byte(c))
225224
} else {
226-
n := utf8.EncodeRune(runeTmp[:], c)
227-
buf = append(buf, runeTmp[:n]...)
225+
buf = utf8.AppendRune(buf, c)
228226
}
229227
}
230228
// allow unmatched quotes if already checked.

internal/value/value.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ func Make(ctx *adt.OpContext, v adt.Value) cue.Value {
5151

5252
// UnifyBuiltin returns the given Value unified with the given builtin template.
5353
func UnifyBuiltin(v cue.Value, kind string) cue.Value {
54-
p := strings.Split(kind, ".")
55-
pkg, name := p[0], p[1]
54+
pkg, name, _ := strings.Cut(kind, ".")
5655
s := runtime.SharedRuntime.LoadImport(pkg)
5756
if s == nil {
5857
return v

pkg/tool/os/env.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,7 @@ func (c *environCmd) Run(ctx *task.Context) (res interface{}, err error) {
9898
update := map[string]interface{}{}
9999

100100
for _, kv := range os.Environ() {
101-
a := strings.SplitN(kv, "=", 2)
102-
103-
name := a[0]
104-
str := a[1]
105-
101+
name, str, _ := strings.Cut(kv, "=")
106102
if v := ctx.Obj.Lookup(name); v.Exists() {
107103
update[name], err = fromString(name, str, v)
108104
if err != nil {

0 commit comments

Comments
 (0)