shithub: mc

Download patch

ref: 4efd6777d6644058c97db129288a4ab48fcfd92d
parent: eeb8989f663c7c2f7ed7e5f92e38bced908cf805
author: Ori Bernstein <ori@eigenstate.org>
date: Wed Jun 13 16:02:18 EDT 2018

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.

--- a/lib/std/striter.myr
+++ b/lib/std/striter.myr
@@ -19,13 +19,20 @@
 		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 =
@@ -90,4 +97,38 @@
 
 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]
 }