Replace uses of strings.Split(N) with strings.Cut()#44381
Merged
tianon merged 28 commits intomoby:masterfrom Dec 21, 2022
Merged
Replace uses of strings.Split(N) with strings.Cut()#44381tianon merged 28 commits intomoby:masterfrom
strings.Split(N) with strings.Cut()#44381tianon merged 28 commits intomoby:masterfrom
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
b2e2f3c to
07702c1
Compare
cpuguy83
approved these changes
Nov 1, 2022
tianon
reviewed
Nov 10, 2022
api/types/types.go
Outdated
| if kv[0] == "name" { | ||
| secopt.Name = kv[1] | ||
| if k == "name" { | ||
| secopt.Name = k |
Member
There was a problem hiding this comment.
Typo 👀
Suggested change
| secopt.Name = k | |
| secopt.Name = v |
| if len(suffix) > 1 { | ||
| return id[:maxlen] + "-" + suffix[1] | ||
| id, suffix, _ := strings.Cut(id, "-") | ||
| if len(id) > 12 { |
Member
There was a problem hiding this comment.
Suggested change
| if len(id) > 12 { | |
| if len(id) > maxlen { |
? 👀
(I think the previous id[:maxlen] approach is reasonably harmless, right?)
Member
Author
There was a problem hiding this comment.
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] | ||
| } |
Member
There was a problem hiding this comment.
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] |
07702c1 to
675fb1c
Compare
thaJeztah
commented
Nov 24, 2022
api/types/types.go
Outdated
| if kv[0] == "name" { | ||
| secopt.Name = kv[1] | ||
| if k == "name" { | ||
| secopt.Name = k |
| if len(suffix) > 1 { | ||
| return id[:maxlen] + "-" + suffix[1] | ||
| id, suffix, _ := strings.Cut(id, "-") | ||
| if len(id) > 12 { |
Member
Author
There was a problem hiding this comment.
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] | ||
| } |
675fb1c to
1defe9f
Compare
1defe9f to
eebfcec
Compare
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>
eebfcec to
6a91e09
Compare
tianon
approved these changes
Dec 21, 2022
thaJeztah
commented
Jan 15, 2023
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) |
Member
Author
There was a problem hiding this comment.
Turned out this one was overly restrictive, as (contrary to the error message) ADDR is optional here (for fd:// scheme); see #44823
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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, butstrings.Cutalso is more performant, so switching made sense to do;I split it up per package, but see individual commits for details