From c0bcb33d99ff939eb75758f80c948a10ea6733d2 Mon Sep 17 00:00:00 2001 From: "Anna (navi) Figueiredo Gomes" Date: Fri, 12 Apr 2024 15:30:17 +0200 Subject: json.h: avoid expanding `o` twice Signed-off-by: Anna (navi) Figueiredo Gomes --- include/json.h | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'include/json.h') diff --git a/include/json.h b/include/json.h index 85749d6..c8c844e 100644 --- a/include/json.h +++ b/include/json.h @@ -129,9 +129,6 @@ struct json *json_bool(bool boolean); char *json_print(struct json *json); -#define json_foreach(e, o) \ - for (struct json *e = json_check_type((o), JSON_OBJECT | JSON_ARRAY) ? (o)->children->items[0] : NULL; e; e = e->next) - static inline bool json_check_type(const struct json *json, const enum json_type type) { return json && json->type & type; } @@ -160,4 +157,12 @@ static inline bool json_is_null(const struct json *json) { return json && json->type == JSON_NULL; } +#define json_foreach(e, o) \ + for (struct json *e = json_iter((o)); e; e = e->next) + +// Avoid expanding `o` twice in the foreach macro +static inline struct json *json_iter(struct json *json) { + return json_check_type(json, JSON_OBJECT | JSON_ARRAY) ? json->children->items[0] : NULL; +} + #endif -- cgit v1.2.3