summaryrefslogtreecommitdiff
path: root/lib/std/cmp.myr
blob: edc5fe302e70ee1f379e637fe47cb0ad28452e0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use "extremum.use"
use "types.use"

pkg std =
	type order = union
		`Before
		`Equal
		`After
	;;

	generic numcmp	: (a : @a, b : @a -> order)
	const strcmp	: (a : byte[:], b : byte[:] -> order)
	const strncmp	: (a : byte[:], b : byte[:], n : size -> order)
;;

generic numcmp = {a, b
	if a < b
		-> `Before
	elif a == b
		-> `Equal
	else
		-> `After
	;;
}

const strcmp = {a, b
	var l
	var i

	l = min(a.len, b.len)
	for i = 0; i < l; i++
		if a[i] < b[i]
			-> `Before
		elif a[i] > b[i]
			-> `After
		;;
	;;

	if a.len < b.len
		-> `Before
	elif a.len > b.len
		-> `After
	else
		-> `Equal
	;;
		
}

const strncmp = {a, b, n
	a = a[:min(a.len, n)]
	b = b[:min(b.len, n)]
	-> strcmp(a, b)
}