Skip to content

strings: added CutSpace, bytes: added CutSpace #63195

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions src/bytes/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -1377,6 +1377,20 @@ func CutPrefix(s, prefix []byte) (after []byte, found bool) {
return s[len(prefix):], true
}

// CutSpace slices s around the each instance of one or more consecutive white space
// characters, as defined by unicode.IsSpace, returning the text before and after the
// white space characters. The found result reports white space characters appears in s.
// If no whitespace characters appear in s, CutSpace returns s, nil, false.
//
// CutSpace returns slices of the original slice s, not copies.
func CutSpace(s []byte) (before, after []byte, found bool) {
i := indexFunc(s, unicode.IsSpace, true)
if i == -1 {
return s, nil, false
}
return s[:i], TrimLeftFunc(s[i:], unicode.IsSpace), true
}

// CutSuffix returns s without the provided ending suffix byte slice
// and reports whether it found the suffix.
// If s doesn't end with suffix, CutSuffix returns s, false.
Expand Down
24 changes: 24 additions & 0 deletions src/bytes/bytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1758,6 +1758,30 @@ func TestCutPrefix(t *testing.T) {
}
}

var cutSpaceTests = []struct {
s string
before, after string
found bool
}{
{"abc", "abc", "", false},
{" abc", "", "abc", true},
{"ab c", "ab", "c", true},
{"abc ", "abc", "", true},
{"abc ", "abc", "", true},
{" abc ", "", "abc ", true},
{"a\u0085bc", "a", "bc", true},
{"a\t\n\v\f\r\u0085\u00A0bc", "a", "bc", true},
{"", "", "", false},
}

func TestCutSpace(t *testing.T) {
for _, tt := range cutSpaceTests {
if before, after, found := CutSpace([]byte(tt.s)); string(before) != tt.before || string(after) != tt.after || found != tt.found {
t.Errorf("Cut(%q) = %q, %q, %v, want %q, %q, %v", tt.s, before, after, found, tt.before, tt.after, tt.found)
}
}
}

var cutSuffixTests = []struct {
s, sep string
before string
Expand Down
12 changes: 12 additions & 0 deletions src/strings/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -1288,6 +1288,18 @@ func CutPrefix(s, prefix string) (after string, found bool) {
return s[len(prefix):], true
}

// CutSpace slices s around the each instance of one or more consecutive white space
// characters, as defined by unicode.IsSpace, returning the text before and after the
// white space characters. The found result reports white space characters appears in s.
// If no whitespace characters appear in s, CutSpace returns s, "", false.
func CutSpace(s string) (before, after string, found bool) {
i := indexFunc(s, unicode.IsSpace, true)
if i == -1 {
return s, "", false
}
return s[:i], TrimLeftFunc(s[i:], unicode.IsSpace), true
}

// CutSuffix returns s without the provided ending suffix string
// and reports whether it found the suffix.
// If s doesn't end with suffix, CutSuffix returns s, false.
Expand Down
25 changes: 25 additions & 0 deletions src/strings/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1677,6 +1677,31 @@ func TestCutPrefix(t *testing.T) {
}
}

var cutSpaceTests = []struct {
s string
before, after string
found bool
}{
{"abc", "abc", "", false},
{" abc", "", "abc", true},
{"ab c", "ab", "c", true},
{"abc ", "abc", "", true},
{"abc ", "abc", "", true},
{" abc ", "", "abc ", true},
{"a\u0085bc", "a", "bc", true},
{"a\t\n\v\f\r\u0085\u00A0bc", "a", "bc", true},
{"", "", "", false},
}

func TestCutSpace(t *testing.T) {
for _, tt := range cutSpaceTests {
if before, after, found := CutSpace(tt.s); before != tt.before || after != tt.after || found != tt.found {
t.Errorf("CutSpace(%q) = %q, %q, %v, want %q, %q, %v", tt.s, before, after, found, tt.before, tt.after, tt.found)
}
}
}


var cutSuffixTests = []struct {
s, sep string
before string
Expand Down