summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2016-11-26 22:18:09 -0800
committerOri Bernstein <ori@eigenstate.org>2016-11-26 22:18:09 -0800
commiteb94f197e6ba52de5ee513c936b97cf84509643b (patch)
tree52a57d97330354d35dc67389bf11ae1e3fcd12a4
parent40c1498b2d4ef5a7a39dfad83812f03be0c6453e (diff)
downloadmc-eb94f197e6ba52de5ee513c936b97cf84509643b.tar.gz
Add charoffiter.
Iterates by character and offset into the rest of the string. (Should this be the offset of the character?)
-rw-r--r--lib/std/striter.myr32
1 files changed, 30 insertions, 2 deletions
diff --git a/lib/std/striter.myr b/lib/std/striter.myr
index 2d6db55..3c81c98 100644
--- a/lib/std/striter.myr
+++ b/lib/std/striter.myr
@@ -9,15 +9,22 @@ pkg std =
rest : byte[:]
;;
+ type charoffiter = struct
+ str : byte[:]
+ idx : size
+ ;;
+
type splititer = struct
rest : byte[:]
split : byte[:]
;;
- impl iterable chariter -> char
+ impl iterable chariter -> char
+ impl iterable charoffiter -> (char, size)
impl iterable splititer -> byte[:]
const bychar : (str : byte[:] -> chariter)
+ const bycharoff : (str : byte[:] -> charoffiter)
const bysplit : (str : byte[:], split : byte[:] -> splititer)
;;
@@ -38,6 +45,28 @@ const bychar = {str
-> [.rest = str]
}
+
+impl iterable charoffiter -> (char, size) =
+ __iternext__ = {ci, cv
+ var c
+
+ if ci.idx == ci.str.len
+ -> false
+ ;;
+ c = std.decode(ci.str[ci.idx:])
+ ci.idx += std.charlen(c)
+ cv# = (c, ci.idx)
+ -> true
+ }
+
+ __iterfin__ = {ci, c
+ }
+;;
+
+const bycharoff = {s
+ -> [.str=s, .idx=0]
+}
+
impl iterable splititer -> byte[:] =
__iternext__ = {si, sp
match std.strfind(si.rest, si.split)
@@ -62,4 +91,3 @@ impl iterable splititer -> byte[:] =
const bysplit = {str, split
-> [.rest = str, .split = split]
}
-