Skip to content

Commit

Permalink
Use strings.Cut() where appropriate. (#14170)
Browse files Browse the repository at this point in the history
`strings.Cut()` is a wrapper around `stringslite.Cut()`:
https://cs.opensource.google/go/go/+/refs/tags/go1.23.1:src/internal/stringslite/strings.go;l=108
`strings.SplitN()` is a wrapper around `strings.genSplit()`:
https://cs.opensource.google/go/go/+/refs/tags/go1.23.1:src/strings/strings.go;l=236

The former (`strings.Cut()`) seems to be slightly more efficient as it
doesn't do any slice allocation.
  • Loading branch information
tomponline authored Sep 27, 2024
2 parents 396d951 + 8825e31 commit ef33aea
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lxc/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ func (g *cmdGlobal) cmpInstancesAndSnapshots(toComplete string) ([]string, cobra
resource := resources[0]

if strings.Contains(resource.name, shared.SnapshotDelimiter) {
instName := strings.SplitN(resource.name, shared.SnapshotDelimiter, 2)[0]
instName, _, _ := strings.Cut(resource.name, shared.SnapshotDelimiter)
snapshots, _ := resource.server.GetInstanceSnapshotNames(instName)
for _, snapshot := range snapshots {
results = append(results, fmt.Sprintf("%s/%s", instName, snapshot))
Expand Down
12 changes: 6 additions & 6 deletions lxc/config/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ type Remote struct {

// ParseRemote splits remote and object.
func (c *Config) ParseRemote(raw string) (remoteName string, resourceName string, err error) {
result := strings.SplitN(raw, ":", 2)
if len(result) == 1 {
remote, object, found := strings.Cut(raw, ":")
if !found {
return c.DefaultRemote, raw, nil
}

_, ok := c.Remotes[result[0]]
_, ok := c.Remotes[remote]
if !ok {
// Attempt to play nice with snapshots containing ":"
if shared.IsSnapshot(raw) && shared.IsSnapshot(result[0]) {
if shared.IsSnapshot(raw) && shared.IsSnapshot(remote) {
return c.DefaultRemote, raw, nil
}

return "", "", fmt.Errorf("The remote \"%s\" doesn't exist", result[0])
return "", "", fmt.Errorf("The remote \"%s\" doesn't exist", remote)
}

return result[0], result[1], nil
return remote, object, nil
}

// GetInstanceServer returns a lxd.InstanceServer for the remote with the given name.
Expand Down
9 changes: 3 additions & 6 deletions lxc/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,10 @@ func (c *cmdExec) run(cmd *cobra.Command, args []string) error {
}

for _, arg := range c.flagEnvironment {
pieces := strings.SplitN(arg, "=", 2)
value := ""
if len(pieces) > 1 {
value = pieces[1]
variable, value, found := strings.Cut(arg, "=")
if found {
env[variable] = value
}

env[pieces[0]] = value
}

// Configure the terminal
Expand Down
8 changes: 4 additions & 4 deletions lxc/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ func (c *cmdProjectInfo) run(cmd *cobra.Command, args []string) error {
byteLimits := []string{"disk", "memory"}
data := [][]string{}
for k, v := range projectState.Resources {
shortKey := strings.SplitN(k, ".", 2)[0]
shortKey, _, _ := strings.Cut(k, ".")

limit := i18n.G("UNLIMITED")
if v.Limit >= 0 {
Expand All @@ -979,9 +979,9 @@ func (c *cmdProjectInfo) run(cmd *cobra.Command, args []string) error {
}

columnName := strings.ToUpper(k)
fields := strings.SplitN(columnName, ".", 2)
if len(fields) == 2 {
columnName = fmt.Sprintf("%s (%s)", fields[0], fields[1])
before, after, found := strings.Cut(columnName, ".")
if found {
columnName = fmt.Sprintf("%s (%s)", before, after)
}

data = append(data, []string{columnName, limit, usage})
Expand Down

0 comments on commit ef33aea

Please sign in to comment.