shithub: mc

Download patch

ref: 856307892da283e02158ae7543d1a8f9e632fbf1
parent: f5bedf7001b8fb6e42ab1f044b27f3a39eb537c6
author: Ori Bernstein <[email protected]>
date: Tue Dec 17 16:38:14 EST 2013

Make the implementation match the documentation.

--- a/Makefile
+++ b/Makefile
@@ -7,3 +7,5 @@
 include config.mk
 include mk/myr.mk
 
+check: all
+	make -C test check
--- a/interp.myr
+++ b/interp.myr
@@ -3,30 +3,42 @@
 use "types.use"
 
 pkg regex =
-	const exec	: (re : regex#, str : byte[:] -> bool)
-	const search	: (re : regex#, str : byte[:] -> bool)
+	const exec	: (re : regex#, str : byte[:] -> std.option(byte[:][:]))
+	const search	: (re : regex#, str : byte[:] -> std.option(byte[:][:]))
 ;;
 
+const Zthr = 0 castto(rethread#)
 
 const exec = {re, str
-	var i
-
 	re.str = str
 	re.strp = 0
 	run(re)
 	match re.matched
 	| `std.Some thr:
-		for i = 0; i < re.nmatch; i++
-			std.put("match %z:", i)
-			std.put("[%z..%z]\n", thr.mstart[i], thr.mend[i])
+		if thr.mend[0] != str.len
+			-> `std.None
+		else
+			-> `std.Some getmatches(re, thr)
 		;;
-		-> thr.mend[0] == str.len
 	| `std.None:
-		-> false
+		-> `std.None
 	;;
 }
 
-const Zthr = 0 castto(rethread#)
+const getmatches = {re, thr
+	var ret
+	var i
+
+	ret = std.slalloc(re.nmatch)
+	for i = 0; i < re.nmatch; i++
+		if thr.mstart[i] != -1 && thr.mend[i] != -1
+			ret[i] = re.str[thr.mstart[i]:thr.mend[i]]
+		else
+			ret[i] = [][:]
+		;;
+	;;
+	-> ret
+}
 
 const run = {re
 	var i, consumed
--- a/test/Makefile
+++ b/test/Makefile
@@ -11,6 +11,7 @@
 
 .PHONY: clean
 clean:
+	rm -f testmatch.use testmatch.o
 	@for i in `awk '/^[A-Z]/{print $$2}' tests`; do \
 	    echo rm -f $$i; \
 	    rm -f $$i; \