Skip to content

Commit 7d62514

Browse files
committed
all: start making more use of strings.Cut
Possibly the most underrated modern strings API. Signed-off-by: Daniel Martí <[email protected]> Change-Id: Ifb803704773b19cac700b56d8ed4fd16c5ed6a55 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1210874 TryBot-Result: CUEcueckoo <[email protected]> Unity-Result: CUE porcuepine <[email protected]> Reviewed-by: Roger Peppe <[email protected]>
1 parent cbed854 commit 7d62514

File tree

5 files changed

+16
-43
lines changed

5 files changed

+16
-43
lines changed

cmd/cue/cmd/get_go.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -1477,9 +1477,7 @@ func getName(name string, tag string) string {
14771477
tags := reflect.StructTag(tag)
14781478
for _, s := range []string{"json", "yaml"} {
14791479
if tag, ok := tags.Lookup(s); ok {
1480-
if p := strings.Index(tag, ","); p >= 0 {
1481-
tag = tag[:p]
1482-
}
1480+
tag, _, _ = strings.Cut(tag, ",")
14831481
if tag != "" {
14841482
return tag
14851483
}

cue/decode.go

+3-15
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ func typeFields(t reflect.Type) structFields {
559559
if tag == "-" {
560560
continue
561561
}
562-
name, opts := parseTag(tag)
562+
name, opts, _ := strings.Cut(tag, ",")
563563
if !isValidTag(name) {
564564
name = ""
565565
}
@@ -584,7 +584,7 @@ func typeFields(t reflect.Type) structFields {
584584
tag: tagged,
585585
index: index,
586586
typ: ft,
587-
omitEmpty: opts.Contains("omitempty"),
587+
omitEmpty: tagOptions(opts).Contains("omitempty"),
588588
}
589589
field.nameBytes = []byte(field.name)
590590
field.equalFold = foldFunc(field.nameBytes)
@@ -698,15 +698,6 @@ func cachedTypeFields(t reflect.Type) structFields {
698698
// tag, or the empty string. It does not include the leading comma.
699699
type tagOptions string
700700

701-
// parseTag splits a struct field's json tag into its name and
702-
// comma-separated options.
703-
func parseTag(tag string) (string, tagOptions) {
704-
if idx := strings.Index(tag, ","); idx != -1 {
705-
return tag[:idx], tagOptions(tag[idx+1:])
706-
}
707-
return tag, tagOptions("")
708-
}
709-
710701
// Contains reports whether a comma-separated list of options
711702
// contains a particular substr flag. substr must be surrounded by a
712703
// string boundary or commas.
@@ -717,10 +708,7 @@ func (o tagOptions) Contains(optionName string) bool {
717708
s := string(o)
718709
for s != "" {
719710
var next string
720-
i := strings.Index(s, ",")
721-
if i >= 0 {
722-
s, next = s[:i], s[i+1:]
723-
}
711+
s, next, _ = strings.Cut(s, ",")
724712
if s == optionName {
725713
return true
726714
}

cue/load/tags.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -291,19 +291,19 @@ func findTags(b *build.Instance) (tags []*tag, errs errors.Error) {
291291
func (tg *tagger) injectTags(tags []string) errors.Error {
292292
// Parses command line args
293293
for _, s := range tags {
294-
p := strings.Index(s, "=")
294+
name, val, ok := strings.Cut(s, "=")
295295
found := tg.usedTags[s]
296-
if p > 0 { // key-value
296+
if ok { // key-value
297297
for _, t := range tg.tags {
298-
if t.key == s[:p] {
298+
if t.key == name {
299299
found = true
300-
if err := t.inject(s[p+1:], tg); err != nil {
300+
if err := t.inject(val, tg); err != nil {
301301
return err
302302
}
303303
}
304304
}
305305
if !found {
306-
return errors.Newf(token.NoPos, "no tag for %q", s[:p])
306+
return errors.Newf(token.NoPos, "no tag for %q", name)
307307
}
308308
} else { // shorthand
309309
for _, t := range tg.tags {

mod/module/module.go

+2-9
Original file line numberDiff line numberDiff line change
@@ -254,15 +254,8 @@ func Sort(list []Version) {
254254
// To help go.sum formatting, allow version/file.
255255
// Compare semver prefix by semver rules,
256256
// file by string order.
257-
va := a.version
258-
vb := b.version
259-
var fa, fb string
260-
if k := strings.Index(va, "/"); k >= 0 {
261-
va, fa = va[:k], va[k:]
262-
}
263-
if k := strings.Index(vb, "/"); k >= 0 {
264-
vb, fb = vb[:k], vb[k:]
265-
}
257+
va, fa, _ := strings.Cut(a.version, "/")
258+
vb, fb, _ := strings.Cut(b.version, "/")
266259
if c := semver.Compare(va, vb); c != 0 {
267260
return c
268261
}

mod/module/path.go

+5-11
Original file line numberDiff line numberDiff line change
@@ -114,20 +114,17 @@ func CheckPathWithoutVersion(basePath string) (err error) {
114114
if err := checkPath(basePath, modulePath); err != nil {
115115
return err
116116
}
117-
i := strings.Index(basePath, "/")
118-
if i < 0 {
119-
i = len(basePath)
120-
}
121-
if i == 0 {
117+
firstPath, _, _ := strings.Cut(basePath, "/")
118+
if firstPath == "" {
122119
return fmt.Errorf("leading slash")
123120
}
124-
if !strings.Contains(basePath[:i], ".") {
121+
if !strings.Contains(firstPath, ".") {
125122
return fmt.Errorf("missing dot in first path element")
126123
}
127124
if basePath[0] == '-' {
128125
return fmt.Errorf("leading dash in first path element")
129126
}
130-
for _, r := range basePath[:i] {
127+
for _, r := range firstPath {
131128
if !firstPathOK(r) {
132129
return fmt.Errorf("invalid char %q in first path element", r)
133130
}
@@ -297,10 +294,7 @@ func checkElem(elem string, kind pathKind) error {
297294
}
298295
// Windows disallows a bunch of path elements, sadly.
299296
// See https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file
300-
short := elem
301-
if i := strings.Index(short, "."); i >= 0 {
302-
short = short[:i]
303-
}
297+
short, _, _ := strings.Cut(elem, ".")
304298
for _, bad := range badWindowsNames {
305299
if strings.EqualFold(bad, short) {
306300
return fmt.Errorf("%q disallowed as path element component on Windows", short)

0 commit comments

Comments
 (0)