xref: /netbsd-src/external/bsd/tmux/dist/compat/utf8proc.c (revision 4e179ddab96f97aa64efc589ce1918be5318aaaf)
1*4e179ddaSchristos /*
2*4e179ddaSchristos  * Copyright (c) 2016 Joshua Rubin <joshua@rubixconsulting.com>
3*4e179ddaSchristos  *
4*4e179ddaSchristos  * Permission to use, copy, modify, and distribute this software for any
5*4e179ddaSchristos  * purpose with or without fee is hereby granted, provided that the above
6*4e179ddaSchristos  * copyright notice and this permission notice appear in all copies.
7*4e179ddaSchristos  *
8*4e179ddaSchristos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9*4e179ddaSchristos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10*4e179ddaSchristos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11*4e179ddaSchristos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12*4e179ddaSchristos  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
13*4e179ddaSchristos  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
14*4e179ddaSchristos  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15*4e179ddaSchristos  */
16*4e179ddaSchristos 
17*4e179ddaSchristos #include <sys/types.h>
18*4e179ddaSchristos 
19*4e179ddaSchristos #include <utf8proc.h>
20*4e179ddaSchristos 
21*4e179ddaSchristos #include "compat.h"
22*4e179ddaSchristos 
23*4e179ddaSchristos int
utf8proc_wcwidth(wchar_t wc)24*4e179ddaSchristos utf8proc_wcwidth(wchar_t wc)
25*4e179ddaSchristos {
26*4e179ddaSchristos 	int	cat;
27*4e179ddaSchristos 
28*4e179ddaSchristos 	cat = utf8proc_category(wc);
29*4e179ddaSchristos 	if (cat == UTF8PROC_CATEGORY_CO) {
30*4e179ddaSchristos 		/*
31*4e179ddaSchristos 		 * The private use category is where powerline and similar
32*4e179ddaSchristos 		 * codepoints are stored, they have "ambiguous" width - use 1.
33*4e179ddaSchristos 		 */
34*4e179ddaSchristos 		return (1);
35*4e179ddaSchristos 	}
36*4e179ddaSchristos 	return (utf8proc_charwidth(wc));
37*4e179ddaSchristos }
38*4e179ddaSchristos 
39*4e179ddaSchristos int
utf8proc_mbtowc(wchar_t * pwc,const char * s,size_t n)40*4e179ddaSchristos utf8proc_mbtowc(wchar_t *pwc, const char *s, size_t n)
41*4e179ddaSchristos {
42*4e179ddaSchristos 	utf8proc_ssize_t	slen;
43*4e179ddaSchristos 
44*4e179ddaSchristos 	if (s == NULL)
45*4e179ddaSchristos 		return (0);
46*4e179ddaSchristos 
47*4e179ddaSchristos 	/*
48*4e179ddaSchristos 	 * *pwc == -1 indicates invalid codepoint
49*4e179ddaSchristos 	 * slen < 0 indicates an error
50*4e179ddaSchristos 	 */
51*4e179ddaSchristos 	slen = utf8proc_iterate(s, n, pwc);
52*4e179ddaSchristos 	if (*pwc == (wchar_t)-1 || slen < 0)
53*4e179ddaSchristos 		return (-1);
54*4e179ddaSchristos 	return (slen);
55*4e179ddaSchristos }
56*4e179ddaSchristos 
57*4e179ddaSchristos int
utf8proc_wctomb(char * s,wchar_t wc)58*4e179ddaSchristos utf8proc_wctomb(char *s, wchar_t wc)
59*4e179ddaSchristos {
60*4e179ddaSchristos 	if (s == NULL)
61*4e179ddaSchristos 		return (0);
62*4e179ddaSchristos 
63*4e179ddaSchristos 	if (!utf8proc_codepoint_valid(wc))
64*4e179ddaSchristos 		return (-1);
65*4e179ddaSchristos 	return (utf8proc_encode_char(wc, s));
66*4e179ddaSchristos }
67