summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2016-09-03 14:00:16 -0700
committerOri Bernstein <ori@eigenstate.org>2016-09-03 14:00:16 -0700
commit5b8d17c48f84ac4dd5e68a18b7f9ea119ea8745e (patch)
treed74de5e9176696b9b16c87fe7d97dd4453e47d87
parent2b3f312f337859198ac209d80be99a23c2e99331 (diff)
downloadmc-5b8d17c48f84ac4dd5e68a18b7f9ea119ea8745e.tar.gz
Fix 'bysection' iteration.
-rw-r--r--lib/inifile/access.myr16
-rw-r--r--lib/inifile/parse.myr8
-rw-r--r--lib/inifile/test/inifile.myr19
-rw-r--r--lib/inifile/types.myr1
-rw-r--r--lib/std/iterutil.myr6
-rw-r--r--lib/std/test/iterutil.myr4
6 files changed, 42 insertions, 12 deletions
diff --git a/lib/inifile/access.myr b/lib/inifile/access.myr
index f43fddc..49a6476 100644
--- a/lib/inifile/access.myr
+++ b/lib/inifile/access.myr
@@ -5,7 +5,8 @@ use "types"
pkg inifile =
type inisectiter = struct
- it : std.htkviter((byte[:], byte[:]), byte[:])
+ ini : inifile#
+ idx : int
;;
impl iterable inisectiter -> byte[:]
@@ -41,19 +42,18 @@ const put = {ini, sect, key, val
}
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
diff --git a/lib/inifile/parse.myr b/lib/inifile/parse.myr
index a44af72..420fc80 100644
--- a/lib/inifile/parse.myr
+++ b/lib/inifile/parse.myr
@@ -30,6 +30,10 @@ const free = {ini
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 @@ const parsesection = {p, ini, ln
;;
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
diff --git a/lib/inifile/test/inifile.myr b/lib/inifile/test/inifile.myr
index 4fd894f..5c8c679 100644
--- a/lib/inifile/test/inifile.myr
+++ b/lib/inifile/test/inifile.myr
@@ -4,6 +4,7 @@ use inifile
const main = {
failopen()
getkeys()
+ itersects()
}
const failopen = {
@@ -35,6 +36,24 @@ const getkeys = {
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
match inifile.get(ini, sect, key)
| `std.Some val:
diff --git a/lib/inifile/types.myr b/lib/inifile/types.myr
index 3df3d50..f573b7b 100644
--- a/lib/inifile/types.myr
+++ b/lib/inifile/types.myr
@@ -8,6 +8,7 @@ pkg inifile =
;;
type inifile = struct
+ sects : byte[:][:]
elts : std.htab((byte[:], byte[:]), byte[:])#
;;
;;
diff --git a/lib/std/iterutil.myr b/lib/std/iterutil.myr
index 3ba9c08..0b7e0ea 100644
--- 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 @@ pkg std =
;;
type enumiter(@a) = struct
- s : @a[:]
idx : size
+ s : @a[:]
;;
impl iterable zipiter(@a, @b) -> (@a, @b)
@@ -45,7 +46,8 @@ generic byenum = {a
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
diff --git a/lib/std/test/iterutil.myr b/lib/std/test/iterutil.myr
index d4dde60..e41c37f 100644
--- a/lib/std/test/iterutil.myr
+++ b/lib/std/test/iterutil.myr
@@ -4,9 +4,9 @@ const main = {
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++
;;