shithub: mc

Download patch

ref: 8da5ac5ac4258588634453d55cec95f29a02647b
parent: 56c70bd6049b8eb88c7b7ab3636d6c1761fa0008
author: Ori Bernstein <ori@eigenstate.org>
date: Sat Mar 24 19:45:06 EDT 2018

Clean up and remove edge cases in strfind.

--- a/lib/std/strfind.myr
+++ b/lib/std/strfind.myr
@@ -8,23 +8,17 @@
 ;;
 
 const strfind = {haystack, needle
-	-> strfindin(haystack, needle, 0, haystack.len)
+	-> strfindin(haystack, needle, 0, haystack.len, 1)
 }
 
 const strrfind = {haystack, needle
-	-> strfindin(haystack, needle, haystack.len - 1, -1)
+	-> strfindin(haystack, needle, haystack.len - 1, -1, -1)
 }
 
-const strfindin = {haystack, needle, start, end
-	var inc : size
-
-	inc = 1
-	if start > end
-		inc = -1
+const strfindin = {haystack, needle, start, end, inc
+	if haystack.len == 0 && needle.len == 0
+		-> `std.Some 0
 	;;
-	if needle.len == 0
-		-> `None
-	;;
 	for var i = start; i != end; i += inc
 		/* 
 		if we knew the direction we could terminate early,
@@ -33,14 +27,12 @@
 		if i + needle.len > haystack.len
 			continue
 		;;
-		if haystack[i] == needle[0]
-			for var j = 0; j < needle.len; j++
-				if haystack[i + j] != needle[j]
-					goto nextiter
-				;;
+		for var j = 0; j < needle.len; j++
+			if haystack[i + j] != needle[j]
+				goto nextiter
 			;;
-			-> `Some i
 		;;
+		-> `Some i
 :nextiter
 	;;
 	-> `None
--- a/mbld/test.myr
+++ b/mbld/test.myr
@@ -124,11 +124,7 @@
 
 const matchtest = {name, pat
 	match std.strfind(pat, ":")
-	| `std.Some i:	pat = pat[:i]
-	| `std.None:	/* ok */
-	;;
-	match std.strfind(name, pat)
-	| `std.Some _:	-> true
-	| `std.None:	-> false
+	| `std.Some i:	-> std.strhas(name, pat[:i])
+	| `std.None:	-> std.strhas(name, pat)
 	;;
 }
--- a/test/data/strfind-expected
+++ b/test/data/strfind-expected
@@ -1,4 +1,4 @@
-No match
+Found 0
 No match
 No match
 Found 0