summaryrefslogtreecommitdiff
path: root/lib/std/cmp.myr
blob: 15c49948f827ee306fd55fb7fa4851b93d319b02 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use "extremum"
use "types"
use "utf"
use "chartype"

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)
	const strcasecmp	: (a : byte[:], b : byte[:] -> order)
;;

extern const put : (str : byte[:], args : ... -> int64)

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

const strcmp = {a, b
	var l

	l = min(a.len, b.len)
	for var 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)
}


const strcasecmp = {a, b
	var ca, cb

	while a.len > 0 && b.len > 0
		(ca, a) = std.strstep(a)
		(cb, b) = std.strstep(b)
		ca = toupper(ca)
		cb = toupper(cb)
		if ca < cb
			-> `Before
		elif ca > cb
			-> `After
		;;
	;;
	if a.len < b.len
		-> `Before
	elif a.len > b.len
		-> `After
	else
		-> `Equal
	;;
}