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