xref: /openbsd-src/usr.bin/tmux/utf8-combined.c (revision 2af4974031c8b473886d033106a56ef901b1ac30)
1*2af49740Snicm /* $OpenBSD: utf8-combined.c,v 1.3 2023/09/15 15:49:05 nicm Exp $ */
2ecd3a22eSnicm 
3ecd3a22eSnicm /*
4ecd3a22eSnicm  * Copyright (c) 2023 Nicholas Marriott <nicholas.marriott@gmail.com>
5ecd3a22eSnicm  *
6ecd3a22eSnicm  * Permission to use, copy, modify, and distribute this software for any
7ecd3a22eSnicm  * purpose with or without fee is hereby granted, provided that the above
8ecd3a22eSnicm  * copyright notice and this permission notice appear in all copies.
9ecd3a22eSnicm  *
10ecd3a22eSnicm  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11ecd3a22eSnicm  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12ecd3a22eSnicm  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13ecd3a22eSnicm  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14ecd3a22eSnicm  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15ecd3a22eSnicm  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16ecd3a22eSnicm  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17ecd3a22eSnicm  */
18ecd3a22eSnicm 
19ecd3a22eSnicm #include <sys/types.h>
20ecd3a22eSnicm 
21ecd3a22eSnicm #include <stdlib.h>
22ecd3a22eSnicm #include <string.h>
23ecd3a22eSnicm #include <wchar.h>
24ecd3a22eSnicm 
25ecd3a22eSnicm #include "tmux.h"
26ecd3a22eSnicm 
27*2af49740Snicm static const wchar_t utf8_modifier_table[] = {
28*2af49740Snicm 	0x1F1E6,
29*2af49740Snicm 	0x1F1E7,
30*2af49740Snicm 	0x1F1E8,
31*2af49740Snicm 	0x1F1E9,
32*2af49740Snicm 	0x1F1EA,
33*2af49740Snicm 	0x1F1EB,
34*2af49740Snicm 	0x1F1EC,
35*2af49740Snicm 	0x1F1ED,
36*2af49740Snicm 	0x1F1EE,
37*2af49740Snicm 	0x1F1EF,
38*2af49740Snicm 	0x1F1F0,
39*2af49740Snicm 	0x1F1F1,
40*2af49740Snicm 	0x1F1F2,
41*2af49740Snicm 	0x1F1F3,
42*2af49740Snicm 	0x1F1F4,
43*2af49740Snicm 	0x1F1F5,
44*2af49740Snicm 	0x1F1F6,
45*2af49740Snicm 	0x1F1F7,
46*2af49740Snicm 	0x1F1F8,
47*2af49740Snicm 	0x1F1F9,
48*2af49740Snicm 	0x1F1FA,
49*2af49740Snicm 	0x1F1FB,
50*2af49740Snicm 	0x1F1FC,
51*2af49740Snicm 	0x1F1FD,
52*2af49740Snicm 	0x1F1FE,
53*2af49740Snicm 	0x1F1FF,
54*2af49740Snicm 	0x1F3FB,
55*2af49740Snicm 	0x1F3FC,
56*2af49740Snicm 	0x1F3FD,
57*2af49740Snicm 	0x1F3FE,
58*2af49740Snicm 	0x1F3FF
59ecd3a22eSnicm };
60ecd3a22eSnicm 
61*2af49740Snicm /* Has this got a zero width joiner at the end? */
62*2af49740Snicm int
63*2af49740Snicm utf8_has_zwj(const struct utf8_data *ud)
64ecd3a22eSnicm {
65*2af49740Snicm 	if (ud->size < 3)
66*2af49740Snicm 		return (0);
67*2af49740Snicm 	return (memcmp(ud->data + ud->size - 3, "\342\200\215", 3) == 0);
68ecd3a22eSnicm }
69ecd3a22eSnicm 
70*2af49740Snicm /* Is this a zero width joiner? */
71*2af49740Snicm int
72ecd3a22eSnicm utf8_is_zwj(const struct utf8_data *ud)
73ecd3a22eSnicm {
74*2af49740Snicm 	if (ud->size != 3)
75*2af49740Snicm 		return (0);
76*2af49740Snicm 	return (memcmp(ud->data, "\342\200\215", 3) == 0);
77ecd3a22eSnicm }
78ecd3a22eSnicm 
79*2af49740Snicm /* Is this a variation selector? */
80ecd3a22eSnicm int
81*2af49740Snicm utf8_is_vs(const struct utf8_data *ud)
82ecd3a22eSnicm {
83*2af49740Snicm 	if (ud->size != 3)
84*2af49740Snicm 		return (0);
85*2af49740Snicm 	return (memcmp(ud->data, "\357\270\217", 3) == 0);
86ecd3a22eSnicm }
87ecd3a22eSnicm 
88*2af49740Snicm /* Is this in the modifier table? */
89*2af49740Snicm int
90*2af49740Snicm utf8_is_modifier(const struct utf8_data *ud)
91*2af49740Snicm {
92*2af49740Snicm 	wchar_t	wc;
93ecd3a22eSnicm 
94*2af49740Snicm 	if (utf8_towc(ud, &wc) != UTF8_DONE)
95*2af49740Snicm 		return (0);
96*2af49740Snicm 	if (!utf8_in_table(wc, utf8_modifier_table,
97*2af49740Snicm 	    nitems(utf8_modifier_table)))
98*2af49740Snicm 		return (0);
99*2af49740Snicm 	return (1);
100ecd3a22eSnicm }
101