Set Ready For Review
To view, visit change 407176. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Ian Lance Taylor, Robert Griesemer.
1 comment:
Patchset:
I think this is probably late for Go 1.19 because we are in the freeze window.
Considering this change won't be that much harmful, is it possible to request an exception here?
To view, visit change 407176. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Changkun Ou, Ian Lance Taylor, Robert Griesemer.
Patch set 1:Run-TryBot +1
2 comments:
Patchset:
Looks good, but I think we should also add it to bytes (because bytes and strings are supposed to be kept in sync).
File src/strings/strings.go:
if prefix == "" {
return s, true
}
You can just drop this branch. (And below.)
To view, visit change 407176. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Changkun Ou, Ian Lance Taylor, Robert Griesemer.
2 comments:
Patchset:
Small naming consistency issue.
File src/strings/strings.go:
Maybe rename "ok" to "found" in order to be consistent with Cut?
Same comment applies for CutSuffix below
To view, visit change 407176. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Changkun Ou, Ian Lance Taylor, Ingo Oeser, Robert Griesemer.
1 comment:
Patchset:
Changkun, the Go 1.20 dev tree is open. Want to finish this up?
To view, visit change 407176. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Caleb Spare, Changkun Ou, Ian Lance Taylor, Ingo Oeser, Robert Griesemer.
Changkun Ou uploaded patch set #2 to this change.
The following approvals got outdated and were removed: Run-TryBot+1 by Caleb Spare, TryBot-Result-1 by Gopher Robot
strings, bytes: add CutPrefix and CutSuffix
Fixes #42537
Change-Id: Ie03c2614ffee30ebe707acad6b9f6c28fb134a45
---
A api/next/42537.txt
M src/bytes/bytes.go
M src/bytes/bytes_test.go
M src/strings/strings.go
M src/strings/strings_test.go
5 files changed, 147 insertions(+), 0 deletions(-)
To view, visit change 407176. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Caleb Spare, Ian Lance Taylor, Ingo Oeser, Robert Griesemer, Russ Cox.
Patch set 2:Run-TryBot +1
2 comments:
File src/strings/strings.go:
Yes please.
Done
if prefix == "" {
return s, true
}
You can just drop this branch. (And below. […]
Done
To view, visit change 407176. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Brad Fitzpatrick, Caleb Spare, Ian Lance Taylor, Ingo Oeser, Robert Griesemer, Russ Cox.
1 comment:
Patchset:
Changkun, the Go 1.20 dev tree is open. […]
Sorry for the late response. The new patch has been uploaded.
To view, visit change 407176. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Brad Fitzpatrick, Caleb Spare, Changkun Ou, Ian Lance Taylor, Ingo Oeser, Robert Griesemer, Russ Cox.
Patch set 2:Code-Review +2
1 comment:
Patchset:
Thanks.
RELNOTE=yes
To view, visit change 407176. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Brad Fitzpatrick, Caleb Spare, Changkun Ou, Ian Lance Taylor, Ingo Oeser, Robert Griesemer, Russ Cox.
Changkun Ou uploaded patch set #3 to this change.
The following approvals got outdated and were removed: Run-TryBot+1 by Changkun Ou, TryBot-Result+1 by Gopher Robot
strings, bytes: add CutPrefix and CutSuffix
Fixes #42537
Change-Id: Ie03c2614ffee30ebe707acad6b9f6c28fb134a45
---
A api/next/42537.txt
M src/bytes/bytes.go
M src/bytes/bytes_test.go
M src/strings/strings.go
M src/strings/strings_test.go
5 files changed, 147 insertions(+), 0 deletions(-)
To view, visit change 407176. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Brad Fitzpatrick, Caleb Spare, Ian Lance Taylor, Ingo Oeser, Robert Griesemer, Russ Cox.
Patch set 3:Run-TryBot +1
1 comment:
Patchset:
rebased to resolve conflict
To view, visit change 407176. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Brad Fitzpatrick, Caleb Spare, Changkun Ou, Ian Lance Taylor, Ingo Oeser, Robert Griesemer, Russ Cox.
Patch set 3:Code-Review +1
Robert Griesemer submitted this change.
2 is the latest approved patch-set.
The change was submitted with unreviewed changes in the following files:
```
The name of the file: src/bytes/bytes.go
Insertions: 10, Deletions: 0.
The diff is too large to show. Please review the diff.
```
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 <bsie...@gmail.com>
TryBot-Result: Gopher Robot <go...@golang.org>
Run-TryBot: Changkun Ou <ma...@changkun.de>
Reviewed-by: Ian Lance Taylor <ia...@google.com>
---
A api/next/42537.txt
M src/bytes/bytes.go
M src/bytes/bytes_test.go
M src/strings/strings.go
M src/strings/strings_test.go
5 files changed, 152 insertions(+), 0 deletions(-)
diff --git a/api/next/42537.txt b/api/next/42537.txt
new file mode 100644
index 0000000..4f2446a
--- /dev/null
+++ b/api/next/42537.txt
@@ -0,0 +1,4 @@
+pkg bytes, func CutPrefix([]uint8, []uint8) ([]uint8, bool) #42537
+pkg bytes, func CutSuffix([]uint8, []uint8) ([]uint8, bool) #42537
+pkg strings, func CutPrefix(string, string) (string, bool) #42537
+pkg strings, func CutSuffix(string, string) (string, bool) #42537
\ No newline at end of file
diff --git a/src/bytes/bytes.go b/src/bytes/bytes.go
index 27834fc..c0cd704 100644
--- a/src/bytes/bytes.go
+++ b/src/bytes/bytes.go
@@ -1309,3 +1309,29 @@
}
return append([]byte{}, b...)
}
+
+// CutPrefix returns s without the provided leading prefix byte slice
+// and reports whether it found the prefix.
+// If s doesn't start with prefix, CutPrefix returns s, false.
+// If prefix is the empty byte slice, CutPrefix returns s, true.
+//
+// CutPrefix returns slices of the original slice s, not copies.
+func CutPrefix(s, prefix []byte) (after []byte, found bool) {
+ if !HasPrefix(s, prefix) {
+ return s, false
+ }
+ return s[len(prefix):], 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.
+// If suffix is the empty byte slice, CutSuffix returns s, true.
+//
+// CutSuffix returns slices of the original slice s, not copies.
+func CutSuffix(s, suffix []byte) (after []byte, found bool) {
+ if !HasSuffix(s, suffix) {
+ return s, false
+ }
+ return s[:len(s)-len(suffix)], true
+}
diff --git a/src/bytes/bytes_test.go b/src/bytes/bytes_test.go
index 392657d..05c7ccc 100644
--- a/src/bytes/bytes_test.go
+++ b/src/bytes/bytes_test.go
@@ -1700,6 +1700,48 @@
}
}
+var cutPrefixTests = []struct {
+ s, sep string
+ after string
+ found bool
+}{
+ {"abc", "a", "bc", true},
+ {"abc", "abc", "", true},
+ {"abc", "", "abc", true},
+ {"abc", "d", "abc", false},
+ {"", "d", "", false},
+ {"", "", "", true},
+}
+
+func TestCutPrefix(t *testing.T) {
+ for _, tt := range cutPrefixTests {
+ if after, found := CutPrefix([]byte(tt.s), []byte(tt.sep)); string(after) != tt.after || found != tt.found {
+ t.Errorf("CutPrefix(%q, %q) = %q, %v, want %q, %v", tt.s, tt.sep, after, found, tt.after, tt.found)
+ }
+ }
+}
+
+var cutSuffixTests = []struct {
+ s, sep string
+ after string
+ found bool
+}{
+ {"abc", "bc", "a", true},
+ {"abc", "abc", "", true},
+ {"abc", "", "abc", true},
+ {"abc", "d", "abc", false},
+ {"", "d", "", false},
+ {"", "", "", true},
+}
+
+func TestCutSuffix(t *testing.T) {
+ for _, tt := range cutSuffixTests {
+ if after, found := CutSuffix([]byte(tt.s), []byte(tt.sep)); string(after) != tt.after || found != tt.found {
+ t.Errorf("CutSuffix(%q, %q) = %q, %v, want %q, %v", tt.s, tt.sep, after, found, tt.after, tt.found)
+ }
+ }
+}
+
func TestBufferGrowNegative(t *testing.T) {
defer func() {
if err := recover(); err == nil {
diff --git a/src/strings/strings.go b/src/strings/strings.go
index 013d718..4ab78c3 100644
--- a/src/strings/strings.go
+++ b/src/strings/strings.go
@@ -1187,3 +1187,25 @@
}
return s, "", false
}
+
+// CutPrefix returns s without the provided leading prefix string
+// and reports whether it found the prefix.
+// If s doesn't start with prefix, CutPrefix returns s, false.
+// If prefix is the empty string, CutPrefix returns s, true.
+func CutPrefix(s, prefix string) (after string, found bool) {
+ if !HasPrefix(s, prefix) {
+ return s, false
+ }
+ return s[len(prefix):], 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.
+// If suffix is the empty string, CutSuffix returns s, true.
+func CutSuffix(s, suffix string) (after string, found bool) {
+ if !HasSuffix(s, suffix) {
+ return s, false
+ }
+ return s[:len(s)-len(suffix)], true
+}
diff --git a/src/strings/strings_test.go b/src/strings/strings_test.go
index a1604c2..d6153aa 100644
--- a/src/strings/strings_test.go
+++ b/src/strings/strings_test.go
@@ -1611,6 +1611,48 @@
}
}
+var cutPrefixTests = []struct {
+ s, sep string
+ after string
+ found bool
+}{
+ {"abc", "a", "bc", true},
+ {"abc", "abc", "", true},
+ {"abc", "", "abc", true},
+ {"abc", "d", "abc", false},
+ {"", "d", "", false},
+ {"", "", "", true},
+}
+
+func TestCutPrefix(t *testing.T) {
+ for _, tt := range cutPrefixTests {
+ if after, found := CutPrefix(tt.s, tt.sep); after != tt.after || found != tt.found {
+ t.Errorf("CutPrefix(%q, %q) = %q, %v, want %q, %v", tt.s, tt.sep, after, found, tt.after, tt.found)
+ }
+ }
+}
+
+var cutSuffixTests = []struct {
+ s, sep string
+ after string
+ found bool
+}{
+ {"abc", "bc", "a", true},
+ {"abc", "abc", "", true},
+ {"abc", "", "abc", true},
+ {"abc", "d", "abc", false},
+ {"", "d", "", false},
+ {"", "", "", true},
+}
+
+func TestCutSuffix(t *testing.T) {
+ for _, tt := range cutSuffixTests {
+ if after, found := CutSuffix(tt.s, tt.sep); after != tt.after || found != tt.found {
+ t.Errorf("CutSuffix(%q, %q) = %q, %v, want %q, %v", tt.s, tt.sep, after, found, tt.after, tt.found)
+ }
+ }
+}
+
func makeBenchInputHard() string {
tokens := [...]string{
"<a>", "<p>", "<b>", "<strong>",
To view, visit change 407176. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Changkun Ou.
1 comment:
File src/strings/strings.go:
Patch Set #4, Line 1206: after
Shouldn't it have been named "before"?
Fair point. Feel free to send a CL.
To view, visit change 407176. To unsubscribe, or for help writing mail filters, visit settings.
1 comment:
File src/strings/strings.go:
Patch Set #4, Line 1206: after
Fair point. Feel free to send a CL.
While writing the code, "after" actually means the results after this cut to me.
To view, visit change 407176. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Changkun Ou.
1 comment:
File src/strings/strings.go:
Patch Set #4, Line 1206: after
Shouldn't it have been named "before"?
To view, visit change 407176. To unsubscribe, or for help writing mail filters, visit settings.
File src/strings/strings.go:
Patch Set #4, Line 1206: after
"after" actually means the results after this cut to me
That was one of the alternatives I considered. But then I looked at the Cut function, and in that context it stoped making sense. I'll be happy to send a CL.
To view, visit change 407176. To unsubscribe, or for help writing mail filters, visit settings.
File src/strings/strings.go:
Patch Set #4, Line 1206: after
> "after" actually means the results after this cut to me […]
See CL 424922.
To view, visit change 407176. To unsubscribe, or for help writing mail filters, visit settings.