diff options
author | Ayke van Laethem <[email protected]> | 2024-06-22 17:54:47 +0200 |
---|---|---|
committer | Ron Evans <[email protected]> | 2024-08-17 11:49:14 +0200 |
commit | b8048112df62edfba34e7c22977845179a4b1c31 (patch) | |
tree | 345d836ed71141c74e7ab76c0d28d592cd7f9f4d /src | |
parent | b51cda9721b626afd8b3d16c8444deed912f941d (diff) | |
download | tinygo-b8048112df62edfba34e7c22977845179a4b1c31.tar.gz tinygo-b8048112df62edfba34e7c22977845179a4b1c31.zip |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/internal/bytealg/bytealg.go | 25 |
1 files changed, 25 insertions, 0 deletions
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. |