refactor(erigon-lib/metrics): replace strings.IndexByte with strings.Cut (#9202)

above go1.18  Index calls are more clearly written using Cut.
https://github.com/golang/go/issues/46336
This commit is contained in:
ddl 2024-01-12 21:07:49 +08:00 committed by GitHub
parent fa1e1bab27
commit db1dcbb9d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -12,25 +12,24 @@ func parseMetric(s string) (string, prometheus.Labels, error) {
if len(s) == 0 {
return "", nil, fmt.Errorf("metric cannot be empty")
}
n := strings.IndexByte(s, '{')
if n < 0 {
ident, rest, ok := strings.Cut(s, "{")
if !ok {
if err := validateIdent(s); err != nil {
return "", nil, err
}
return s, nil, nil
}
ident := s[:n]
s = s[n+1:]
if err := validateIdent(ident); err != nil {
return "", nil, err
}
if len(s) == 0 || s[len(s)-1] != '}' {
if len(rest) == 0 || rest[len(rest)-1] != '}' {
return "", nil, fmt.Errorf("missing closing curly brace at the end of %q", ident)
}
tags, err := parseTags(s[:len(s)-1])
tags, err := parseTags(rest[:len(rest)-1])
if err != nil {
return "", nil, err
}