github.com/sandwichdev/go-internals@v0.0.0-20210605002614-12311ac6b2c5/bytealg/compare_generic.go (about) 1 // Copyright 2018 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // +build !386,!amd64,!s390x,!arm,!arm64,!ppc64,!ppc64le,!mips,!mipsle,!wasm,!mips64,!mips64le 6 7 package bytealg 8 9 import _ "unsafe" // for go:linkname 10 11 func Compare(a, b []byte) int { 12 l := len(a) 13 if len(b) < l { 14 l = len(b) 15 } 16 if l == 0 || &a[0] == &b[0] { 17 goto samebytes 18 } 19 for i := 0; i < l; i++ { 20 c1, c2 := a[i], b[i] 21 if c1 < c2 { 22 return -1 23 } 24 if c1 > c2 { 25 return +1 26 } 27 } 28 samebytes: 29 if len(a) < len(b) { 30 return -1 31 } 32 if len(a) > len(b) { 33 return +1 34 } 35 return 0 36 } 37 38 //go:linkname runtime_cmpstring runtime.cmpstring 39 func runtime_cmpstring(a, b string) int { 40 l := len(a) 41 if len(b) < l { 42 l = len(b) 43 } 44 for i := 0; i < l; i++ { 45 c1, c2 := a[i], b[i] 46 if c1 < c2 { 47 return -1 48 } 49 if c1 > c2 { 50 return +1 51 } 52 } 53 if len(a) < len(b) { 54 return -1 55 } 56 if len(a) > len(b) { 57 return +1 58 } 59 return 0 60 }