summaryrefslogtreecommitdiffstats
path: root/compat
diff options
context:
space:
mode:
authorNicholas Marriott <nicholas.marriott@gmail.com>2016-09-01 20:40:03 +0100
committerNicholas Marriott <nicholas.marriott@gmail.com>2016-09-01 20:40:03 +0100
commit6c94774b70f72952c4c512e4aa59a207ca1c34f2 (patch)
tree04502f9607958f3b4559e3bac5e5668e8d517ab0 /compat
parentae297cb487590d0bb8e42e21e28926a1f957ad0b (diff)
Add support for using utf8proc with --enable-utf8proc, useful for platforms
(like OS X) where the system implementation is crap. From Joshua Rubin.
Diffstat (limited to 'compat')
-rw-r--r--compat/utf8proc.c70
1 files changed, 70 insertions, 0 deletions
diff --git a/compat/utf8proc.c b/compat/utf8proc.c
new file mode 100644
index 00000000..023d762a
--- /dev/null
+++ b/compat/utf8proc.c
@@ -0,0 +1,70 @@
+/*
+ * Copyright (c) 2016 Joshua Rubin <joshua@rubixconsulting.com>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
+ * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
+ * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/types.h>
+
+#include <utf8proc.h>
+
+#include "tmux.h"
+
+int
+utf8proc_wcwidth(wchar_t wc)
+{
+ int cat;
+
+ cat = utf8proc_category(wc);
+ if (cat == UTF8PROC_CATEGORY_CO) {
+ /*
+ * The private use category is where powerline and similar
+ * codepoints are stored, they have "ambiguous" width - use 1.
+ */
+ return (1);
+ }
+ if (cat == UTF8PROC_CATEGORY_SO) {
+ /* Symbols, like emoji, should always use width 1. */
+ return (1);
+ }
+ return (utf8proc_charwidth(wc));
+}
+
+int
+utf8proc_mbtowc(wchar_t *pwc, const char *s, size_t n)
+{
+ utf8proc_ssize_t slen;
+
+ if (s == NULL)
+ return (0);
+
+ /*
+ * *pwc == -1 indicates invalid codepoint
+ * slen < 0 indicates an error
+ */
+ slen = utf8proc_iterate(s, n, pwc);
+ if (*pwc == (wchar_t)-1 || slen < 0)
+ return (-1);
+ return (slen);
+}
+
+int
+utf8proc_wctomb(char *s, wchar_t wc)
+{
+ if (s == NULL)
+ return (0);
+
+ if (!utf8proc_codepoint_valid(wc))
+ return (-1);
+ return (utf8proc_encode_char(wc, s));
+}