summaryrefslogtreecommitdiff
path: root/lib/std/cmp.myr
diff options
context:
space:
mode:
Diffstat (limited to 'lib/std/cmp.myr')
-rw-r--r--lib/std/cmp.myr54
1 files changed, 54 insertions, 0 deletions
diff --git a/lib/std/cmp.myr b/lib/std/cmp.myr
new file mode 100644
index 0000000..edc5fe3
--- /dev/null
+++ b/lib/std/cmp.myr
@@ -0,0 +1,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)
+}
+