From 6c94774b70f72952c4c512e4aa59a207ca1c34f2 Mon Sep 17 00:00:00 2001 From: Nicholas Marriott Date: Thu, 1 Sep 2016 20:40:03 +0100 Subject: Add support for using utf8proc with --enable-utf8proc, useful for platforms (like OS X) where the system implementation is crap. From Joshua Rubin. --- compat/utf8proc.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 compat/utf8proc.c (limited to 'compat') 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 + * + * 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 + +#include + +#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)); +} -- cgit v1.2.3