mirror of
https://github.com/valyala/fasthttp.git
synced 2026-06-14 15:56:44 +03:00
Optimized EqualBytesStr
This commit is contained in:
+3
-10
@@ -297,17 +297,10 @@ func hexChar(c byte) byte {
|
||||
|
||||
// EqualBytesStr returns true if string(b) == s.
|
||||
//
|
||||
// It doesn't allocate memory unlike string(b) do.
|
||||
// This function has no performance benefits comparing to string(b) == s.
|
||||
// It is left here for backwards compatilbility only.
|
||||
func EqualBytesStr(b []byte, s string) bool {
|
||||
if len(s) != len(b) {
|
||||
return false
|
||||
}
|
||||
for i, n := 0, len(s); i < n; i++ {
|
||||
if s[i] != b[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
return string(b) == s
|
||||
}
|
||||
|
||||
// AppendBytesStr appends src to dst and returns dst
|
||||
|
||||
@@ -36,3 +36,15 @@ func BenchmarkLowercaseBytesMixed(b *testing.B) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func BenchmarkEqualBytesStr(b *testing.B) {
|
||||
s := "foobarbaraz"
|
||||
bs := []byte(s)
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
if !EqualBytesStr(bs, s) {
|
||||
b.Fatalf("unexpected result: %q != %q", bs, s)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user