shithub: mc

Download patch

ref: 5b8d17c48f84ac4dd5e68a18b7f9ea119ea8745e
parent: 2b3f312f337859198ac209d80be99a23c2e99331
author: Ori Bernstein <ori@eigenstate.org>
date: Sat Sep 3 10:00:16 EDT 2016

Fix 'bysection' iteration.

--- a/lib/inifile/access.myr
+++ b/lib/inifile/access.myr
@@ -5,7 +5,8 @@
 
 pkg inifile =
 	type inisectiter = struct
-		it : std.htkviter((byte[:], byte[:]), byte[:])
+		ini : inifile#
+		idx : int
 	;;
 	impl iterable inisectiter -> byte[:]
 
@@ -41,19 +42,18 @@
 }
 
 const bysection = {ini
-	-> [.it=std.htbykeyvals(ini.elts)]
+	-> [.ini=ini, .idx=0]
 }
 
 impl iterable inisectiter -> byte[:] =
 	__iternext__ = {itp, valp : byte[:]#
-		var kvp : ((byte[:], byte[:]), byte[:])
-		if __iternext__(&itp.it, &kvp)
-			match kvp
-			| ((s, _), _):	valp# = s
-			;;
+		if itp.idx < itp.ini.sects.len
+			valp# = itp.ini.sects[itp.idx]
+			itp.idx++
 			-> true
+		else
+			-> false
 		;;
-		-> false
 	}
 
 	__iterfin__ = {itp, valp -> void
--- a/lib/inifile/parse.myr
+++ b/lib/inifile/parse.myr
@@ -30,6 +30,10 @@
 		std.slfree(sect)
 		std.slfree(key)
 	;;
+	for s in ini.sects
+		std.slfree(s)
+	;;
+	std.slfree(ini.sects)
 	std.htfree(ini.elts)
 	std.free(ini)
 }
@@ -105,6 +109,10 @@
 		;;
 		std.slfree(p.sect)
 		p.sect = std.sldup(std.strstrip(ln[1:idx]))
+		match std.lsearch(ini.sects, p.sect, std.strcmp)
+		| `std.Some s:	/* nothing */
+		| `std.None:	std.slpush(&ini.sects, std.sldup(p.sect))
+		;;
 	| `std.None:
 		p.err = `std.Some (`Parseerr p.line)
 		-> false
--- a/lib/inifile/test/inifile.myr
+++ b/lib/inifile/test/inifile.myr
@@ -4,6 +4,7 @@
 const main = {
 	failopen()
 	getkeys()
+	itersects()
 }
 
 const failopen = {
@@ -33,6 +34,24 @@
 	std.assert(inifile.write(ini, "test/out.ini"), "failed to write test ini")
 
 	inifile.free(ini)
+}
+
+const itersects = {
+	var ini
+	var somesect, anothersection, newsect
+
+	ini = std.try(inifile.load("test/test.ini"))
+	for k in inifile.bysection(ini)
+		match k
+		| "somesect":	somesect++
+		| "another section":	anothersection++
+		| "new sect":	newsect++
+		| bad:	std.fatal("invalid section {}\n", bad)
+		;;
+	;;
+	std.assert(somesect==1, "wrong section count for 'somesect'\n")
+	std.assert(anothersection==1, "wrong section count for 'another section'\n")
+	std.assert(newsect==1, "wrong section count for 'new sect'\n")
 }
 
 const checkval = {ini, sect, key, expected
--- a/lib/inifile/types.myr
+++ b/lib/inifile/types.myr
@@ -8,6 +8,7 @@
 	;;
 
 	type inifile = struct
+		sects	: byte[:][:]
 		elts	: std.htab((byte[:], byte[:]), byte[:])#
 	;;
 ;;
--- a/lib/std/iterutil.myr
+++ b/lib/std/iterutil.myr
@@ -1,4 +1,5 @@
 use "types"
+use "fmt"
 
 pkg std =
 	type zipiter(@a, @b) = struct
@@ -7,8 +8,8 @@
 	;;
 
 	type enumiter(@a) = struct
-		s	: @a[:]
 		idx	: size
+		s	: @a[:]
 	;;
 
 	impl iterable zipiter(@a, @b) -> (@a, @b)
@@ -45,7 +46,8 @@
 
 impl iterable enumiter(@a) -> (size, @b) =
 	__iternext__ = {itp, valp
-		if itp.s.len > itp.idx
+		if itp.idx < itp.s.len
+			std.put("itp.idx: {}, itp.s[{}]: {}\n", itp.idx, itp.idx, itp.s[itp.idx])
 			valp# = (itp.idx, itp.s[itp.idx])
 			itp.idx++
 			-> true
--- a/lib/std/test/iterutil.myr
+++ b/lib/std/test/iterutil.myr
@@ -4,9 +4,9 @@
 	var n
 
 	n = 0
-	for (x, i) in std.byenum([0,2,4,6,8][:])
+	for (x, i) in std.byenum([1,3,5,7,9][:])
 		std.assert(x == n, "invalid enum idx {}", x)
-		std.assert(i == n*2, "invalid enum val {}", i)
+		std.assert(i == n*2 + 1, "invalid enum val {}", i)
 		n++
 	;;