【Golang1.20源码阅读】strings/compare.go

源码

// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package strings

// Compare returns an integer comparing two strings lexicographically.
// Compare返回一个整数,以字典方式比较两个字符串。
// The result will be 0 if a == b, -1 if a < b, and +1 if a > b.
// 如果a==b,结果将为0,如果a<b,则为-1,如果a>b,则结果为+1。
// Compare is included only for symmetry with package bytes.
// Compare仅用于与bytes包对称。
// It is usually clearer and always faster to use the built-in
// string comparison operators ==, <, >, and so on.
// 使用内置的字符串比较运算符==、<、>等通常更清晰、更快速
func Compare(a, b string) int {
	// NOTE(rsc): This function does NOT call the runtime cmpstring function,
	// because we do not want to provide any performance justification for
	// using strings.Compare. Basically no one should use strings.Compare.
	// As the comment above says, it is here only for symmetry with package bytes.
	// If performance is important, the compiler should be changed to recognize
	// the pattern so that all code doing three-way comparisons, not just code
	// using strings.Compare, can benefit.
	// 注意(rsc):这个函数不调用运行时cmpstring函数,因为我们不想为使用strings.Compare提供任何性能理由。
	// 基本上没有人应该使用strings.Compare。正如上面的评论所说,它在这里只是为了与包字节对称。
	// 如果性能很重要,应该更改编译器以识别模式,这样所有进行三元比较的代码,而不仅仅是使用字符串的代码。Compare,都会受益。
	if a == b {
		return 0
	}
	if a < b {
		return -1
	}
	return +1
}

猜你喜欢

转载自blog.csdn.net/qq2942713658/article/details/132458443
今日推荐