diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2024-03-21 11:55:49 -0400 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2024-03-21 11:55:49 -0400 |
commit | 43fbffc232679ddd286f7c527755d15872c73d01 (patch) | |
tree | 4b44f07a5f48f67551501d417753d9385debc9f9 /src/bfstd.c | |
parent | a2a6ac8edd5d85398f6edb6afd02e683d9da9e7b (diff) | |
download | bfs-43fbffc232679ddd286f7c527755d15872c73d01.tar.xz |
bfstd: Add an ASCII fast path to xstrwidth()
Diffstat (limited to 'src/bfstd.c')
-rw-r--r-- | src/bfstd.c | 11 |
1 files changed, 10 insertions, 1 deletions
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 '?' |