Debian Codegraph @

strings/compare.ha

raw
1 // SPDX-License-Identifier: MPL-2.0
2 // (c) Hare authors <https://harelang.org>
3
4 use sort::cmp;
5
6 // Compares two strings by their Unicode codepoint sort order. Zero is returned
7 // if the strings are equal, a negative value if a is less than b, or a positive
8 // value if a is greater than b.
9 //
10 // If you only want to check two strings for equality, then this function isn't
11 // necessary; you can compare the strings directly with ==.
12 export fn compare(a: str, b: str) int = {
13 return cmp::strs(&a, &b);
14 };
15
16 @test fn compare() void = {
17 assert(compare("ABC", "ABC") == 0);
18 assert(compare("ABC", "AB") > 0);
19 assert(compare("AB", "ABC") < 0);
20 assert(compare("BCD", "ABC") > 0);
21 assert(compare("ABC", "こんにちは") < 0);
22 assert(compare("ABC", "abc") < 0);
23 };