From fec87a4b36a67688932e7bb7e1720bd2c4363a61 Mon Sep 17 00:00:00 2001 From: Mattias EngdegÄrd Date: Fri, 19 Jan 2024 15:17:52 +0100 Subject: Add C macro for hash table iteration This removes some boilerplate code and further reduces dependencies on hash table implementation internals. * src/lisp.h (DOHASH): New. * src/comp.c (compile_function, Fcomp__compile_ctxt_to_file): * src/composite.c (composition_gstring_cache_clear_font): * src/emacs-module.c (module_global_reference_p): * src/fns.c (Fmaphash): * src/json.c (lisp_to_json_nonscalar_1): * src/minibuf.c (Ftest_completion): * src/print.c (print): Use it instead of a hand-written loop. --- src/json.c | 47 ++++++++++++++++++++++------------------------- 1 file changed, 22 insertions(+), 25 deletions(-) (limited to 'src/json.c') diff --git a/src/json.c b/src/json.c index 266905f1c34..5434780ba13 100644 --- a/src/json.c +++ b/src/json.c @@ -361,33 +361,30 @@ lisp_to_json_nonscalar_1 (Lisp_Object lisp, json = json_check (json_object ()); count = SPECPDL_INDEX (); record_unwind_protect_ptr (json_release_object, json); - for (ptrdiff_t i = 0; i < HASH_TABLE_SIZE (h); ++i) + DOHASH (h, i) { Lisp_Object key = HASH_KEY (h, i); - if (!hash_unused_entry_key_p (key)) - { - CHECK_STRING (key); - Lisp_Object ekey = json_encode (key); - /* We can't specify the length, so the string must be - null-terminated. */ - check_string_without_embedded_nulls (ekey); - const char *key_str = SSDATA (ekey); - /* Reject duplicate keys. These are possible if the hash - table test is not `equal'. */ - if (json_object_get (json, key_str) != NULL) - wrong_type_argument (Qjson_value_p, lisp); - int status - = json_object_set_new (json, key_str, - lisp_to_json (HASH_VALUE (h, i), conf)); - if (status == -1) - { - /* A failure can be caused either by an invalid key or - by low memory. */ - json_check_utf8 (ekey); - json_out_of_memory (); - } - } - } + CHECK_STRING (key); + Lisp_Object ekey = json_encode (key); + /* We can't specify the length, so the string must be + null-terminated. */ + check_string_without_embedded_nulls (ekey); + const char *key_str = SSDATA (ekey); + /* Reject duplicate keys. These are possible if the hash + table test is not `equal'. */ + if (json_object_get (json, key_str) != NULL) + wrong_type_argument (Qjson_value_p, lisp); + int status + = json_object_set_new (json, key_str, + lisp_to_json (HASH_VALUE (h, i), conf)); + if (status == -1) + { + /* A failure can be caused either by an invalid key or + by low memory. */ + json_check_utf8 (ekey); + json_out_of_memory (); + } + } } else if (NILP (lisp)) return json_check (json_object ()); -- cgit v1.2.3