Skip to content

Commit 9de339f

Browse files
committed
refactor color parsing and environ loader using strings.Cut
1 parent b144489 commit 9de339f

File tree

3 files changed

+9
-22
lines changed

3 files changed

+9
-22
lines changed

cli/color.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,32 +41,21 @@ func validColor(x string) bool {
4141
return false
4242
}
4343
}
44-
return num || x == ""
44+
return num
4545
}
4646

4747
func setColors(colors string) error {
48-
var i int
4948
var color string
5049
for _, target := range []*[]byte{
5150
&nullColor, &falseColor, &trueColor, &numberColor,
5251
&stringColor, &objectKeyColor, &arrayColor, &objectColor,
5352
} {
54-
if i < len(colors) {
55-
if j := strings.IndexByte(colors[i:], ':'); j >= 0 {
56-
color = colors[i : i+j]
57-
i += j + 1
58-
} else {
59-
color = colors[i:]
60-
i = len(colors)
61-
}
53+
color, colors, _ = strings.Cut(colors, ":")
54+
if color != "" {
6255
if !validColor(color) {
6356
return fmt.Errorf("invalid color: %q", color)
6457
}
65-
if color == "" {
66-
*target = nil
67-
} else {
68-
*target = newColor(color)
69-
}
58+
*target = newColor(color)
7059
} else {
7160
*target = nil
7261
}

cli/error.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,11 @@ func (err *yamlParseError) Error() string {
125125
msg := strings.TrimPrefix(
126126
strings.TrimPrefix(err.err.Error(), "yaml: "),
127127
"unmarshal errors:\n ")
128-
if fmt.Sscanf(msg, "line %d: ", &line); line == 0 {
128+
if _, e := fmt.Sscanf(msg, "line %d: ", &line); e != nil {
129129
return "invalid yaml: " + err.fname
130130
}
131-
msg = msg[strings.Index(msg, ": ")+2:]
132-
if i := strings.IndexByte(msg, '\n'); i >= 0 {
133-
msg = msg[:i]
134-
}
131+
_, msg, _ = strings.Cut(msg, ": ")
132+
msg, _, _ = strings.Cut(msg, "\n")
135133
linestr := getLineByLine(err.contents, line)
136134
return fmt.Sprintf("invalid yaml: %s:%d\n%s %s",
137135
err.fname, line, formatLineInfo(linestr, line, 0), msg)

compiler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -913,8 +913,8 @@ func (c *compiler) compileFunc(e *Func) error {
913913
env := make(map[string]any)
914914
if c.environLoader != nil {
915915
for _, kv := range c.environLoader() {
916-
if i := strings.IndexByte(kv, '='); i > 0 {
917-
env[kv[:i]] = kv[i+1:]
916+
if k, v, ok := strings.Cut(kv, "="); ok && k != "" {
917+
env[k] = v
918918
}
919919
}
920920
}

0 commit comments

Comments
 (0)