Message:
Hello golan...@googlegroups.com (cc: golan...@googlegroups.com),
I'd like you to review this change to
https://go.googlecode.com/hg/
Description:
bytes: EqualString
Please review this at http://codereview.appspot.com/4969049/
Affected files:
M src/pkg/bytes/bytes.go
M src/pkg/bytes/bytes_test.go
Index: src/pkg/bytes/bytes.go
===================================================================
--- a/src/pkg/bytes/bytes.go
+++ b/src/pkg/bytes/bytes.go
@@ -49,6 +49,19 @@
return true
}
+// EqualString returns a boolean reporting whether string(a) == b.
+func EqualString(a []byte, b string) bool {
+ if len(a) != len(b) {
+ return false
+ }
+ for i, c := range a {
+ if c != b[i] {
+ return false
+ }
+ }
+ return true
+}
+
// explode splits s into an array of UTF-8 sequences, one per Unicode
character (still arrays of bytes),
// up to a maximum of n byte arrays. Invalid UTF-8 sequences are chopped
into individual bytes.
func explode(s []byte, n int) [][]byte {
Index: src/pkg/bytes/bytes_test.go
===================================================================
--- a/src/pkg/bytes/bytes_test.go
+++ b/src/pkg/bytes/bytes_test.go
@@ -65,12 +65,16 @@
b := []byte(tt.b)
cmp := Compare(a, b)
eql := Equal(a, b)
+ eqls := EqualString(a, tt.b)
if cmp != tt.i {
t.Errorf(`Compare(%q, %q) = %v`, tt.a, tt.b, cmp)
}
if eql != (tt.i == 0) {
t.Errorf(`Equal(%q, %q) = %v`, tt.a, tt.b, eql)
}
+ if eqls != (tt.i == 0) {
+ t.Errorf(`EqualString(%q, %q) = %v`, tt.a, tt.b, eqls)
+ }
}
}
One question: Why shouldn't this be in strings?
- Robert
On Mon, Aug 29, 2011 at 9:23 AM, <g...@golang.org> wrote:
> LGTM
>
> http://codereview.appspot.com/4969049/
>
But wait for Russ' opinion.
One question: Why shouldn't this be in strings?
One question: Why shouldn't this be in strings?
If you're doing many comparisons then
existing code typically does a single conversion
outside the loop and then uses == or bytes.Equal.
Russ
This might be below the line, as long as it's
not the first of many. Let's wait and see what Rob thinks.
Russ
Rob is afraid of the strings and bytes packages filling up with hard-to-remember helpers. Although he does have some sympathy for this function, the fact that it's uncertain which package it belongs in pushes him towards the "no" camp.
It would help if these packages had a known structure.
-Rob, for rob
More seriously, why doing
if strings.EqualsBytes(s, b) { ... }
rather than
if string(b) == s { ... }
If this is just about saving the conversion, should this be done by
the compiler?
--
Gustavo Niemeyer
http://niemeyer.net
http://niemeyer.net/plus
http://niemeyer.net/twitter
http://niemeyer.net/blog
-- I never filed a patent.
+1
- gri
+1