compare.go

1// Copyright 2015 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
5package strings
6
7// Compare returns an integer comparing two strings lexicographically.
8// The result will be 0 if a == b, -1 if a < b, and +1 if a > b.
9//
10// Compare is included only for symmetry with package bytes.
11// It is usually clearer and always faster to use the built-in
12// string comparison operators ==, <, >, and so on.
13func Compare(a, b string) int {
14	// NOTE(rsc): This function does NOT call the runtime cmpstring function,
15	// because we do not want to provide any performance justification for
16	// using strings.Compare. Basically no one should use strings.Compare.
17	// As the comment above says, it is here only for symmetry with package bytes.
18	// If performance is important, the compiler should be changed to recognize
19	// the pattern so that all code doing three-way comparisons, not just code
20	// using strings.Compare, can benefit.
21	if a == b {
22		return 0
23	}
24	if a < b {
25		return -1
26	}
27	return +1
28}