summaryrefslogtreecommitdiffstats
path: root/src/bfstd.c
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2024-03-21 11:55:49 -0400
committerTavian Barnes <tavianator@tavianator.com>2024-03-21 11:55:49 -0400
commit43fbffc232679ddd286f7c527755d15872c73d01 (patch)
tree4b44f07a5f48f67551501d417753d9385debc9f9 /src/bfstd.c
parenta2a6ac8edd5d85398f6edb6afd02e683d9da9e7b (diff)
downloadbfs-43fbffc232679ddd286f7c527755d15872c73d01.tar.xz
bfstd: Add an ASCII fast path to xstrwidth()
Diffstat (limited to 'src/bfstd.c')
-rw-r--r--src/bfstd.c11
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 '?'