summaryrefslogtreecommitdiff
path: root/libstd/strfind.myr
diff options
context:
space:
mode:
Diffstat (limited to 'libstd/strfind.myr')
-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
}
+