Skip to content

Commit 0e0fc0d

Browse files
changkuntmm1
authored andcommitted
strings, bytes: add CutPrefix and CutSuffix
Fixes golang#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]> (cherry picked from commit 6800559)
1 parent e93b412 commit 0e0fc0d

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
@@ -1698,6 +1698,48 @@ func TestCut(t *testing.T) {
16981698
}
16991699
}
17001700

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

src/strings/strings.go

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

src/strings/strings_test.go

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

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

0 commit comments

Comments
 (0)