summaryrefslogtreecommitdiff
path: root/lib/std
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2018-11-12 18:56:18 -0800
committerOri Bernstein <ori@eigenstate.org>2018-11-12 18:56:18 -0800
commitf86a95697db4370ac5fc12432aa20117d0754139 (patch)
treef847c36a4489b4a729736beb37353a8a87c359a4 /lib/std
parentb5bfee96de8a7fae9bdda140f630bea0b5997737 (diff)
downloadmc-f86a95697db4370ac5fc12432aa20117d0754139.tar.gz
Simplify strfind.
Stop trying to be so damn general.
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/strfind.myr41
1 files changed, 23 insertions, 18 deletions
diff --git a/lib/std/strfind.myr b/lib/std/strfind.myr
index f446f69..7a94a4f 100644
--- a/lib/std/strfind.myr
+++ b/lib/std/strfind.myr
@@ -8,34 +8,39 @@ pkg std =
;;
const strfind = {haystack, needle
- -> strfindin(haystack, needle, 0, haystack.len, 1)
+ if needle.len > haystack.len
+ -> `std.None
+ elif needle.len == 0 && haystack.len == 0
+ -> `std.Some 0
+ ;;
+ for var i = 0; i <= haystack.len - needle.len; i++
+ for var j = 0; j < needle.len; j++
+ if haystack[i + j] != needle[j]
+ goto next
+ ;;
+ ;;
+ -> `std.Some i
+:next
+ ;;
+ -> `std.None
}
const strrfind = {haystack, needle
- -> strfindin(haystack, needle, haystack.len - 1, -1, -1)
-}
-
-const strfindin = {haystack, needle, start, end, inc
- if haystack.len == 0 && needle.len == 0
+ if needle.len > haystack.len
+ -> `std.None
+ elif needle.len == 0 && haystack.len == 0
-> `std.Some 0
;;
- for var 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
- continue
- ;;
+ for var i = haystack.len - haystack.len; i > 0; i --
for var j = 0; j < needle.len; j++
if haystack[i + j] != needle[j]
- goto nextiter
+ goto next
;;
;;
- -> `Some i
-:nextiter
+ -> `std.Some i
+:next
;;
- -> `None
+ -> `std.None
}
const strhas = {haystack, needle