[PATCH hare v3] encoding::json: fix infinite loop in iterator next
Export this patch
Signed-off-by: Patrick Widmer <patrick.widmer@tbwnet.ch>
---
Add simple test case for iterator, as suggested
encoding/json/+test/value.ha | 13 +++++++++++++
encoding/json/value.ha | 4 +++-
2 files changed, 16 insertions(+), 1 deletion(-)
diff --git a/encoding/json/+test/value.ha b/encoding/json/+test/value.ha
index 79e08dea..a614d1e0 100644
--- a/encoding/json/+test/value.ha
+++ b/encoding/json/+test/value.ha
@@ -18,3 +18,16 @@
del(&obj, "hello");
assert(get(&obj, "hello") is void);
};
+
+@test fn iterator() void = {
+ let obj = newobject();
+ defer finish(obj);
+
+ set(&obj, "hello", "world");
+ let it = iter(&obj);
+ let pair = next(&it) as (const str, const *value);
+
+ assert(pair.0 == "hello");
+ assert(*pair.1 as str == "world");
+ assert(next(&it) is void);
+};
diff --git a/encoding/json/value.ha b/encoding/json/value.ha
index 486be438..8fd1f642 100644
--- a/encoding/json/value.ha
+++ b/encoding/json/value.ha
@@ -75,11 +75,13 @@ export fn iter(obj: *object) iterator = {
export fn next(iter: *iterator) ((const str, const *value) | void) = {
for (iter.i < len(iter.obj.buckets); iter.i += 1) {
const bucket = &iter.obj.buckets[iter.i];
- for (iter.j < len(bucket); iter.j += 1) {
+ for (iter.j < len(bucket)) {
const key = bucket[iter.j].0;
const val = &bucket[iter.j].1;
+ iter.j += 1;
return (key, val);
};
+ iter.j = 0;
};
};
--
2.36.1