From 63a254951da0feeb5b11c1da2de4cf37c31f4450 Mon Sep 17 00:00:00 2001 From: NRK Date: Sun, 24 Jul 2022 16:09:58 +0600 Subject: [PATCH] icons-hash: some minor cleanups --- src/icons-hash.c | 49 ++++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/src/icons-hash.c b/src/icons-hash.c index 647e07f4..f422b225 100644 --- a/src/icons-hash.c +++ b/src/icons-hash.c @@ -12,21 +12,23 @@ #ifdef ICONS_GENERATE -#define ICONS_PROBE_MAX_ALLOWED 6 -#define ICONS_MATCH_MAX ((size_t)-1) +#include +#include +#include +#include +#include "icons.h" #ifdef NDEBUG #error "NDEBUG" #endif -#include -#include -#include -#include "icons.h" #define ASSERT(X) assert(X) #define ARRLEN(X) (sizeof(X) / sizeof((X)[0])) #define MAX(A, B) ((A) > (B) ? (A) : (B)) #define HGEN_ITERARATION (1ul << 14) +#define ICONS_PROBE_MAX_ALLOWED 6 +#define ICONS_MATCH_MAX ((size_t)-1) + #if 0 /* enable for debugging */ #define log(...) fprintf(stderr, "[INFO]: " __VA_ARGS__) #else @@ -133,19 +135,9 @@ main(void) assert(found); } - log("hash_start: %u\n", (unsigned)hash_start); - log("hash_mul : %u\n", (unsigned)hash_mul); - log("max_probe : %u\n", max_probe); - - printf("#ifndef INCLUDE_ICONS_GENERATED\n"); - printf("#define INCLUDE_ICONS_GENERATED\n\n"); - - printf("/*\n * NOTE: This file is automatically generated.\n"); - printf(" * DO NOT EDIT THIS FILE DIRECTLY.\n"); - printf(" * Use `icons.h` to customize icons\n */\n\n"); - - printf("#define hash_start %uu\n", hash_start); - printf("#define hash_mul %uu\n\n", hash_mul); + log("hash_start: %6u\n", (unsigned)hash_start); + log("hash_mul : %6u\n", (unsigned)hash_mul); + log("max_probe : %6u\n", max_probe); size_t match_max = 0, icon_max = 0; for (size_t i = 0; i < ARRLEN(icons_name); ++i) { @@ -160,6 +152,15 @@ main(void) icon_max = MAX(icon_max, strlen(exec_icon.icon) + 1); icon_max = MAX(icon_max, strlen(file_icon.icon) + 1); + printf("#ifndef INCLUDE_ICONS_GENERATED\n"); + printf("#define INCLUDE_ICONS_GENERATED\n\n"); + + printf("/*\n * NOTE: This file is automatically generated.\n"); + printf(" * DO NOT EDIT THIS FILE DIRECTLY.\n"); + printf(" * Use `icons.h` to customize icons\n */\n\n"); + + printf("#define hash_start %uu\n", hash_start); + printf("#define hash_mul %uu\n\n", hash_mul); printf("#define ICONS_PROBE_MAX %u\n", max_probe); printf("#define ICONS_MATCH_MAX %zuu\n\n", match_max); @@ -190,13 +191,13 @@ main(void) static uint16_t icon_ext_hash(const char *str) { - const unsigned int z = 16 - ICONS_TABLE_SIZE; /* 16 == size of `hash` in bits */ - uint16_t hash = hash_start; - for (size_t i = 0; i < ICONS_MATCH_MAX && str[i] != '\0'; ++i) { - hash ^= TOUPPER((unsigned char)str[i]) + i; + uint32_t i, hash = hash_start; + const unsigned int z = (sizeof hash * CHAR_BIT) - ICONS_TABLE_SIZE; + for (i = 0; i < ICONS_MATCH_MAX && str[i] != '\0'; ++i) { + hash ^= TOUPPER((unsigned char)str[i]); hash *= hash_mul; } - hash = (hash >> z) ^ hash; + hash ^= (hash >> z); hash *= GOLDEN_RATIO_16; hash >>= z; ASSERT(hash < ARRLEN(table));