internal/bytealg: add CompareString

This function was added to Go many years ago, but is starting to be used
in packages in Go 1.23.
This commit is contained in:
Ayke van Laethem
2024-06-22 17:54:47 +02:00
committed by Ron Evans
parent b51cda9721
commit b8048112df
+25
View File
@@ -42,6 +42,31 @@ func Compare(a, b []byte) int {
}
}
// This function was copied from the Go 1.23 source tree (with runtime_cmpstring
// manually inlined).
func CompareString(a, b string) int {
l := len(a)
if len(b) < l {
l = len(b)
}
for i := 0; i < l; i++ {
c1, c2 := a[i], b[i]
if c1 < c2 {
return -1
}
if c1 > c2 {
return +1
}
}
if len(a) < len(b) {
return -1
}
if len(a) > len(b) {
return +1
}
return 0
}
// Count the number of instances of a byte in a slice.
func Count(b []byte, c byte) int {
// Use a simple implementation, as there is no intrinsic that does this like we want.