If we know the terminal outside tmux is not UTF-8, replace UTF-8 in

error messages and whatnot with underscores the same as we do when we
draw UTF-8 characters as part of the screen.
This commit is contained in:
nicm
2015-11-12 11:10:50 +00:00
parent 1b86f520ea
commit c41673f3fa
4 changed files with 67 additions and 4 deletions

44
utf8.c
View File

@ -585,6 +585,50 @@ utf8_strvis(char *dst, const char *src, size_t len, int flag)
return (dst - start);
}
/*
* Sanitize a string, changing any UTF-8 characters to '_'. Caller should free
* the returned string. Anything not valid printable ASCII or UTF-8 is
* stripped.
*/
char *
utf8_sanitize(const char *src)
{
char *dst;
size_t n;
int more;
struct utf8_data utf8data;
u_int i;
dst = NULL;
n = 0;
while (*src != '\0') {
dst = xreallocarray(dst, n + 1, sizeof *dst);
if (utf8_open(&utf8data, *src)) {
more = 1;
while (*++src != '\0' && more)
more = utf8_append(&utf8data, *src);
if (!more) {
dst = xreallocarray(dst, n + utf8data.width,
sizeof *dst);
for (i = 0; i < utf8data.width; i++)
dst[n++] = '_';
continue;
}
src -= utf8data.have;
}
if (*src > 0x1f && *src < 0x7f)
dst[n] = *src;
src++;
n++;
}
dst = xreallocarray(dst, n + 1, sizeof *dst);
dst[n] = '\0';
return (dst);
}
/*
* Convert a string into a buffer of UTF-8 characters. Terminated by size == 0.
* Caller frees.