summaryrefslogtreecommitdiff
path: root/src/input.c
diff options
context:
space:
mode:
authorBerke Güzel <wenekar1@gmail.com>2026-10-13 19:22:05 +0300
committerBerke Güzel <wenekar1@gmail.com>2026-10-13 19:22:05 +0300
commit01e1c61e9fcf4a0b0164a7fe5395a209a50f6905 (patch)
tree1f1eb33e49eab338650a42e8f602af75d8d28a24 /src/input.c
parent2d02d39d2cd32e9eb559638865d2746f02c08556 (diff)
Optimize hot loops with direct access, LUTs, and closed-form formulas
Diffstat (limited to 'src/input.c')
-rw-r--r--src/input.c27
1 files changed, 18 insertions, 9 deletions
diff --git a/src/input.c b/src/input.c
index 5625825..798ffc0 100644
--- a/src/input.c
+++ b/src/input.c
@@ -37,6 +37,16 @@ static inline void sb_bytes(struct seqbuf *sb, const char *s, size_t len)
static inline void sb_u32(struct seqbuf *sb, uint32_t val)
{
+ if (val < 10) {
+ char c = (char)('0' + val);
+ sb_bytes(sb, &c, 1);
+ return;
+ }
+ if (val < 100) {
+ char s[2] = { (char)('0' + (val / 10)), (char)('0' + (val % 10)) };
+ sb_bytes(sb, s, 2);
+ return;
+ }
char tmp[10];
char *end = tmp + sizeof(tmp);
char *p = end;
@@ -652,16 +662,15 @@ struct term_input_result term_input_encode_mouse(const struct term_mouse_event *
sb_u32(&sb, (uint32_t)(cell.y + 1));
sb_ch(&sb, 'M');
} else if (m->mouse_encoding == TERM_MOUSE_ENC_UTF8) {
- const unsigned values[3] = {
- (unsigned)legacy_code + 32, (unsigned)cell.x + 33,
- (unsigned)cell.y + 33,
- };
sb_bytes(&sb, "\033[M", 3);
- for (int i = 0; i < 3; i++) {
- char tmp[8];
- size_t n = (size_t)utf8_encode(values[i], tmp);
- sb_bytes(&sb, tmp, n);
- }
+ char tmp[8];
+ size_t n;
+ n = (size_t)utf8_encode((unsigned)legacy_code + 32, tmp);
+ sb_bytes(&sb, tmp, n);
+ n = (size_t)utf8_encode((unsigned)cell.x + 33, tmp);
+ sb_bytes(&sb, tmp, n);
+ n = (size_t)utf8_encode((unsigned)cell.y + 33, tmp);
+ sb_bytes(&sb, tmp, n);
} else {
if (legacy_code > 223 || cell.x > 222 || cell.y > 222)
return out;