Skip to content

Commit a75a2d6

Browse files
authored
all: replace some cases of strings.SplitN with strings.Cut (#28446)
1 parent db7895d commit a75a2d6

File tree

6 files changed

+20
-22
lines changed

6 files changed

+20
-22
lines changed

accounts/scwallet/wallet.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -776,16 +776,16 @@ func (w *Wallet) findAccountPath(account accounts.Account) (accounts.DerivationP
776776
return nil, fmt.Errorf("scheme %s does not match wallet scheme %s", account.URL.Scheme, w.Hub.scheme)
777777
}
778778

779-
parts := strings.SplitN(account.URL.Path, "/", 2)
780-
if len(parts) != 2 {
779+
url, path, found := strings.Cut(account.URL.Path, "/")
780+
if !found {
781781
return nil, fmt.Errorf("invalid URL format: %s", account.URL)
782782
}
783783

784-
if parts[0] != fmt.Sprintf("%x", w.PublicKey[1:3]) {
784+
if url != fmt.Sprintf("%x", w.PublicKey[1:3]) {
785785
return nil, fmt.Errorf("URL %s is not for this wallet", account.URL)
786786
}
787787

788-
return accounts.ParseDerivationPath(parts[1])
788+
return accounts.ParseDerivationPath(path)
789789
}
790790

791791
// Session represents a secured communication session with the wallet.

cmd/p2psim/main.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,9 +417,7 @@ func rpcNode(ctx *cli.Context) error {
417417
}
418418

419419
func rpcSubscribe(client *rpc.Client, out io.Writer, method string, args ...string) error {
420-
parts := strings.SplitN(method, "_", 2)
421-
namespace := parts[0]
422-
method = parts[1]
420+
namespace, method, _ := strings.Cut(method, "_")
423421
ch := make(chan interface{})
424422
subArgs := make([]interface{}, len(args)+1)
425423
subArgs[0] = method

p2p/nat/nat.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ type Interface interface {
6161
// "pmp:192.168.0.1" uses NAT-PMP with the given gateway address
6262
func Parse(spec string) (Interface, error) {
6363
var (
64-
parts = strings.SplitN(spec, ":", 2)
65-
mech = strings.ToLower(parts[0])
66-
ip net.IP
64+
before, after, found = strings.Cut(spec, ":")
65+
mech = strings.ToLower(before)
66+
ip net.IP
6767
)
68-
if len(parts) > 1 {
69-
ip = net.ParseIP(parts[1])
68+
if found {
69+
ip = net.ParseIP(after)
7070
if ip == nil {
7171
return nil, errors.New("invalid IP address")
7272
}
@@ -86,7 +86,7 @@ func Parse(spec string) (Interface, error) {
8686
case "pmp", "natpmp", "nat-pmp":
8787
return PMP(ip), nil
8888
default:
89-
return nil, fmt.Errorf("unknown mechanism %q", parts[0])
89+
return nil, fmt.Errorf("unknown mechanism %q", before)
9090
}
9191
}
9292

p2p/simulations/http.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -479,12 +479,12 @@ func (s *Server) StreamNetworkEvents(w http.ResponseWriter, req *http.Request) {
479479
func NewMsgFilters(filterParam string) (MsgFilters, error) {
480480
filters := make(MsgFilters)
481481
for _, filter := range strings.Split(filterParam, "-") {
482-
protoCodes := strings.SplitN(filter, ":", 2)
483-
if len(protoCodes) != 2 || protoCodes[0] == "" || protoCodes[1] == "" {
482+
proto, codes, found := strings.Cut(filter, ":")
483+
if !found || proto == "" || codes == "" {
484484
return nil, fmt.Errorf("invalid message filter: %s", filter)
485485
}
486-
proto := protoCodes[0]
487-
for _, code := range strings.Split(protoCodes[1], ",") {
486+
487+
for _, code := range strings.Split(codes, ",") {
488488
if code == "*" || code == "-1" {
489489
filters[MsgFilter{Proto: proto, Code: -1}] = struct{}{}
490490
continue

rpc/json.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ func (msg *jsonrpcMessage) isUnsubscribe() bool {
8686
}
8787

8888
func (msg *jsonrpcMessage) namespace() string {
89-
elem := strings.SplitN(msg.Method, serviceMethodSeparator, 2)
90-
return elem[0]
89+
before, _, _ := strings.Cut(msg.Method, serviceMethodSeparator)
90+
return before
9191
}
9292

9393
func (msg *jsonrpcMessage) String() string {

rpc/service.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,13 @@ func (r *serviceRegistry) registerName(name string, rcvr interface{}) error {
9393

9494
// callback returns the callback corresponding to the given RPC method name.
9595
func (r *serviceRegistry) callback(method string) *callback {
96-
elem := strings.SplitN(method, serviceMethodSeparator, 2)
97-
if len(elem) != 2 {
96+
before, after, found := strings.Cut(method, serviceMethodSeparator)
97+
if !found {
9898
return nil
9999
}
100100
r.mu.Lock()
101101
defer r.mu.Unlock()
102-
return r.services[elem[0]].callbacks[elem[1]]
102+
return r.services[before].callbacks[after]
103103
}
104104

105105
// subscription returns a subscription callback in the given service.

0 commit comments

Comments
 (0)