File: compare.ha

package info (click to toggle)
hare 0.25.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,948 kB
  • sloc: asm: 1,264; makefile: 123; sh: 114; lisp: 101
file content (23 lines) | stat: -rw-r--r-- 764 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// SPDX-License-Identifier: MPL-2.0
// (c) Hare authors <https://harelang.org>

use sort::cmp;

// Compares two strings by their Unicode codepoint sort order. Zero is returned
// if the strings are equal, a negative value if a is less than b, or a positive
// value if a is greater than b.
//
// If you only want to check two strings for equality, then this function isn't
// necessary; you can compare the strings directly with ==.
export fn compare(a: str, b: str) int = {
	return cmp::strs(&a, &b);
};

@test fn compare() void = {
	assert(compare("ABC", "ABC") == 0);
	assert(compare("ABC", "AB") > 0);
	assert(compare("AB", "ABC") < 0);
	assert(compare("BCD", "ABC") > 0);
	assert(compare("ABC", "こんにちは") < 0);
	assert(compare("ABC", "abc") < 0);
};