diff options
| author | Berke Güzel <wenekar1@gmail.com> | 2026-10-14 17:36:22 +0300 |
|---|---|---|
| committer | Berke Güzel <wenekar1@gmail.com> | 2026-10-14 17:36:22 +0300 |
| commit | 13bf7edad251df9943bd75665a3510535849b0ac (patch) | |
| tree | 4597aae71940edac0b7d8b416c4803cd72ac5345 /src/palette.c | |
| parent | 513bf031a589d9e2a1a2730adf56f95e5e316a06 (diff) | |
Deduplicate scroll and SIMD paths while preserving performance
Diffstat (limited to 'src/palette.c')
| -rw-r--r-- | src/palette.c | 60 |
1 files changed, 18 insertions, 42 deletions
diff --git a/src/palette.c b/src/palette.c index cee5bad..610fe52 100644 --- a/src/palette.c +++ b/src/palette.c @@ -133,19 +133,13 @@ static uint32_t *palette_slot(struct term_theme *p, const char *key, const char *digits = key + 5; if (!*digits || (*digits == '0' && digits[1])) return NULL; - int i; - if (!digits[1]) { - if (digits[0] < '0' || digits[0] > '9') + unsigned i = 0; + for (; *digits; digits++) { + if ((unsigned)(*digits - '0') > 9) return NULL; - i = digits[0] - '0'; - } else if (!digits[2]) { - if (digits[0] < '0' || digits[0] > '9' || digits[1] < '0' || digits[1] > '9') - return NULL; - i = (digits[0] - '0') * 10 + (digits[1] - '0'); + i = i * 10 + (unsigned)(*digits - '0'); if (i > 15) return NULL; - } else { - return NULL; } *fallback = builtin_theme.ansi[i]; return &p->ansi[i]; @@ -179,29 +173,17 @@ static bool parse_hex_channel(const char *s, size_t len, uint8_t *out) *out = (uint8_t)((d0 << 4) | d1); return true; } - if (len == 1) { - int d = term_hex_nib(s[0]); + if (len < 1 || len > 4) + return false; + unsigned value = 0; + for (size_t i = 0; i < len; i++) { + int d = term_hex_nib(s[i]); if (d < 0) return false; - *out = (uint8_t)(d * 17); - return true; + value = (value << 4) | (unsigned)d; } - if (len == 4) { - int d0 = term_hex_nib(s[0]), d1 = term_hex_nib(s[1]); - int d2 = term_hex_nib(s[2]), d3 = term_hex_nib(s[3]); - if ((d0 | d1 | d2 | d3) < 0) - return false; - *out = (uint8_t)((((unsigned)d0 << 12) | ((unsigned)d1 << 8) | ((unsigned)d2 << 4) | (unsigned)d3) * 255 / 65535); - return true; - } - if (len == 3) { - int d0 = term_hex_nib(s[0]), d1 = term_hex_nib(s[1]), d2 = term_hex_nib(s[2]); - if ((d0 | d1 | d2) < 0) - return false; - *out = (uint8_t)((((unsigned)d0 << 8) | ((unsigned)d1 << 4) | (unsigned)d2) * 255 / 4095); - return true; - } - return false; + *out = (uint8_t)(value * 255 / ((1u << (4 * len)) - 1)); + return true; } static bool parse_fraction(const char *s, size_t len, uint8_t *out) @@ -306,19 +288,13 @@ bool term_color_parse(const char *input, uint32_t *out) } if (ci != 3) return false; - uint8_t c0, c1, c2; - if (intensity) { - if (!parse_fraction(channels[0], chlens[0], &c0) || - !parse_fraction(channels[1], chlens[1], &c1) || - !parse_fraction(channels[2], chlens[2], &c2)) - return false; - } else { - if (!parse_hex_channel(channels[0], chlens[0], &c0) || - !parse_hex_channel(channels[1], chlens[1], &c1) || - !parse_hex_channel(channels[2], chlens[2], &c2)) - return false; + uint8_t c[3]; + for (int j = 0; j < 3; j++) { + bool ok = intensity ? parse_fraction(channels[j], chlens[j], &c[j]) : + parse_hex_channel(channels[j], chlens[j], &c[j]); + if (!ok) return false; } - *out = term_rgb(c0, c1, c2); + *out = term_rgb(c[0], c[1], c[2]); return true; } |
