Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

all: replace some strings.SplitN(arg, sep, 2) with strings.Cut(arg, sep) #28446

Merged
merged 1 commit into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions accounts/scwallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -776,16 +776,16 @@ func (w *Wallet) findAccountPath(account accounts.Account) (accounts.DerivationP
return nil, fmt.Errorf("scheme %s does not match wallet scheme %s", account.URL.Scheme, w.Hub.scheme)
}

parts := strings.SplitN(account.URL.Path, "/", 2)
if len(parts) != 2 {
url, path, found := strings.Cut(account.URL.Path, "/")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not the same. The original code checked that there are not more that 1 occurrences of /, the updated code doesn't and just lumps all further occurrences together.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that's what the original code does

SplitN slices s into substrings separated by sep and returns a slice of the substrings between those separators.
The count determines the number of substrings to return:

parts := strings.SplitN("go/go/go", "/", 2)
fmt.Println(len(parts))
fmt.Println(parts)
// 2
// [go go/go]

parts := strings.SplitN("gogogo", "/", 2)
fmt.Println(len(parts))
fmt.Println(parts)
// 1
// [gogogo]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original code checked that there are not more that 1 occurrences of /,

Nope, it didn't actually

if !found {
return nil, fmt.Errorf("invalid URL format: %s", account.URL)
}

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

return accounts.ParseDerivationPath(parts[1])
return accounts.ParseDerivationPath(path)
}

// Session represents a secured communication session with the wallet.
Expand Down
4 changes: 1 addition & 3 deletions cmd/p2psim/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,9 +417,7 @@ func rpcNode(ctx *cli.Context) error {
}

func rpcSubscribe(client *rpc.Client, out io.Writer, method string, args ...string) error {
parts := strings.SplitN(method, "_", 2)
namespace := parts[0]
method = parts[1]
namespace, method, _ := strings.Cut(method, "_")
ch := make(chan interface{})
subArgs := make([]interface{}, len(args)+1)
subArgs[0] = method
Expand Down
12 changes: 6 additions & 6 deletions p2p/nat/nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ type Interface interface {
// "pmp:192.168.0.1" uses NAT-PMP with the given gateway address
func Parse(spec string) (Interface, error) {
var (
parts = strings.SplitN(spec, ":", 2)
mech = strings.ToLower(parts[0])
ip net.IP
before, after, found = strings.Cut(spec, ":")
mech = strings.ToLower(before)
ip net.IP
)
if len(parts) > 1 {
ip = net.ParseIP(parts[1])
if found {
ip = net.ParseIP(after)
if ip == nil {
return nil, errors.New("invalid IP address")
}
Expand All @@ -86,7 +86,7 @@ func Parse(spec string) (Interface, error) {
case "pmp", "natpmp", "nat-pmp":
return PMP(ip), nil
default:
return nil, fmt.Errorf("unknown mechanism %q", parts[0])
return nil, fmt.Errorf("unknown mechanism %q", before)
}
}

Expand Down
8 changes: 4 additions & 4 deletions p2p/simulations/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,12 +479,12 @@ func (s *Server) StreamNetworkEvents(w http.ResponseWriter, req *http.Request) {
func NewMsgFilters(filterParam string) (MsgFilters, error) {
filters := make(MsgFilters)
for _, filter := range strings.Split(filterParam, "-") {
protoCodes := strings.SplitN(filter, ":", 2)
if len(protoCodes) != 2 || protoCodes[0] == "" || protoCodes[1] == "" {
proto, codes, found := strings.Cut(filter, ":")
if !found || proto == "" || codes == "" {
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" {
filters[MsgFilter{Proto: proto, Code: -1}] = struct{}{}
continue
Expand Down
4 changes: 2 additions & 2 deletions rpc/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ func (msg *jsonrpcMessage) isUnsubscribe() bool {
}

func (msg *jsonrpcMessage) namespace() string {
elem := strings.SplitN(msg.Method, serviceMethodSeparator, 2)
return elem[0]
before, _, _ := strings.Cut(msg.Method, serviceMethodSeparator)
return before
}

func (msg *jsonrpcMessage) String() string {
Expand Down
6 changes: 3 additions & 3 deletions rpc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ func (r *serviceRegistry) registerName(name string, rcvr interface{}) error {

// callback returns the callback corresponding to the given RPC method name.
func (r *serviceRegistry) callback(method string) *callback {
elem := strings.SplitN(method, serviceMethodSeparator, 2)
if len(elem) != 2 {
before, after, found := strings.Cut(method, serviceMethodSeparator)
if !found {
return nil
}
r.mu.Lock()
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.
Expand Down