diff options
author | pommicket <pommicket@gmail.com> | 2023-01-05 11:52:57 -0500 |
---|---|---|
committer | pommicket <pommicket@gmail.com> | 2023-01-05 11:54:50 -0500 |
commit | be318b18a01481d16149c34f649c9cdb1f91979d (patch) | |
tree | 9a224f909a78170a67e7e42060d1069aebaed9e8 /colors.c | |
parent | f897a6a62e9572f1f3cc6c7905192d49d7868c9a (diff) |
clean up some TODOs
Diffstat (limited to 'colors.c')
-rw-r--r-- | colors.c | 28 |
1 files changed, 22 insertions, 6 deletions
@@ -7,7 +7,7 @@ typedef struct { const char *name; } ColorName; -static ColorName const color_names[] = { +static ColorName color_names[] = { {COLOR_UNKNOWN, "unknown"}, {COLOR_TEXT, "text"}, {COLOR_TEXT_SECONDARY, "text-secondary"}, @@ -62,12 +62,28 @@ static ColorName const color_names[] = { static_assert_if_possible(arr_count(color_names) == COLOR_COUNT) +int color_name_cmp(const void *av, const void *bv) { + const ColorName *a = av, *b = bv; + return strcmp(a->name, b->name); +} + +void color_init(void) { + qsort(color_names, arr_count(color_names), sizeof *color_names, color_name_cmp); +} + ColorSetting color_setting_from_str(const char *str) { - // @TODO(optimize): sort color_names, binary search - for (int i = 0; i < COLOR_COUNT; ++i) { - ColorName const *n = &color_names[i]; - if (streq(n->name, str)) - return n->setting; + int lo = 0; + int hi = COLOR_COUNT; + while (lo < hi) { + int mid = (lo + hi) / 2; + int cmp = strcmp(color_names[mid].name, str); + if (cmp < 0) { + lo = mid + 1; + } else if (cmp > 0) { + hi = mid; + } else { + return color_names[mid].setting; + } } return COLOR_UNKNOWN; } |