shithub: mc

Download patch

ref: 02efc3a7d15eb97ec7b80be68474d112f65493c8
parent: b6368fd6d548d07f3a94e782579badab14790821
author: Ori Bernstein <ori@eigenstate.org>
date: Thu Oct 1 18:35:08 EDT 2015

Add 'strtok()' implementation to split on spaces.

--- a/lib/std/strsplit.myr
+++ b/lib/std/strsplit.myr
@@ -1,4 +1,5 @@
 use "alloc.use"
+use "chartype.use"
 use "die.use"
 use "extremum.use"
 use "option.use"
@@ -5,11 +6,15 @@
 use "slpush.use"
 use "strfind.use"
 use "types.use"
+use "utf.use"
 
 pkg std =
 	const strsplit	: (s : byte[:], delim : byte[:] -> byte[:][:])
+	const strtok	: (s : byte[:] -> byte[:][:])
 ;;
 
+extern const put	: (fmt : byte[:], args : ... -> size)
+
 const strsplit = {s, delim
 	var last
 	var sp
@@ -33,3 +38,24 @@
 	-> sp
 }
 
+const strtok = {s
+	var i, j
+	var toks
+
+	i = 0
+	toks = [][:]
+	while i != s.len
+		while isspace(std.decode(s[i:])) && i < s.len
+			i++
+		;;
+		j = i
+		while !std.isspace(std.decode(s[j:])) && j < s.len
+			j++
+		;;
+		if i != j
+			toks = slpush(toks, s[i:j])
+		;;
+		i = j
+	;;
+	-> toks
+}
--- /dev/null
+++ b/lib/std/test/strsplit.myr
@@ -1,0 +1,23 @@
+use std
+
+const main = {
+	check(std.strsplit("", ","), [][:])
+	check(std.strsplit("a,b,c ,,d,", ","), \
+		["a", "b", "c ", "", "d", ""][:])
+	check(std.strtok(""), [][:])
+	check(std.strtok(" "), [][:])
+	check(std.strtok("\t"), [][:])
+	check(std.strtok("a b  c\td"), ["a", "b", "c", "d"][:])
+}
+
+const check = {a, b
+	if a.len != b.len
+		std.fatal("a = {}, b = {}\n", a, b)
+		std.fatal("length mismatch: {} != {}: {}\n", a.len, b.len)
+	;;
+	for var i = 0; i < a.len; i++
+		if !std.sleq(a[i], b[i])
+			std.fatal("element {} mismatched: {} != {}\n", i, a[i], b[i])
+		;;
+	;;
+}