From b8048112df62edfba34e7c22977845179a4b1c31 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Sat, 22 Jun 2024 17:54:47 +0200 Subject: [PATCH] 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. --- src/internal/bytealg/bytealg.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/internal/bytealg/bytealg.go b/src/internal/bytealg/bytealg.go index 4cf442e8e..33ece2bba 100644 --- a/src/internal/bytealg/bytealg.go +++ b/src/internal/bytealg/bytealg.go @@ -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.