From 1d8201c6562c0efd8db7ed4214dc19efe617e6f6 Mon Sep 17 00:00:00 2001 From: Kelvin Lee Date: Thu, 5 Nov 2020 22:05:54 +1100 Subject: [PATCH 1/4] Print size_t properly with C11 %zd support. --- examples/simple_parse.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/examples/simple_parse.c b/examples/simple_parse.c index 4554e4f..728416d 100644 --- a/examples/simple_parse.c +++ b/examples/simple_parse.c @@ -30,7 +30,7 @@ void print_json(json_t *root); void print_json_aux(json_t *element, int indent); void print_json_indent(int indent); -const char *json_plural(int count); +const char *json_plural(size_t count); void print_json_object(json_t *element, int indent); void print_json_array(json_t *element, int indent); void print_json_string(json_t *element, int indent); @@ -80,7 +80,7 @@ void print_json_indent(int indent) { } } -const char *json_plural(int count) { return count == 1 ? "" : "s"; } +const char *json_plural(size_t count) { return count == 1 ? "" : "s"; } void print_json_object(json_t *element, int indent) { size_t size; @@ -90,7 +90,11 @@ void print_json_object(json_t *element, int indent) { print_json_indent(indent); size = json_object_size(element); - printf("JSON Object of %ld pair%s:\n", size, json_plural(size)); +#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L + printf("JSON Object of %zd pair%s:\n", size, json_plural(size)); +#else + printf("JSON Object of %lld pair%s:\n", (long long)size, json_plural(size)); +#endif json_object_foreach(element, key, value) { print_json_indent(indent + 2); printf("JSON Key: \"%s\"\n", key); @@ -103,7 +107,11 @@ void print_json_array(json_t *element, int indent) { size_t size = json_array_size(element); print_json_indent(indent); - printf("JSON Array of %ld element%s:\n", size, json_plural(size)); +#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L + printf("JSON Array of %zd element%s:\n", size, json_plural(size)); +#else + printf("JSON Array of %lld element%s:\n", (long long)size, json_plural(size)); +#endif for (i = 0; i < size; i++) { print_json_aux(json_array_get(element, i), indent + 2); } From ec1b6318e4d31e8dc7c62ef5bf9b63e0379cdc4b Mon Sep 17 00:00:00 2001 From: Kelvin Lee Date: Thu, 5 Nov 2020 22:06:38 +1100 Subject: [PATCH 2/4] Use size_t to receive result from fread()/ftell(). --- test/bin/json_process.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/bin/json_process.c b/test/bin/json_process.c index f7f46d0..fc98543 100644 --- a/test/bin/json_process.c +++ b/test/bin/json_process.c @@ -63,7 +63,7 @@ static const char *strip(char *str) { } static char *loadfile(FILE *file) { - long fsize, ret; + size_t fsize, ret; char *buf; fseek(file, 0, SEEK_END); From 38c4b80ab70aaa9fbc3d728959083ddff9132b6e Mon Sep 17 00:00:00 2001 From: Kelvin Lee Date: Fri, 6 Nov 2020 08:40:20 +1100 Subject: [PATCH 3/4] Fix hashsize() should return size_t. --- src/lookup3.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lookup3.h b/src/lookup3.h index 7202b21..9b39aa1 100644 --- a/src/lookup3.h +++ b/src/lookup3.h @@ -73,7 +73,7 @@ on 1 byte), but shoehorning those bytes into integers efficiently is messy. # define HASH_BIG_ENDIAN 0 #endif -#define hashsize(n) ((uint32_t)1<<(n)) +#define hashsize(n) ((size_t)1<<(n)) #define hashmask(n) (hashsize(n)-1) #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k)))) From 638449c43dfdc0a37ef999dd349988cf294525e6 Mon Sep 17 00:00:00 2001 From: Kelvin Lee Date: Fri, 6 Nov 2020 15:51:14 +1100 Subject: [PATCH 4/4] C11 %zd is bit overkill here. Especially requiring conditional compile. --- examples/simple_parse.c | 8 -------- 1 file changed, 8 deletions(-) diff --git a/examples/simple_parse.c b/examples/simple_parse.c index 728416d..a96a0f8 100644 --- a/examples/simple_parse.c +++ b/examples/simple_parse.c @@ -90,11 +90,7 @@ void print_json_object(json_t *element, int indent) { print_json_indent(indent); size = json_object_size(element); -#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L - printf("JSON Object of %zd pair%s:\n", size, json_plural(size)); -#else printf("JSON Object of %lld pair%s:\n", (long long)size, json_plural(size)); -#endif json_object_foreach(element, key, value) { print_json_indent(indent + 2); printf("JSON Key: \"%s\"\n", key); @@ -107,11 +103,7 @@ void print_json_array(json_t *element, int indent) { size_t size = json_array_size(element); print_json_indent(indent); -#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L - printf("JSON Array of %zd element%s:\n", size, json_plural(size)); -#else printf("JSON Array of %lld element%s:\n", (long long)size, json_plural(size)); -#endif for (i = 0; i < size; i++) { print_json_aux(json_array_get(element, i), indent + 2); }