ref: 338eb4e4545d7005f90b73695849ed572be3edc6
parent: 71825983b463d8b86c904bb76db0218b1a621bf4
author: Ori Bernstein <[email protected]>
date: Mon Dec 16 13:09:35 EST 2013
Add a 'strfind' function.
--- a/libstd/Makefile
+++ b/libstd/Makefile
@@ -25,6 +25,7 @@
sljoin.myr \
slpush.myr \
slurp.myr \
+ strfind.myr \
strsplit.myr \
strstrip.myr \
strcmp.myr \
--- /dev/null
+++ b/libstd/strfind.myr
@@ -1,0 +1,26 @@
+use "types.use"
+use "option.use"
+
+pkg std =
+ const strfind : (haystack : byte[:], needle : byte[:] -> option(size))
+;;
+
+const strfind = {haystack, needle
+ var i, j
+
+ for i = 0; i < haystack.len; i++
+ if i + needle.len > haystack.len
+ -> `None
+ ;;
+ if haystack[i] == needle[0]
+ for j = 0; j < needle.len; j++
+ if haystack[i + j] != needle[j]
+ goto nextiter
+ ;;
+ ;;
+ -> `Some i
+ ;;
+:nextiter
+ ;;
+ -> `None
+}
--- /dev/null
+++ b/test/data/strfind-expected
@@ -1,0 +1,8 @@
+No match
+Found 0
+Found 0
+No match
+Found 1
+Found 2
+No match
+No match
--- /dev/null
+++ b/test/strfind.myr
@@ -1,0 +1,22 @@
+use std
+
+const main = {
+ printloc(std.strfind("ab", "abc"))
+ printloc(std.strfind("abc", "abc"))
+ printloc(std.strfind("abcde", "abc"))
+ printloc(std.strfind("abcde", "xyz"))
+ printloc(std.strfind("abcde", "bcd"))
+ printloc(std.strfind("abcde", "cde"))
+ printloc(std.strfind("abcde", "def"))
+ printloc(std.strfind("abcde", "abx"))
+}
+
+const printloc = {l
+ match l
+ | `std.Some loc: std.put("Found %z\n", loc)
+ | `std.None: std.put("No match\n")
+ ;;
+}
+
+
+
--- a/test/tests
+++ b/test/tests
@@ -107,6 +107,7 @@
B catfile C
B strstrip C
B strsplit C
+B strfind C
# B local-labels E 10 ## BUGGERED
F declmismatch
F infermismatch