shithub: mc

Download patch

ref: 6e418753ce132f10cb25e2cd4679159c50fba931
parent: 14346a4d8913a81c2c62e18ba150f6b770d5731d
author: Ori Bernstein <[email protected]>
date: Thu Jan 2 05:14:06 EST 2014

Add generic comparator functions.

    Also, make 'strcmp' usable with 'std.sort'

--- a/libstd/Makefile
+++ b/libstd/Makefile
@@ -3,6 +3,7 @@
     alloc.myr \
     blat.myr \
     chartype.myr \
+    cmp.myr \
     die.myr \
     endian.myr \
     env.myr \
@@ -10,6 +11,7 @@
     extremum.myr \
     fmt.myr \
     hashfuncs.myr \
+    hasprefix.myr \
     htab.myr \
     intparse.myr \
     ipparse.myr \
@@ -30,7 +32,6 @@
     strjoin.myr \
     strsplit.myr \
     strstrip.myr \
-    strcmp.myr \
     sys.myr \
     types.myr \
     units.myr \
--- /dev/null
+++ b/libstd/cmp.myr
@@ -1,0 +1,46 @@
+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)
+;;
+
+const strcmp = {a, b
+	var l
+	var i
+	var v
+
+	l = min(a.len, b.len)
+	for i = 0; i < l; i++
+		v = b[i] - a[i]
+		if v < 0
+			-> `Before
+		elif v < 0
+			-> `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)
+}
+
--- a/libstd/intparse.myr
+++ b/libstd/intparse.myr
@@ -1,9 +1,9 @@
 use "die.use"
-use "strcmp.use"
+use "fmt.use"
+use "hasprefix.use"
 use "option.use"
 use "types.use"
 use "utf.use"
-use "fmt.use"
 
 pkg std =
 	generic intparsebase	: (s : byte[:], base : int -> option(@a::(tcint,tcnum,tctest)))
--- a/libstd/ipparse.myr
+++ b/libstd/ipparse.myr
@@ -3,7 +3,7 @@
 use "option.use"
 
  /* FIXME: needed for decls which should be pulled in as hidden */
-use "strcmp.use"
+use "hasprefix.use"
 use "utf.use"
 
 pkg std =
--- a/libstd/sort.myr
+++ b/libstd/sort.myr
@@ -1,9 +1,6 @@
+use "cmp.use"
+
 pkg std =
-	type order = union
-		`Before
-		`Equal
-		`After
-	;;
 	generic sort	: (sl:@a[:], cmp:(a:@a, b:@a -> order) -> @a[:])
 ;;
 
--- a/libstd/strcmp.myr
+++ /dev/null
@@ -1,57 +1,0 @@
-use "extremum.use"
-use "types.use"
-use "fmt.use"
-
-pkg std =
-	type comparison
-
-	const strcmp	: (a : byte[:], b : byte[:] -> comparison)
-	const strncmp	: (a : byte[:], b : byte[:], n : size -> comparison)
-	const hasprefix	: (a : byte[:], b : byte[:] -> bool)
-;;
-
-type comparison = union
-	`Equal
-	`Before
-	`After
-;;
-
-
-const strcmp = {a, b
-	var l
-	var i
-	var v
-
-	l = min(a.len, b.len)
-	for i = 0; i < l; i++
-		v = b[i] - a[i]
-		if v < 0
-			-> `Before
-		elif v < 0
-			-> `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 hasprefix = {s, pre
-	match strncmp(s, pre, pre.len)
-	| `Equal:	-> true
-	| _:		-> false
-	;;
-}
-
--- a/test/stdsort.myr
+++ b/test/stdsort.myr
@@ -4,10 +4,24 @@
 	var a = [
 		3, 5, 4, 9, 7, 2, 6, 0, 1, 8,
 	]
+	var b = [
+		3, 5, 4, 9, 7, 2, 6, 0, 1, 8,
+	]
+	var c = [
+		"a", "aa", "b", "C", "Cc", "cC", "d", "f", "fuckit", "go",
+	]
 
 	std.sort(a[:], intcmp)
-	for i in a
-		std.put("%i\n", i)
+	for v in a
+		std.put("%i\n", v)
+	;;
+	std.sort(b[:], std.numcmp)
+	for v in b
+		std.put("%i\n", v)
+	;;
+	std.sort(c[:], std.strcmp)
+	for v in c
+		std.put("%s\n", v)
 	;;
 }