Skip to content

Replace uses of strings.Split(N) with strings.Cut()#44381

Merged
tianon merged 28 commits intomoby:masterfrom
thaJeztah:strings_cut
Dec 21, 2022
Merged

Replace uses of strings.Split(N) with strings.Cut()#44381
tianon merged 28 commits intomoby:masterfrom
thaJeztah:strings_cut

Conversation

@thaJeztah
Copy link
Copy Markdown
Member

strings.Cut() was added in go1.18, so now that go1.17 is EOL, it should be ok to use. For some of these I was initially a bit on the fence, but strings.Cut also is more performant, so switching made sense to do;

func BenchmarkSplit(b *testing.B) {
	b.ReportAllocs()
	data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
	for i := 0; i < b.N; i++ {
		for _, s := range data {
			_ = strings.SplitN(s, "=", 2)[0]
		}
	}
}

func BenchmarkCut(b *testing.B) {
	b.ReportAllocs()
	data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
	for i := 0; i < b.N; i++ {
		for _, s := range data {
			_, _, _ = strings.Cut(s, "=")
		}
	}
}
BenchmarkSplit
BenchmarkSplit-10    	 8244206	       128.0 ns/op	     128 B/op	       4 allocs/op
BenchmarkCut
BenchmarkCut-10      	54411998	        21.80 ns/op	       0 B/op	       0 allocs/op

I split it up per package, but see individual commits for details

@thaJeztah thaJeztah added status/2-code-review kind/refactor PR's that refactor, or clean-up code labels Nov 1, 2022
@thaJeztah thaJeztah added this to the v-next milestone Nov 1, 2022
@thaJeztah

This comment was marked as outdated.

@thaJeztah thaJeztah force-pushed the strings_cut branch 2 times, most recently from b2e2f3c to 07702c1 Compare November 1, 2022 19:12
@thaJeztah thaJeztah marked this pull request as ready for review November 1, 2022 21:22
if kv[0] == "name" {
secopt.Name = kv[1]
if k == "name" {
secopt.Name = k
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Typo 👀

Suggested change
secopt.Name = k
secopt.Name = v

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

whoops; fixed!

if len(suffix) > 1 {
return id[:maxlen] + "-" + suffix[1]
id, suffix, _ := strings.Cut(id, "-")
if len(id) > 12 {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
if len(id) > 12 {
if len(id) > maxlen {

? 👀

(I think the previous id[:maxlen] approach is reasonably harmless, right?)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Well, it panics if id would be shorter than 12; https://go.dev/play/p/IHd0vxxaYh2

Details
// You can edit this code!
// Click here and start typing.
package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(getMountpoint("123456789012-"))
	fmt.Println(getMountpoint("12345678901-"))
}

const maxlen = 12

func getMountpoint(id string) string {
	id, suffix, _ := strings.Cut(id, "-")
	id = id[:maxlen]
	if suffix != "" {
		// preserve filesystem suffix.
		id += "-" + suffix
	}
	return id
}

Comment on lines 31 to 34
id, suffix, _ := strings.Cut(id, "-")
if len(id) > 12 {
id = id[:maxlen]
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ie, this could instead be:

Suggested change
id, suffix, _ := strings.Cut(id, "-")
if len(id) > 12 {
id = id[:maxlen]
}
id, suffix, _ := strings.Cut(id, "-")
id = id[:maxlen]

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

See above 😅

Copy link
Copy Markdown
Member Author

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

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

@tianon updated; PTAL

if kv[0] == "name" {
secopt.Name = kv[1]
if k == "name" {
secopt.Name = k
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

whoops; fixed!

if len(suffix) > 1 {
return id[:maxlen] + "-" + suffix[1]
id, suffix, _ := strings.Cut(id, "-")
if len(id) > 12 {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Well, it panics if id would be shorter than 12; https://go.dev/play/p/IHd0vxxaYh2

Details
// You can edit this code!
// Click here and start typing.
package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(getMountpoint("123456789012-"))
	fmt.Println(getMountpoint("12345678901-"))
}

const maxlen = 12

func getMountpoint(id string) string {
	id, suffix, _ := strings.Cut(id, "-")
	id = id[:maxlen]
	if suffix != "" {
		// preserve filesystem suffix.
		id += "-" + suffix
	}
	return id
}

Comment on lines 31 to 34
id, suffix, _ := strings.Cut(id, "-")
if len(id) > 12 {
id = id[:maxlen]
}
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

See above 😅

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
We're looking for a specific prefix, so remove the prefix instead. Also remove
redundant error-wrapping, as `os.Open()` already provides details in the error
returned;

    open /no/such/file: no such file or directory
    open /etc/os-release: permission denied

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Fixes a (theoretical?) panic if ID would be shorter than 12
characters. Also trim the ID _after_ cutting off the suffix.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Comment on lines +665 to 667
proto, addr, ok := strings.Cut(protoAddr, "://")
if !ok || addr == "" {
return nil, fmt.Errorf("bad format %s, expected PROTO://ADDR", protoAddr)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Turned out this one was overly restrictive, as (contrary to the error message) ADDR is optional here (for fd:// scheme); see #44823

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/refactor PR's that refactor, or clean-up code status/2-code-review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants