From 01e1c61e9fcf4a0b0164a7fe5395a209a50f6905 Mon Sep 17 00:00:00 2001 From: Berke Güzel Date: Tue, 13 Oct 2026 19:22:05 +0300 Subject: Optimize hot loops with direct access, LUTs, and closed-form formulas --- src/fastcsi.c | 41 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) (limited to 'src/fastcsi.c') diff --git a/src/fastcsi.c b/src/fastcsi.c index 416e3dc..dfd2dba 100644 --- a/src/fastcsi.c +++ b/src/fastcsi.c @@ -91,17 +91,40 @@ static inline const char *parse_color_sequence(const char *p, const char *end, u static const char *parse_256color(const char *p, const char *end, uint32_t *color) { const char *q = p + 2; - unsigned v = 0; - int nd = 0; - while (q < end && (unsigned char)(*q - '0') <= 9) { - v = v * 10 + (unsigned char)(*q++ - '0'); - if (++nd > 3) - return NULL; + if (q >= end) + return NULL; + unsigned char d0 = (unsigned char)(*q - '0'); + if (d0 > 9) + return NULL; + q++; + if (q < end && *q == 'm') { + *color = term_color_index(d0); + return q + 1; + } + if (q >= end) + return NULL; + unsigned char d1 = (unsigned char)(*q - '0'); + if (d1 > 9) + return NULL; + q++; + if (q < end && *q == 'm') { + *color = term_color_index(d0 * 10 + d1); + return q + 1; } - if (!nd || q >= end || *q != 'm' || v > 255) + if (q >= end) + return NULL; + unsigned char d2 = (unsigned char)(*q - '0'); + if (d2 > 9) return NULL; - *color = term_color_index(v); - return q + 1; + q++; + if (q < end && *q == 'm') { + unsigned v = d0 * 100 + d1 * 10 + d2; + if (v <= 255) { + *color = term_color_index(v); + return q + 1; + } + } + return NULL; } TERM_NOINLINE -- cgit v1.2.3