Skip to content

Commit b804811

Browse files
aykevldeadprogram
authored andcommitted
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.
1 parent b51cda9 commit b804811

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/internal/bytealg/bytealg.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,31 @@ func Compare(a, b []byte) int {
4242
}
4343
}
4444

45+
// This function was copied from the Go 1.23 source tree (with runtime_cmpstring
46+
// manually inlined).
47+
func CompareString(a, b string) int {
48+
l := len(a)
49+
if len(b) < l {
50+
l = len(b)
51+
}
52+
for i := 0; i < l; i++ {
53+
c1, c2 := a[i], b[i]
54+
if c1 < c2 {
55+
return -1
56+
}
57+
if c1 > c2 {
58+
return +1
59+
}
60+
}
61+
if len(a) < len(b) {
62+
return -1
63+
}
64+
if len(a) > len(b) {
65+
return +1
66+
}
67+
return 0
68+
}
69+
4570
// Count the number of instances of a byte in a slice.
4671
func Count(b []byte, c byte) int {
4772
// Use a simple implementation, as there is no intrinsic that does this like we want.

0 commit comments

Comments
 (0)