github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/internal/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  //go:build !386 && !amd64 && !s390x && !arm && !arm64 && !ppc64 && !ppc64le && !mips && !mipsle && !wasm && !mips64 && !mips64le
     6  // +build !386,!amd64,!s390x,!arm,!arm64,!ppc64,!ppc64le,!mips,!mipsle,!wasm,!mips64,!mips64le
     7  
     8  package bytealg
     9  
    10  import _ "unsafe" // for go:linkname
    11  
    12  func Compare(a, b []byte) int {
    13  	l := len(a)
    14  	if len(b) < l {
    15  		l = len(b)
    16  	}
    17  	if l == 0 || &a[0] == &b[0] {
    18  		goto samebytes
    19  	}
    20  	for i := 0; i < l; i++ {
    21  		c1, c2 := a[i], b[i]
    22  		if c1 < c2 {
    23  			return -1
    24  		}
    25  		if c1 > c2 {
    26  			return +1
    27  		}
    28  	}
    29  samebytes:
    30  	if len(a) < len(b) {
    31  		return -1
    32  	}
    33  	if len(a) > len(b) {
    34  		return +1
    35  	}
    36  	return 0
    37  }
    38  
    39  //go:linkname runtime_cmpstring runtime.cmpstring
    40  func runtime_cmpstring(a, b string) int {
    41  	l := len(a)
    42  	if len(b) < l {
    43  		l = len(b)
    44  	}
    45  	for i := 0; i < l; i++ {
    46  		c1, c2 := a[i], b[i]
    47  		if c1 < c2 {
    48  			return -1
    49  		}
    50  		if c1 > c2 {
    51  			return +1
    52  		}
    53  	}
    54  	if len(a) < len(b) {
    55  		return -1
    56  	}
    57  	if len(a) > len(b) {
    58  		return +1
    59  	}
    60  	return 0
    61  }