ref: eb94f197e6ba52de5ee513c936b97cf84509643b
parent: 40c1498b2d4ef5a7a39dfad83812f03be0c6453e
author: Ori Bernstein <ori@eigenstate.org>
date: Sat Nov 26 17:18:09 EST 2016
Add charoffiter. Iterates by character and offset into the rest of the string. (Should this be the offset of the character?)
--- a/lib/std/striter.myr
+++ b/lib/std/striter.myr
@@ -9,15 +9,22 @@
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 @@
-> [.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 @@
const bysplit = {str, split
-> [.rest = str, .split = split]
}
-