diff --git a/config/options.go b/config/options.go index bdf6780c22a..d7ec87ca0ee 100644 --- a/config/options.go +++ b/config/options.go @@ -137,12 +137,12 @@ func resolver(input map[string]any, mapper func(name string) string, toType bool func mapper(input map[string]any) func(name string) string { mapper := func(name string) string { - args := strings.SplitN(strings.TrimSpace(name), ":", 2) //nolint:mnd - if v, has := readValue(input, args[0]); has { + key, value, cut := strings.Cut(strings.TrimSpace(name), ":") + if v, has := readValue(input, key); has { s, _ := v.String() return s - } else if len(args) > 1 { // default value - return args[1] + } else if cut { // default value + return value } return "" } diff --git a/contrib/config/apollo/apollo.go b/contrib/config/apollo/apollo.go index 8e15b9011cc..2513c313ae2 100644 --- a/contrib/config/apollo/apollo.go +++ b/contrib/config/apollo/apollo.go @@ -130,9 +130,12 @@ func NewSource(opts ...Option) config.Source { } func format(ns string) string { - arr := strings.Split(ns, ".") - suffix := arr[len(arr)-1] - if len(arr) <= 1 || suffix == properties { + lastDot := strings.LastIndexByte(ns, '.') + if lastDot == -1 || lastDot == len(ns)-1 { + return json + } + suffix := ns[lastDot+1:] + if suffix == properties { return json } if _, ok := formats[suffix]; !ok { @@ -258,19 +261,19 @@ func resolve(key string, value any, target map[string]any) { // genKey got the key of config.KeyValue pair. // eg: namespace.ext with subKey got namespace.subKey func genKey(ns, sub string) string { - arr := strings.Split(ns, ".") - if len(arr) == 1 { - if ns == "" { - return sub - } + if ns == "" { + return sub + } + lastDot := strings.LastIndexByte(ns, '.') + if lastDot == -1 || lastDot == len(ns)-1 { return ns + "." + sub } - suffix := arr[len(arr)-1] + suffix := ns[lastDot+1:] _, ok := formats[suffix] if ok { - return strings.Join(arr[:len(arr)-1], ".") + "." + sub + return ns[:lastDot] + "." + sub } return ns + "." + sub diff --git a/contrib/registry/consul/client.go b/contrib/registry/consul/client.go index 471db8b0f10..00ff52f8fa7 100644 --- a/contrib/registry/consul/client.go +++ b/contrib/registry/consul/client.go @@ -59,9 +59,9 @@ func defaultResolver(_ context.Context, entries []*api.ServiceEntry) []*registry for _, entry := range entries { var version string for _, tag := range entry.Service.Tags { - ss := strings.SplitN(tag, "=", 2) - if len(ss) == 2 && ss[0] == "version" { - version = ss[1] + k, v, cut := strings.Cut(tag, "=") + if cut && k == "version" { + version = v } } endpoints := make([]string, 0) diff --git a/contrib/registry/kubernetes/registry.go b/contrib/registry/kubernetes/registry.go index dc620d9abe6..f6170089c56 100644 --- a/contrib/registry/kubernetes/registry.go +++ b/contrib/registry/kubernetes/registry.go @@ -388,7 +388,7 @@ func getServiceInstanceFromPod(pod *corev1.Pod) (*registry.ServiceInstance, erro protocol := protocolMap.GetProtocol(port) if protocol == "" { if cp.Name != "" { - protocol = strings.Split(cp.Name, "-")[0] + protocol, _, _ = strings.Cut(cp.Name, "-") } else { protocol = string(cp.Protocol) } diff --git a/middleware/auth/jwt/jwt.go b/middleware/auth/jwt/jwt.go index d39ec802954..4a703d48856 100644 --- a/middleware/auth/jwt/jwt.go +++ b/middleware/auth/jwt/jwt.go @@ -89,11 +89,10 @@ func Server(keyFunc jwt.Keyfunc, opts ...Option) middleware.Middleware { if keyFunc == nil { return nil, ErrMissingKeyFunc } - auths := strings.SplitN(header.RequestHeader().Get(authorizationKey), " ", 2) - if len(auths) != 2 || !strings.EqualFold(auths[0], bearerWord) { + bearer, jwtToken, cut := strings.Cut(header.RequestHeader().Get(authorizationKey), " ") + if !cut || !strings.EqualFold(bearer, bearerWord) { return nil, ErrMissingJwtToken } - jwtToken := auths[1] var ( tokenInfo *jwt.Token err error diff --git a/middleware/tracing/span.go b/middleware/tracing/span.go index d25a8b38770..c6c4b30fa16 100644 --- a/middleware/tracing/span.go +++ b/middleware/tracing/span.go @@ -103,17 +103,17 @@ func setServerSpan(ctx context.Context, span trace.Span, m any) { // on a gRPC's FullMethod. func parseFullMethod(fullMethod string) (string, []attribute.KeyValue) { name := strings.TrimLeft(fullMethod, "/") - parts := strings.SplitN(name, "/", 2) - if len(parts) != 2 { //nolint:mnd + service, method, cut := strings.Cut(name, "/") + if !cut { // Invalid format, does not follow `/package.service/method`. return name, []attribute.KeyValue{attribute.Key("rpc.operation").String(fullMethod)} } var attrs []attribute.KeyValue - if service := parts[0]; service != "" { + if service != "" { attrs = append(attrs, semconv.RPCServiceKey.String(service)) } - if method := parts[1]; method != "" { + if method != "" { attrs = append(attrs, semconv.RPCMethodKey.String(method)) } return name, attrs