From 43fbffc232679ddd286f7c527755d15872c73d01 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 21 Mar 2024 11:55:49 -0400 Subject: bfstd: Add an ASCII fast path to xstrwidth() --- src/bfstd.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/bfstd.c b/src/bfstd.c index 43513b8..12af438 100644 --- a/src/bfstd.c +++ b/src/bfstd.c @@ -728,8 +728,17 @@ size_t xstrwidth(const char *str) { size_t len = strlen(str); size_t ret = 0; + size_t asclen = asciinlen(str, len); + size_t i; + for (i = 0; i < asclen; ++i) { + // Assume all ASCII printables have width 1 + if (xisprint(str[i])) { + ++ret; + } + } + mbstate_t mb = {0}; - for (size_t i = 0; i < len;) { + while (i < len) { wint_t wc = xmbrtowc(str, &i, len, &mb); if (wc == WEOF) { // Assume a single-width '?' -- cgit v1.2.3