summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2018-06-13 20:02:18 -0700
committerOri Bernstein <ori@markovcorp.com>2018-06-15 11:38:49 -0700
commit4efd6777d6644058c97db129288a4ab48fcfd92d (patch)
tree440d2fcb8e351ee36e82b99a103ff59b59ea30b6
parenteeb8989f663c7c2f7ed7e5f92e38bced908cf805 (diff)
downloadmc-4efd6777d6644058c97db129288a4ab48fcfd92d.tar.gz
Add API to allow string iteration by non-space tokens.
This allows multiply space separated strings to be interated without memory allocation for std.strtok.
-rw-r--r--lib/std/striter.myr41
1 files changed, 41 insertions, 0 deletions
diff --git a/lib/std/striter.myr b/lib/std/striter.myr
index 0dd1b02..ec3c79c 100644
--- a/lib/std/striter.myr
+++ b/lib/std/striter.myr
@@ -19,13 +19,20 @@ pkg std =
split : byte[:]
;;
+ type tokiter = struct
+ str : byte[:]
+ idx : size
+ ;;
+
impl iterable chariter -> char
impl iterable charoffiter -> (char, size)
impl iterable splititer -> byte[:]
+ impl iterable tokiter -> byte[:]
const bychar : (str : byte[:] -> chariter)
const bycharoff : (str : byte[:] -> charoffiter)
const bysplit : (str : byte[:], split : byte[:] -> splititer)
+ const bytok : (str : byte[:] -> tokiter)
;;
impl iterable chariter -> char =
@@ -91,3 +98,37 @@ impl iterable splititer -> byte[:] =
const bysplit = {str, split
-> [.rest = str, .split = split]
}
+
+impl iterable tokiter -> byte[:] =
+ __iternext__ = {it, sp
+ var s, lo, hi, c
+
+ s = it.str
+ lo = it.idx
+ while lo < s.len
+ c = std.decode(s[lo:])
+ if c != ' ' && c != '\t'
+ break
+ ;;
+ lo += charlen(c)
+ ;;
+ hi = lo
+ while hi < s.len
+ c = std.decode(s[hi:])
+ if c == ' ' || c == '\t'
+ break
+ ;;
+ hi += charlen(c)
+ ;;
+ it.idx = hi
+ sp# = s[lo:hi]
+ -> hi > lo
+ }
+
+ __iterfin__ = {ci, c
+ }
+;;
+
+const bytok = {str
+ -> [.str = str, .idx = 0]
+}