Skip to content

Commit

Permalink
Replace strings.SplitN usages with strings.Cut
Browse files Browse the repository at this point in the history
  • Loading branch information
WilczynskiT committed Aug 4, 2022
1 parent a153945 commit 37e387c
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 24 deletions.
6 changes: 3 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,11 +387,11 @@ func parseEnvFile(envInput io.Reader) (map[string]string, error) {
}

// split line into key and value
fields := strings.SplitN(line, "=", 2)
if len(fields) != 2 {
before, after, isCut := strings.Cut(line, "=")
if !isCut {
return nil, fmt.Errorf("can't parse line %d; line should be in KEY=VALUE format", lineNumber)
}
key, val := fields[0], fields[1]
key, val := before, after

// sometimes keys are prefixed by "export " so file can be sourced in bash; ignore it here
key = strings.TrimPrefix(key, "export ")
Expand Down
10 changes: 5 additions & 5 deletions listeners.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,15 +468,15 @@ func ParseNetworkAddress(addr string) (NetworkAddress, error) {
}
var start, end uint64
if port != "" {
ports := strings.SplitN(port, "-", 2)
if len(ports) == 1 {
ports = append(ports, ports[0])
before, after, found := strings.Cut(port, "-")
if !found {
after = before
}
start, err = strconv.ParseUint(ports[0], 10, 16)
start, err = strconv.ParseUint(before, 10, 16)
if err != nil {
return NetworkAddress{}, fmt.Errorf("invalid start port: %v", err)
}
end, err = strconv.ParseUint(ports[1], 10, 16)
end, err = strconv.ParseUint(after, 10, 16)
if err != nil {
return NetworkAddress{}, fmt.Errorf("invalid end port: %v", err)
}
Expand Down
6 changes: 3 additions & 3 deletions modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,11 @@ func ParseStructTag(tag string) (map[string]string, error) {
if pair == "" {
continue
}
parts := strings.SplitN(pair, "=", 2)
if len(parts) != 2 {
before, after, isCut := strings.Cut(pair, "=")
if !isCut {
return nil, fmt.Errorf("missing key in '%s' (pair %d)", pair, i)
}
results[parts[0]] = parts[1]
results[before] = after
}
return results, nil
}
Expand Down
6 changes: 3 additions & 3 deletions modules/caddyhttp/matchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -610,11 +610,11 @@ func (m *MatchQuery) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
if query == "" {
continue
}
parts := strings.SplitN(query, "=", 2)
if len(parts) != 2 {
before, after, found := strings.Cut(query, "=")
if !found {
return d.Errf("malformed query matcher token: %s; must be in param=val format", d.Val())
}
url.Values(*m).Add(parts[0], parts[1])
url.Values(*m).Add(before, after)
}
if d.NextBlock(0) {
return d.Err("malformed query matcher: blocks are not supported")
Expand Down
11 changes: 5 additions & 6 deletions modules/caddyhttp/push/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,16 @@ func parseLinkHeader(header string) []linkResource {
l.uri = strings.TrimSpace(link[li+1 : ri])

for _, param := range strings.Split(strings.TrimSpace(link[ri+1:]), semicolon) {
parts := strings.SplitN(strings.TrimSpace(param), equal, 2)
key := strings.TrimSpace(parts[0])
before, after, isCut := strings.Cut(strings.TrimSpace(param), equal)
key := strings.TrimSpace(before)
if key == "" {
continue
}
if len(parts) == 1 {
if isCut {
l.params[key] = strings.TrimSpace(after)
} else {
l.params[key] = key
}
if len(parts) == 2 {
l.params[key] = strings.TrimSpace(parts[1])
}
}

resources = append(resources, l)
Expand Down
8 changes: 4 additions & 4 deletions modules/caddyhttp/reverseproxy/fastcgi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,13 +424,13 @@ func (c *FCGIClient) Request(p map[string]string, req io.Reader) (resp *http.Res
resp.Header = http.Header(mimeHeader)

if resp.Header.Get("Status") != "" {
statusParts := strings.SplitN(resp.Header.Get("Status"), " ", 2)
resp.StatusCode, err = strconv.Atoi(statusParts[0])
statusNumber, statusInfo, statusIsCut := strings.Cut(resp.Header.Get("Status"), " ")
resp.StatusCode, err = strconv.Atoi(statusNumber)
if err != nil {
return
}
if len(statusParts) > 1 {
resp.Status = statusParts[1]
if statusIsCut {
resp.Status = statusInfo
}

} else {
Expand Down

0 comments on commit 37e387c

Please sign in to comment.