summaryrefslogtreecommitdiffstats
path: root/utf8.c
diff options
context:
space:
mode:
authornicm <nicm>2015-11-12 11:10:50 +0000
committernicm <nicm>2015-11-12 11:10:50 +0000
commitc41673f3fae397e872214880bd005cce631cf11b (patch)
tree6f07266d52a6a1b21a213de354f14a59c13d420d /utf8.c
parent1b86f520ea1620628e569ea833c7b13306c18a4e (diff)
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.
Diffstat (limited to 'utf8.c')
-rw-r--r--utf8.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/utf8.c b/utf8.c
index e61bf996..cb20ea6d 100644
--- a/utf8.c
+++ b/utf8.c
@@ -586,6 +586,50 @@ utf8_strvis(char *dst, const char *src, size_t len, int flag)
}
/*
+ * 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.
*/