Source file src/internal/bytealg/compare_generic.go

     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 && !loong64 && !ppc64 && !ppc64le && !mips && !mipsle && !wasm && !mips64 && !mips64le && !riscv64
     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  func CompareString(a, b string) int {
    39  	return runtime_cmpstring(a, b)
    40  }
    41  
    42  //go:linkname runtime_cmpstring runtime.cmpstring
    43  func runtime_cmpstring(a, b string) int {
    44  	l := len(a)
    45  	if len(b) < l {
    46  		l = len(b)
    47  	}
    48  	for i := 0; i < l; i++ {
    49  		c1, c2 := a[i], b[i]
    50  		if c1 < c2 {
    51  			return -1
    52  		}
    53  		if c1 > c2 {
    54  			return +1
    55  		}
    56  	}
    57  	if len(a) < len(b) {
    58  		return -1
    59  	}
    60  	if len(a) > len(b) {
    61  		return +1
    62  	}
    63  	return 0
    64  }
    65  

View as plain text