all: replace some cases of strings.SplitN with strings.Cut #28446 (#1007)

Co-authored-by: Håvard Anda Estensen <haavard.ae@gmail.com>
This commit is contained in:
Daniel Liu 2025-04-28 18:07:59 +08:00 committed by GitHub
parent 7422a13f24
commit 80abc58f25
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 16 additions and 18 deletions

View file

@ -421,9 +421,7 @@ func rpcNode(ctx *cli.Context) error {
} }
func rpcSubscribe(client *rpc.Client, out io.Writer, method string, args ...string) error { func rpcSubscribe(client *rpc.Client, out io.Writer, method string, args ...string) error {
parts := strings.SplitN(method, "_", 2) namespace, method, _ := strings.Cut(method, "_")
namespace := parts[0]
method = parts[1]
ch := make(chan interface{}) ch := make(chan interface{})
subArgs := make([]interface{}, len(args)+1) subArgs := make([]interface{}, len(args)+1)
subArgs[0] = method subArgs[0] = method

View file

@ -61,12 +61,12 @@ type Interface interface {
// "pmp:192.168.0.1" uses NAT-PMP with the given gateway address // "pmp:192.168.0.1" uses NAT-PMP with the given gateway address
func Parse(spec string) (Interface, error) { func Parse(spec string) (Interface, error) {
var ( var (
parts = strings.SplitN(spec, ":", 2) before, after, found = strings.Cut(spec, ":")
mech = strings.ToLower(parts[0]) mech = strings.ToLower(before)
ip net.IP ip net.IP
) )
if len(parts) > 1 { if found {
ip = net.ParseIP(parts[1]) ip = net.ParseIP(after)
if ip == nil { if ip == nil {
return nil, errors.New("invalid IP address") return nil, errors.New("invalid IP address")
} }
@ -86,7 +86,7 @@ func Parse(spec string) (Interface, error) {
case "pmp", "natpmp", "nat-pmp": case "pmp", "natpmp", "nat-pmp":
return PMP(ip), nil return PMP(ip), nil
default: default:
return nil, fmt.Errorf("unknown mechanism %q", parts[0]) return nil, fmt.Errorf("unknown mechanism %q", before)
} }
} }

View file

@ -482,12 +482,12 @@ func (s *Server) StreamNetworkEvents(w http.ResponseWriter, req *http.Request) {
func NewMsgFilters(filterParam string) (MsgFilters, error) { func NewMsgFilters(filterParam string) (MsgFilters, error) {
filters := make(MsgFilters) filters := make(MsgFilters)
for _, filter := range strings.Split(filterParam, "-") { for _, filter := range strings.Split(filterParam, "-") {
protoCodes := strings.SplitN(filter, ":", 2) proto, codes, found := strings.Cut(filter, ":")
if len(protoCodes) != 2 || protoCodes[0] == "" || protoCodes[1] == "" { if !found || proto == "" || codes == "" {
return nil, fmt.Errorf("invalid message filter: %s", filter) return nil, fmt.Errorf("invalid message filter: %s", filter)
} }
proto := protoCodes[0]
for _, code := range strings.Split(protoCodes[1], ",") { for _, code := range strings.Split(codes, ",") {
if code == "*" || code == "-1" { if code == "*" || code == "-1" {
filters[MsgFilter{Proto: proto, Code: -1}] = struct{}{} filters[MsgFilter{Proto: proto, Code: -1}] = struct{}{}
continue continue

View file

@ -86,8 +86,8 @@ func (msg *jsonrpcMessage) isUnsubscribe() bool {
} }
func (msg *jsonrpcMessage) namespace() string { func (msg *jsonrpcMessage) namespace() string {
elem := strings.SplitN(msg.Method, serviceMethodSeparator, 2) before, _, _ := strings.Cut(msg.Method, serviceMethodSeparator)
return elem[0] return before
} }
func (msg *jsonrpcMessage) String() string { func (msg *jsonrpcMessage) String() string {

View file

@ -93,13 +93,13 @@ func (r *serviceRegistry) registerName(name string, rcvr interface{}) error {
// callback returns the callback corresponding to the given RPC method name. // callback returns the callback corresponding to the given RPC method name.
func (r *serviceRegistry) callback(method string) *callback { func (r *serviceRegistry) callback(method string) *callback {
elem := strings.SplitN(method, serviceMethodSeparator, 2) before, after, found := strings.Cut(method, serviceMethodSeparator)
if len(elem) != 2 { if !found {
return nil return nil
} }
r.mu.Lock() r.mu.Lock()
defer r.mu.Unlock() defer r.mu.Unlock()
return r.services[elem[0]].callbacks[elem[1]] return r.services[before].callbacks[after]
} }
// subscription returns a subscription callback in the given service. // subscription returns a subscription callback in the given service.