diff options
-rw-r--r-- | src/bfstd.c | 4 | ||||
-rw-r--r-- | tests/bfstd.c | 15 |
2 files changed, 17 insertions, 2 deletions
diff --git a/src/bfstd.c b/src/bfstd.c index 2d9f60a..61d2fee 100644 --- a/src/bfstd.c +++ b/src/bfstd.c @@ -666,9 +666,9 @@ static size_t printable_len(const char *str, size_t len, enum wesc_flags flags) multibyte: memset(&mb, 0, sizeof(mb)); - while (i < len) { + for (size_t j = i; i < len; i = j) { wchar_t wc; - if (xmbrtowc(&wc, &i, str, len, &mb) != 0) { + if (xmbrtowc(&wc, &j, str, len, &mb) != 0) { break; } if (!xiswprint(wc, flags)) { diff --git a/tests/bfstd.c b/tests/bfstd.c index 2db084a..83964e5 100644 --- a/tests/bfstd.c +++ b/tests/bfstd.c @@ -5,6 +5,8 @@ #include "../src/config.h" #include "../src/diag.h" #include <errno.h> +#include <langinfo.h> +#include <locale.h> #include <stdio.h> #include <stdint.h> #include <stdlib.h> @@ -33,6 +35,11 @@ static void check_wordesc(const char *str, const char *exp, enum wesc_flags flag } int main(void) { + // Try to set a UTF-8 locale + if (!setlocale(LC_ALL, "C.UTF-8")) { + setlocale(LC_ALL, ""); + } + // From man 3p basename check_base_dir("usr", ".", "usr"); check_base_dir("usr/", ".", "usr"); @@ -54,5 +61,13 @@ int main(void) { check_wordesc("\033[1mbold's\033[0m", "$'\\e[1mbold\\'s\\e[0m'", WESC_SHELL | WESC_TTY); check_wordesc("\x7F", "$'\\x7F'", WESC_SHELL | WESC_TTY); + const char *charmap = nl_langinfo(CODESET); + if (strcmp(charmap, "UTF-8") == 0) { + check_wordesc("\xF0", "$'\\xF0'", WESC_SHELL | WESC_TTY); + check_wordesc("\xF0\x9F", "$'\\xF0\\x9F'", WESC_SHELL | WESC_TTY); + check_wordesc("\xF0\x9F\x98", "$'\\xF0\\x9F\\x98'", WESC_SHELL | WESC_TTY); + check_wordesc("\xF0\x9F\x98\x80", "\xF0\x9F\x98\x80", WESC_SHELL | WESC_TTY); + } + return EXIT_SUCCESS; } |