]> git.lizzy.rs Git - linenoise.git/blob - utf8.c
Guard inclusion of stringbuf.h
[linenoise.git] / utf8.c
1 /**
2  * UTF-8 utility functions
3  *
4  * (c) 2010-2016 Steve Bennett <steveb@workware.net.au>
5  *
6  * See LICENCE for licence details.
7  */
8
9 #include <ctype.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <stdio.h>
13 #include "utf8.h"
14
15 #ifdef USE_UTF8
16 int utf8_fromunicode(char *p, unsigned uc)
17 {
18     if (uc <= 0x7f) {
19         *p = uc;
20         return 1;
21     }
22     else if (uc <= 0x7ff) {
23         *p++ = 0xc0 | ((uc & 0x7c0) >> 6);
24         *p = 0x80 | (uc & 0x3f);
25         return 2;
26     }
27     else if (uc <= 0xffff) {
28         *p++ = 0xe0 | ((uc & 0xf000) >> 12);
29         *p++ = 0x80 | ((uc & 0xfc0) >> 6);
30         *p = 0x80 | (uc & 0x3f);
31         return 3;
32     }
33     /* Note: We silently truncate to 21 bits here: 0x1fffff */
34     else {
35         *p++ = 0xf0 | ((uc & 0x1c0000) >> 18);
36         *p++ = 0x80 | ((uc & 0x3f000) >> 12);
37         *p++ = 0x80 | ((uc & 0xfc0) >> 6);
38         *p = 0x80 | (uc & 0x3f);
39         return 4;
40     }
41 }
42
43 int utf8_charlen(int c)
44 {
45     if ((c & 0x80) == 0) {
46         return 1;
47     }
48     if ((c & 0xe0) == 0xc0) {
49         return 2;
50     }
51     if ((c & 0xf0) == 0xe0) {
52         return 3;
53     }
54     if ((c & 0xf8) == 0xf0) {
55         return 4;
56     }
57     /* Invalid sequence */
58     return -1;
59 }
60
61 int utf8_strlen(const char *str, int bytelen)
62 {
63     int charlen = 0;
64     if (bytelen < 0) {
65         bytelen = strlen(str);
66     }
67     while (bytelen > 0) {
68         int c;
69         int l = utf8_tounicode(str, &c);
70         charlen++;
71         str += l;
72         bytelen -= l;
73     }
74     return charlen;
75 }
76
77 int utf8_strwidth(const char *str, int charlen)
78 {
79     int width = 0;
80     while (charlen) {
81         int c;
82         int l = utf8_tounicode(str, &c);
83         width += utf8_width(c);
84         str += l;
85         charlen--;
86     }
87     return width;
88 }
89
90 int utf8_index(const char *str, int index)
91 {
92     const char *s = str;
93     while (index--) {
94         int c;
95         s += utf8_tounicode(s, &c);
96     }
97     return s - str;
98 }
99
100 int utf8_tounicode(const char *str, int *uc)
101 {
102     unsigned const char *s = (unsigned const char *)str;
103
104     if (s[0] < 0xc0) {
105         *uc = s[0];
106         return 1;
107     }
108     if (s[0] < 0xe0) {
109         if ((s[1] & 0xc0) == 0x80) {
110             *uc = ((s[0] & ~0xc0) << 6) | (s[1] & ~0x80);
111             if (*uc >= 0x80) {
112                 return 2;
113             }
114             /* Otherwise this is an invalid sequence */
115         }
116     }
117     else if (s[0] < 0xf0) {
118         if (((str[1] & 0xc0) == 0x80) && ((str[2] & 0xc0) == 0x80)) {
119             *uc = ((s[0] & ~0xe0) << 12) | ((s[1] & ~0x80) << 6) | (s[2] & ~0x80);
120             if (*uc >= 0x800) {
121                 return 3;
122             }
123             /* Otherwise this is an invalid sequence */
124         }
125     }
126     else if (s[0] < 0xf8) {
127         if (((str[1] & 0xc0) == 0x80) && ((str[2] & 0xc0) == 0x80) && ((str[3] & 0xc0) == 0x80)) {
128             *uc = ((s[0] & ~0xf0) << 18) | ((s[1] & ~0x80) << 12) | ((s[2] & ~0x80) << 6) | (s[3] & ~0x80);
129             if (*uc >= 0x10000) {
130                 return 4;
131             }
132             /* Otherwise this is an invalid sequence */
133         }
134     }
135
136     /* Invalid sequence, so just return the byte */
137     *uc = *s;
138     return 1;
139 }
140
141 struct utf8range {
142     int lower;     /* lower inclusive */
143     int upper;     /* upper exclusive */
144 };
145
146 /* From http://unicode.org/Public/UNIDATA/UnicodeData.txt */
147 static const struct utf8range unicode_range_combining[] = {
148         { 0x0300, 0x0370 },     { 0x0483, 0x048a },     { 0x0591, 0x05d0 },     { 0x0610, 0x061b },
149         { 0x064b, 0x0660 },     { 0x0670, 0x0671 },     { 0x06d6, 0x06dd },     { 0x06df, 0x06e5 },
150         { 0x06e7, 0x06ee },     { 0x0711, 0x0712 },     { 0x0730, 0x074d },     { 0x07a6, 0x07b1 },
151         { 0x07eb, 0x07f4 },     { 0x0816, 0x0830 },     { 0x0859, 0x085e },     { 0x08d4, 0x0904 },
152         { 0x093a, 0x0958 },     { 0x0962, 0x0964 },     { 0x0981, 0x0985 },     { 0x09bc, 0x09ce },
153         { 0x09d7, 0x09dc },     { 0x09e2, 0x09e6 },     { 0x0a01, 0x0a05 },     { 0x0a3c, 0x0a59 },
154         { 0x0a70, 0x0a72 },     { 0x0a75, 0x0a85 },     { 0x0abc, 0x0ad0 },     { 0x0ae2, 0x0ae6 },
155         { 0x0afa, 0x0b05 },     { 0x0b3c, 0x0b5c },     { 0x0b62, 0x0b66 },     { 0x0b82, 0x0b83 },
156         { 0x0bbe, 0x0bd0 },     { 0x0bd7, 0x0be6 },     { 0x0c00, 0x0c05 },     { 0x0c3e, 0x0c58 },
157         { 0x0c62, 0x0c66 },     { 0x0c81, 0x0c85 },     { 0x0cbc, 0x0cde },     { 0x0ce2, 0x0ce6 },
158         { 0x0d00, 0x0d05 },     { 0x0d3b, 0x0d4e },     { 0x0d57, 0x0d58 },     { 0x0d62, 0x0d66 },
159         { 0x0d82, 0x0d85 },     { 0x0dca, 0x0de6 },     { 0x0df2, 0x0df4 },     { 0x0e31, 0x0e32 },
160         { 0x0e34, 0x0e3f },     { 0x0e47, 0x0e4f },     { 0x0eb1, 0x0eb2 },     { 0x0eb4, 0x0ebd },
161         { 0x0ec8, 0x0ed0 },     { 0x0f18, 0x0f1a },     { 0x0f35, 0x0f3a },     { 0x0f3e, 0x0f40 },
162         { 0x0f71, 0x0f88 },     { 0x0f8d, 0x0fbe },     { 0x0fc6, 0x0fc7 },     { 0x102b, 0x103f },
163         { 0x1056, 0x105a },     { 0x105e, 0x1065 },     { 0x1067, 0x106e },     { 0x1071, 0x1075 },
164         { 0x1082, 0x1090 },     { 0x109a, 0x109e },     { 0x135d, 0x1360 },     { 0x1712, 0x1720 },
165         { 0x1732, 0x1735 },     { 0x1752, 0x1760 },     { 0x1772, 0x1780 },     { 0x17b4, 0x17d4 },
166         { 0x17dd, 0x17e0 },     { 0x180b, 0x180e },     { 0x1885, 0x1887 },     { 0x18a9, 0x18aa },
167         { 0x1920, 0x1940 },     { 0x1a17, 0x1a1e },     { 0x1a55, 0x1a80 },     { 0x1ab0, 0x1b05 },
168         { 0x1b34, 0x1b45 },     { 0x1b6b, 0x1b74 },     { 0x1b80, 0x1b83 },     { 0x1ba1, 0x1bae },
169         { 0x1be6, 0x1bfc },     { 0x1c24, 0x1c3b },     { 0x1cd0, 0x1ce9 },     { 0x1ced, 0x1cee },
170         { 0x1cf2, 0x1cf5 },     { 0x1cf7, 0x1d00 },     { 0x1dc0, 0x1e00 },     { 0x20d0, 0x2100 },
171         { 0x2cef, 0x2cf2 },     { 0x2d7f, 0x2d80 },     { 0x2de0, 0x2e00 },     { 0x302a, 0x3030 },
172         { 0x3099, 0x309b },     { 0xa66f, 0xa67e },     { 0xa69e, 0xa6a0 },     { 0xa6f0, 0xa6f2 },
173         { 0xa802, 0xa803 },     { 0xa806, 0xa807 },     { 0xa80b, 0xa80c },     { 0xa823, 0xa828 },
174         { 0xa880, 0xa882 },     { 0xa8b4, 0xa8ce },     { 0xa8e0, 0xa8f2 },     { 0xa926, 0xa92e },
175         { 0xa947, 0xa95f },     { 0xa980, 0xa984 },     { 0xa9b3, 0xa9c1 },     { 0xa9e5, 0xa9e6 },
176         { 0xaa29, 0xaa40 },     { 0xaa43, 0xaa44 },     { 0xaa4c, 0xaa50 },     { 0xaa7b, 0xaa7e },
177         { 0xaab0, 0xaab5 },     { 0xaab7, 0xaab9 },     { 0xaabe, 0xaac2 },     { 0xaaeb, 0xaaf0 },
178         { 0xaaf5, 0xab01 },     { 0xabe3, 0xabf0 },     { 0xfb1e, 0xfb1f },     { 0xfe00, 0xfe10 },
179         { 0xfe20, 0xfe30 },
180 };
181
182 /* From http://unicode.org/Public/UNIDATA/EastAsianWidth.txt */
183 static const struct utf8range unicode_range_wide[] = {
184         { 0x1100, 0x115f },     { 0x231a, 0x231b },     { 0x2329, 0x232a },     { 0x23e9, 0x23ec },
185         { 0x23f0, 0x23f0 },     { 0x23f3, 0x23f3 },     { 0x25fd, 0x25fe },     { 0x2614, 0x2615 },
186         { 0x2648, 0x2653 },     { 0x267f, 0x267f },     { 0x2693, 0x2693 },     { 0x26a1, 0x26a1 },
187         { 0x26aa, 0x26ab },     { 0x26bd, 0x26be },     { 0x26c4, 0x26c5 },     { 0x26ce, 0x26ce },
188         { 0x26d4, 0x26d4 },     { 0x26ea, 0x26ea },     { 0x26f2, 0x26f3 },     { 0x26f5, 0x26f5 },
189         { 0x26fa, 0x26fa },     { 0x26fd, 0x26fd },     { 0x2705, 0x2705 },     { 0x270a, 0x270b },
190         { 0x2728, 0x2728 },     { 0x274c, 0x274c },     { 0x274e, 0x274e },     { 0x2753, 0x2755 },
191         { 0x2757, 0x2757 },     { 0x2795, 0x2797 },     { 0x27b0, 0x27b0 },     { 0x27bf, 0x27bf },
192         { 0x2b1b, 0x2b1c },     { 0x2b50, 0x2b50 },     { 0x2b55, 0x2b55 },     { 0x2e80, 0x2e99 },
193         { 0x2e9b, 0x2ef3 },     { 0x2f00, 0x2fd5 },     { 0x2ff0, 0x2ffb },     { 0x3001, 0x303e },
194         { 0x3041, 0x3096 },     { 0x3099, 0x30ff },     { 0x3105, 0x312e },     { 0x3131, 0x318e },
195         { 0x3190, 0x31ba },     { 0x31c0, 0x31e3 },     { 0x31f0, 0x321e },     { 0x3220, 0x3247 },
196         { 0x3250, 0x32fe },     { 0x3300, 0x4dbf },     { 0x4e00, 0xa48c },     { 0xa490, 0xa4c6 },
197         { 0xa960, 0xa97c },     { 0xac00, 0xd7a3 },     { 0xf900, 0xfaff },     { 0xfe10, 0xfe19 },
198         { 0xfe30, 0xfe52 },     { 0xfe54, 0xfe66 },     { 0xfe68, 0xfe6b },     { 0x16fe0, 0x16fe1 },
199         { 0x17000, 0x187ec },   { 0x18800, 0x18af2 },   { 0x1b000, 0x1b11e },   { 0x1b170, 0x1b2fb },
200         { 0x1f004, 0x1f004 },   { 0x1f0cf, 0x1f0cf },   { 0x1f18e, 0x1f18e },   { 0x1f191, 0x1f19a },
201         { 0x1f200, 0x1f202 },   { 0x1f210, 0x1f23b },   { 0x1f240, 0x1f248 },   { 0x1f250, 0x1f251 },
202         { 0x1f260, 0x1f265 },   { 0x1f300, 0x1f320 },   { 0x1f32d, 0x1f335 },   { 0x1f337, 0x1f37c },
203         { 0x1f37e, 0x1f393 },   { 0x1f3a0, 0x1f3ca },   { 0x1f3cf, 0x1f3d3 },   { 0x1f3e0, 0x1f3f0 },
204         { 0x1f3f4, 0x1f3f4 },   { 0x1f3f8, 0x1f43e },   { 0x1f440, 0x1f440 },   { 0x1f442, 0x1f4fc },
205         { 0x1f4ff, 0x1f53d },   { 0x1f54b, 0x1f54e },   { 0x1f550, 0x1f567 },   { 0x1f57a, 0x1f57a },
206         { 0x1f595, 0x1f596 },   { 0x1f5a4, 0x1f5a4 },   { 0x1f5fb, 0x1f64f },   { 0x1f680, 0x1f6c5 },
207         { 0x1f6cc, 0x1f6cc },   { 0x1f6d0, 0x1f6d2 },   { 0x1f6eb, 0x1f6ec },   { 0x1f6f4, 0x1f6f8 },
208         { 0x1f910, 0x1f93e },   { 0x1f940, 0x1f94c },   { 0x1f950, 0x1f96b },   { 0x1f980, 0x1f997 },
209         { 0x1f9c0, 0x1f9c0 },   { 0x1f9d0, 0x1f9e6 },   { 0x20000, 0x2fffd },   { 0x30000, 0x3fffd },
210 };
211
212 #define ARRAYSIZE(A) sizeof(A) / sizeof(*(A))
213
214 static int cmp_range(const void *key, const void *cm)
215 {
216     const struct utf8range *range = (const struct utf8range *)cm;
217     int ch = *(int *)key;
218     if (ch < range->lower) {
219         return -1;
220     }
221     if (ch >= range->upper) {
222         return 1;
223     }
224     return 0;
225 }
226
227 static int utf8_in_range(const struct utf8range *range, int num, int ch)
228 {
229     const struct utf8range *r =
230         bsearch(&ch, range, num, sizeof(*range), cmp_range);
231
232     if (r) {
233         return 1;
234     }
235     return 0;
236 }
237
238 int utf8_width(int ch)
239 {
240     /* short circuit for common case */
241     if (isascii(ch)) {
242         return 1;
243     }
244     if (utf8_in_range(unicode_range_combining, ARRAYSIZE(unicode_range_combining), ch)) {
245         return 0;
246     }
247     if (utf8_in_range(unicode_range_wide, ARRAYSIZE(unicode_range_wide), ch)) {
248         return 2;
249     }
250     return 1;
251 }
252 #endif