shithub: mc

ref: 8317e87e7b6f9a15ff41f9f7293ef46dc208d0d7
dir: /lib/std/test/strsplit.myr/

View raw version
use std

const main = {
	var b : byte[:][6]

	/* dynamic str split */
	check(std.strsplit("", ","), [][:])
	check(std.strsplit("a,b,c ,,d,", ","), \
		["a", "b", "c ", "", "d", ""][:])
	check(std.strsplit("a,b,c ,,d,", ","), \
		["a", "b", "c ", "", "d", ""][:])

	/* buffered str split */
	check(std.bstrsplit(b[:], "a,b", ","), \
		["a", "b"][:])
	check(std.bstrsplit(b[:], "a,b,c ,,d,", ","), \
		["a", "b", "c ", "", "d", ""][:])
	check(std.bstrsplit(b[:], "a,b,c,d,e,f,g,h", ","), \
		["a", "b", "c", "d", "e", "f,g,h",][:])

	/* tokenizing */
	check(std.strtok(""), [][:])
	check(std.strtok(" "), [][:])
	check(std.strtok("\t"), [][:])
	check(std.strtok("a b  c\td"), ["a", "b", "c", "d"][:])

	/* buffered tokenizing */
	check(std.bstrtok(b[:], "a b  c\td"), ["a", "b", "c", "d"][:])
	check(std.bstrtok(b[:2], "a b  c\td"), ["a", "b  c\td"][:])
	check(std.bstrtok(b[:2], "a b  c\td   "), ["a", "b  c\td"][:])
}

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])
		;;
	;;
}