shithub: mc

Download patch

ref: 31247e29fb596b44fdd5c18140bc945b6a4b71ce
parent: fdafa085a245d4f48f84c6875a6b7cb89b8b5c6c
author: Ori Bernstein <ori@eigenstate.org>
date: Mon May 23 07:21:55 EDT 2016

Refactor thread matching.

--- a/lib/regex/interp.myr
+++ b/lib/regex/interp.myr
@@ -18,15 +18,9 @@
 	re.str = str
 	re.strp = 0
 	thr = run(re, true)
-	if thr != Zthr
-		m = getmatches(re, thr)
-		thrfree(re, thr)
-		cleanup(re)
-		-> `std.Some m
-	else
-		cleanup(re)
-		->  `std.None
-	;;
+	m = getmatches(re, thr)
+	cleanup(re)
+	-> m
 }
 
 const search = {re, str
@@ -33,20 +27,19 @@
 	var thr
 	var m
 
+	m = `std.None
 	for var i = 0; i < str.len; i++
 		re.str = str[i:]
 		re.strp = 0
 		thr = run(re, false)
-		if thr != Zthr
-			m = getmatches(re, thr)
-			thrfree(re, thr)
-			cleanup(re)
-			-> `std.Some m
-		else
-			cleanup(re)
+		m = getmatches(re, thr)
+		match m
+		| `std.Some _:	break
+		| `std.None:	/* nothing */
 		;;
+		cleanup(re)
 	;;
-	->  `std.None
+	->  m
 }
 
 const cleanup = {re
@@ -70,6 +63,9 @@
 const getmatches = {re, thr
 	var ret
 
+	if thr == Zthr
+		-> `std.None
+	;;
 	ret = std.slalloc(re.nmatch)
 	for var i = 0; i < re.nmatch; i++
 		if thr.mstart[i] != -1 && thr.mend[i] != -1
@@ -78,7 +74,8 @@
 			ret[i] = [][:]
 		;;
 	;;
-	-> ret
+	thrfree(re, thr)
+	-> `std.Some ret
 }