Skip to content

Commit 6800559

Browse files
changkunRobert Griesemer
authored and
Robert Griesemer
committed
strings, bytes: add CutPrefix and CutSuffix
Fixes #42537 Change-Id: Ie03c2614ffee30ebe707acad6b9f6c28fb134a45 Reviewed-on: https://go-review.googlesource.com/c/go/+/407176 Reviewed-by: Benny Siegert <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Changkun Ou <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 0df7ad2 commit 6800559

File tree

5 files changed

+136
-0
lines changed

5 files changed

+136
-0
lines changed

api/next/42537.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pkg bytes, func CutPrefix([]uint8, []uint8) ([]uint8, bool) #42537
2+
pkg bytes, func CutSuffix([]uint8, []uint8) ([]uint8, bool) #42537
3+
pkg strings, func CutPrefix(string, string) (string, bool) #42537
4+
pkg strings, func CutSuffix(string, string) (string, bool) #42537

src/bytes/bytes.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,3 +1309,29 @@ func Clone(b []byte) []byte {
13091309
}
13101310
return append([]byte{}, b...)
13111311
}
1312+
1313+
// CutPrefix returns s without the provided leading prefix byte slice
1314+
// and reports whether it found the prefix.
1315+
// If s doesn't start with prefix, CutPrefix returns s, false.
1316+
// If prefix is the empty byte slice, CutPrefix returns s, true.
1317+
//
1318+
// CutPrefix returns slices of the original slice s, not copies.
1319+
func CutPrefix(s, prefix []byte) (after []byte, found bool) {
1320+
if !HasPrefix(s, prefix) {
1321+
return s, false
1322+
}
1323+
return s[len(prefix):], true
1324+
}
1325+
1326+
// CutSuffix returns s without the provided ending suffix byte slice
1327+
// and reports whether it found the suffix.
1328+
// If s doesn't end with suffix, CutSuffix returns s, false.
1329+
// If suffix is the empty byte slice, CutSuffix returns s, true.
1330+
//
1331+
// CutSuffix returns slices of the original slice s, not copies.
1332+
func CutSuffix(s, suffix []byte) (after []byte, found bool) {
1333+
if !HasSuffix(s, suffix) {
1334+
return s, false
1335+
}
1336+
return s[:len(s)-len(suffix)], true
1337+
}

src/bytes/bytes_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,6 +1700,48 @@ func TestCut(t *testing.T) {
17001700
}
17011701
}
17021702

1703+
var cutPrefixTests = []struct {
1704+
s, sep string
1705+
after string
1706+
found bool
1707+
}{
1708+
{"abc", "a", "bc", true},
1709+
{"abc", "abc", "", true},
1710+
{"abc", "", "abc", true},
1711+
{"abc", "d", "abc", false},
1712+
{"", "d", "", false},
1713+
{"", "", "", true},
1714+
}
1715+
1716+
func TestCutPrefix(t *testing.T) {
1717+
for _, tt := range cutPrefixTests {
1718+
if after, found := CutPrefix([]byte(tt.s), []byte(tt.sep)); string(after) != tt.after || found != tt.found {
1719+
t.Errorf("CutPrefix(%q, %q) = %q, %v, want %q, %v", tt.s, tt.sep, after, found, tt.after, tt.found)
1720+
}
1721+
}
1722+
}
1723+
1724+
var cutSuffixTests = []struct {
1725+
s, sep string
1726+
after string
1727+
found bool
1728+
}{
1729+
{"abc", "bc", "a", true},
1730+
{"abc", "abc", "", true},
1731+
{"abc", "", "abc", true},
1732+
{"abc", "d", "abc", false},
1733+
{"", "d", "", false},
1734+
{"", "", "", true},
1735+
}
1736+
1737+
func TestCutSuffix(t *testing.T) {
1738+
for _, tt := range cutSuffixTests {
1739+
if after, found := CutSuffix([]byte(tt.s), []byte(tt.sep)); string(after) != tt.after || found != tt.found {
1740+
t.Errorf("CutSuffix(%q, %q) = %q, %v, want %q, %v", tt.s, tt.sep, after, found, tt.after, tt.found)
1741+
}
1742+
}
1743+
}
1744+
17031745
func TestBufferGrowNegative(t *testing.T) {
17041746
defer func() {
17051747
if err := recover(); err == nil {

src/strings/strings.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,3 +1187,25 @@ func Cut(s, sep string) (before, after string, found bool) {
11871187
}
11881188
return s, "", false
11891189
}
1190+
1191+
// CutPrefix returns s without the provided leading prefix string
1192+
// and reports whether it found the prefix.
1193+
// If s doesn't start with prefix, CutPrefix returns s, false.
1194+
// If prefix is the empty string, CutPrefix returns s, true.
1195+
func CutPrefix(s, prefix string) (after string, found bool) {
1196+
if !HasPrefix(s, prefix) {
1197+
return s, false
1198+
}
1199+
return s[len(prefix):], true
1200+
}
1201+
1202+
// CutSuffix returns s without the provided ending suffix string
1203+
// and reports whether it found the suffix.
1204+
// If s doesn't end with suffix, CutSuffix returns s, false.
1205+
// If suffix is the empty string, CutSuffix returns s, true.
1206+
func CutSuffix(s, suffix string) (after string, found bool) {
1207+
if !HasSuffix(s, suffix) {
1208+
return s, false
1209+
}
1210+
return s[:len(s)-len(suffix)], true
1211+
}

src/strings/strings_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1611,6 +1611,48 @@ func TestCut(t *testing.T) {
16111611
}
16121612
}
16131613

1614+
var cutPrefixTests = []struct {
1615+
s, sep string
1616+
after string
1617+
found bool
1618+
}{
1619+
{"abc", "a", "bc", true},
1620+
{"abc", "abc", "", true},
1621+
{"abc", "", "abc", true},
1622+
{"abc", "d", "abc", false},
1623+
{"", "d", "", false},
1624+
{"", "", "", true},
1625+
}
1626+
1627+
func TestCutPrefix(t *testing.T) {
1628+
for _, tt := range cutPrefixTests {
1629+
if after, found := CutPrefix(tt.s, tt.sep); after != tt.after || found != tt.found {
1630+
t.Errorf("CutPrefix(%q, %q) = %q, %v, want %q, %v", tt.s, tt.sep, after, found, tt.after, tt.found)
1631+
}
1632+
}
1633+
}
1634+
1635+
var cutSuffixTests = []struct {
1636+
s, sep string
1637+
after string
1638+
found bool
1639+
}{
1640+
{"abc", "bc", "a", true},
1641+
{"abc", "abc", "", true},
1642+
{"abc", "", "abc", true},
1643+
{"abc", "d", "abc", false},
1644+
{"", "d", "", false},
1645+
{"", "", "", true},
1646+
}
1647+
1648+
func TestCutSuffix(t *testing.T) {
1649+
for _, tt := range cutSuffixTests {
1650+
if after, found := CutSuffix(tt.s, tt.sep); after != tt.after || found != tt.found {
1651+
t.Errorf("CutSuffix(%q, %q) = %q, %v, want %q, %v", tt.s, tt.sep, after, found, tt.after, tt.found)
1652+
}
1653+
}
1654+
}
1655+
16141656
func makeBenchInputHard() string {
16151657
tokens := [...]string{
16161658
"<a>", "<p>", "<b>", "<strong>",

0 commit comments

Comments
 (0)