summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2014-09-25 19:48:36 -0400
committerOri Bernstein <ori@eigenstate.org>2014-09-25 19:48:36 -0400
commit16c7d49b1a110574b75b5e3093a39fdc28e33974 (patch)
tree1e15c92fd04fa0c3fb736dcfcfc5b437c00dfd75
parent428f4464c3a58ebc5779bb974bd2e789ded86fd6 (diff)
downloadmc-16c7d49b1a110574b75b5e3093a39fdc28e33974.tar.gz
Add strrfind function for reverse string searches.
-rw-r--r--libstd/strfind.myr24
1 files changed, 21 insertions, 3 deletions
diff --git a/libstd/strfind.myr b/libstd/strfind.myr
index 2ad25f7..e3af0a3 100644
--- a/libstd/strfind.myr
+++ b/libstd/strfind.myr
@@ -3,14 +3,31 @@ use "option.use"
pkg std =
const strfind : (haystack : byte[:], needle : byte[:] -> option(size))
+ const strrfind : (haystack : byte[:], needle : byte[:] -> option(size))
;;
const strfind = {haystack, needle
- var i, j
+ -> strfindin(haystack, needle, 0, haystack.len)
+}
+
+const strrfind = {haystack, needle
+ -> strfindin(haystack, needle, haystack.len - 1, -1)
+}
- for i = 0; i < haystack.len; i++
+const strfindin = {haystack, needle, start, end
+ var i, j, inc : size
+
+ inc = 1
+ if start > end
+ inc = -1
+ ;;
+ for i = start; i != end; i += inc
+ /*
+ if we knew the direction we could terminate early,
+ but we allow the start and end to be passed in.
+ */
if i + needle.len > haystack.len
- -> `None
+ continue
;;
if haystack[i] == needle[0]
for j = 0; j < needle.len; j++
@@ -24,3 +41,4 @@ const strfind = {haystack, needle
;;
-> `None
}
+