Skip to content

Commit

Permalink
all: minor wins with strings.Cut and utf8.AppendRune
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
mvdan committed Aug 17, 2023
1 parent d4d109e commit 72ba066
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 15 deletions.
2 changes: 1 addition & 1 deletion cmd/cue/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func getLang() language.Tag {
if loc == "" {
loc = os.Getenv("LANG")
}
loc = strings.Split(loc, ".")[0]
loc, _, _ = strings.Cut(loc, ".")
return language.Make(loc)
}

Expand Down
4 changes: 1 addition & 3 deletions cue/literal/quote.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ func (f Form) appendEscaped(buf []byte, s string) []byte {
}

func (f *Form) appendEscapedRune(buf []byte, r rune) []byte {
var runeTmp [utf8.UTFMax]byte
if (!f.multiline && r == rune(f.quote)) || r == '\\' { // always backslashed
buf = f.appendEscape(buf)
buf = append(buf, byte(r))
Expand All @@ -235,8 +234,7 @@ func (f *Form) appendEscapedRune(buf []byte, r rune) []byte {
return buf
}
} else if strconv.IsPrint(r) || f.graphicOnly && isInGraphicList(r) {
n := utf8.EncodeRune(runeTmp[:], r)
buf = append(buf, runeTmp[:n]...)
buf = utf8.AppendRune(buf, r)
return buf
}
buf = f.appendEscape(buf)
Expand Down
6 changes: 2 additions & 4 deletions cue/literal/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ func (q QuoteInfo) Unquote(s string) (string, error) {
}
}

var runeTmp [utf8.UTFMax]byte
buf := make([]byte, 0, 3*len(s)/2) // Try to avoid more allocations.
stripNL := false
wasEscapedNewline := false
Expand Down Expand Up @@ -220,11 +219,10 @@ func (q QuoteInfo) Unquote(s string) (string, error) {
}
stripNL = false
wasEscapedNewline = false
if c < utf8.RuneSelf || !multibyte {
if !multibyte {
buf = append(buf, byte(c))
} else {
n := utf8.EncodeRune(runeTmp[:], c)
buf = append(buf, runeTmp[:n]...)
buf = utf8.AppendRune(buf, c)
}
}
// allow unmatched quotes if already checked.
Expand Down
3 changes: 1 addition & 2 deletions internal/value/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ func Make(ctx *adt.OpContext, v adt.Value) cue.Value {

// UnifyBuiltin returns the given Value unified with the given builtin template.
func UnifyBuiltin(v cue.Value, kind string) cue.Value {
p := strings.Split(kind, ".")
pkg, name := p[0], p[1]
pkg, name, _ := strings.Cut(kind, ".")
s := runtime.SharedRuntime.LoadImport(pkg)
if s == nil {
return v
Expand Down
6 changes: 1 addition & 5 deletions pkg/tool/os/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,7 @@ func (c *environCmd) Run(ctx *task.Context) (res interface{}, err error) {
update := map[string]interface{}{}

for _, kv := range os.Environ() {
a := strings.SplitN(kv, "=", 2)

name := a[0]
str := a[1]

name, str, _ := strings.Cut(kv, "=")
if v := ctx.Obj.Lookup(name); v.Exists() {
update[name], err = fromString(name, str, v)
if err != nil {
Expand Down

0 comments on commit 72ba066

Please sign in to comment.