-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathutf8.cpp
More file actions
368 lines (318 loc) · 10.5 KB
/
utf8.cpp
File metadata and controls
368 lines (318 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2016 - 2017, 2019 - 2021, 2023, Gothenburg Bit Factory.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// https://opensource.org/license/mit
//
////////////////////////////////////////////////////////////////////////////////
#include <utf8.h>
#include <wcwidth.h>
////////////////////////////////////////////////////////////////////////////////
// Converts '0' -> 0
// '9' -> 9
// 'a'/'A' -> 10
// 'f'/'F' -> 15
#define XDIGIT(x) ((x) >= '0' && (x) <= '9' ? ((x) - '0') : \
(x) >= 'a' && (x) <= 'f' ? ((x) + 10 - 'a') : \
(x) >= 'A' && (x) <= 'F' ? ((x) + 10 - 'A') : 0)
////////////////////////////////////////////////////////////////////////////////
// Note: Assumes 4-digit hex codepoints:
// xxxx
// \uxxxx
// U+xxxx
unsigned int utf8_codepoint (const std::string& input)
{
unsigned int codepoint = 0;
int length = input.length ();
// U+xxxx, \uxxxx
if (length >= 6 &&
((input[0] == 'U' && input[1] == '+') ||
(input[0] == '\\' && input[1] == 'u')))
{
codepoint = XDIGIT (input[2]) << 12 |
XDIGIT (input[3]) << 8 |
XDIGIT (input[4]) << 4 |
XDIGIT (input[5]);
}
else if (length >= 4)
{
codepoint = XDIGIT (input[0]) << 12 |
XDIGIT (input[1]) << 8 |
XDIGIT (input[2]) << 4 |
XDIGIT (input[3]);
}
return codepoint;
}
////////////////////////////////////////////////////////////////////////////////
// Iterates along a UTF8 string.
// - argument i counts bytes advanced through the string
// - returns the next character
unsigned int utf8_next_char (const std::string& input, std::string::size_type& i)
{
// Try to recognize end of string without checking length on every call.
if (input[i] == '\0') {
// If this is not the end of the string, then this is a NUL character
// embedded in the string, so advance over it.
if (i != input.length()) {
i += 1;
}
return 0;
}
// How many bytes in the sequence?
int length = utf8_sequence (input[i]);
i += length;
// 0xxxxxxx -> 0xxxxxxx
if (length == 1)
return input[i - 1];
// 110yyyyy 10xxxxxx -> 00000yyy yyxxxxxx
if (length == 2)
return ((input[i - 2] & 0x1F) << 6) +
(input[i - 1] & 0x3F);
// 1110zzzz 10yyyyyy 10xxxxxx -> zzzzyyyy yyxxxxxx
if (length == 3)
return ((input[i - 3] & 0xF) << 12) +
((input[i - 2] & 0x3F) << 6) +
(input[i - 1] & 0x3F);
// 11110www 10zzzzzz 10yyyyyy 10xxxxxx -> 000wwwzz zzzzyyyy yyxxxxxx
if (length == 4)
return ((input[i - 4] & 0x7) << 18) +
((input[i - 3] & 0x3F) << 12) +
((input[i - 2] & 0x3F) << 6) +
(input[i - 1] & 0x3F);
// Default: pretend as though it's a single character.
// TODO Or should this throw?
return input[i - 1];
}
////////////////////////////////////////////////////////////////////////////////
// http://en.wikipedia.org/wiki/UTF-8
std::string utf8_character (unsigned int codepoint)
{
char sequence[5] {};
// 0xxxxxxx -> 0xxxxxxx
if (codepoint < 0x80)
{
sequence[0] = codepoint;
}
// 00000yyy yyxxxxxx -> 110yyyyy 10xxxxxx
else if (codepoint < 0x800)
{
sequence[0] = 0xC0 | (codepoint & 0x7C0) >> 6;
sequence[1] = 0x80 | (codepoint & 0x3F);
}
// zzzzyyyy yyxxxxxx -> 1110zzzz 10yyyyyy 10xxxxxx
else if (codepoint < 0x10000)
{
sequence[0] = 0xE0 | (codepoint & 0xF000) >> 12;
sequence[1] = 0x80 | (codepoint & 0xFC0) >> 6;
sequence[2] = 0x80 | (codepoint & 0x3F);
}
// 000wwwzz zzzzyyyy yyxxxxxx -> 11110www 10zzzzzz 10yyyyyy 10xxxxxx
else if (codepoint < 0x110000)
{
sequence[0] = 0xF0 | (codepoint & 0x1C0000) >> 18;
sequence[1] = 0x80 | (codepoint & 0x03F000) >> 12;
sequence[2] = 0x80 | (codepoint & 0x0FC0) >> 6;
sequence[3] = 0x80 | (codepoint & 0x3F);
}
return std::string (sequence);
}
////////////////////////////////////////////////////////////////////////////////
int utf8_sequence (unsigned int character)
{
if ((character & 0xE0) == 0xC0)
return 2;
if ((character & 0xF0) == 0xE0)
return 3;
if ((character & 0xF8) == 0xF0)
return 4;
return 1;
}
////////////////////////////////////////////////////////////////////////////////
// Length of a string in characters.
unsigned int utf8_length (const std::string& str)
{
int byteLength = str.length ();
int charLength = byteLength;
const char* data = str.data ();
// Decrement the number of bytes for each byte that matches 0b10??????
// this way only the first byte of any utf8 sequence is counted.
for (int i = 0; i < byteLength; i++)
{
// Extract the first two bits and check whether they are 10
if ((data[i] & 0xC0) == 0x80)
charLength--;
}
return charLength;
}
////////////////////////////////////////////////////////////////////////////////
// Width of a string in character cells.
unsigned int utf8_width (const std::string& str)
{
unsigned int length = 0;
std::string::size_type i = 0;
unsigned int c;
while ((c = utf8_next_char (str, i)))
{
// Control characters, and more especially newline characters, make
// mk_wcwidth() return -1. Ignore that, thereby "adding zero" to length.
// Since control characters are not displayed in reports, this is a valid
// choice.
int l = mk_wcwidth (c);
if (l != -1)
length += l;
}
return length;
}
////////////////////////////////////////////////////////////////////////////////
unsigned int utf8_text_length (const std::string& str)
{
int byteLength = str.length ();
int charLength = byteLength;
const char* data = str.data ();
bool in_color = false;
// Decrement the number of bytes for each byte that matches 0b10??????
// this way only the first byte of any utf8 sequence is counted.
for (int i = 0; i < byteLength; i++)
{
if (in_color)
{
if (data[i] == 'm')
in_color = false;
--charLength;
}
else
{
if (data[i] == 033)
{
in_color = true;
--charLength;
}
else
{
// Extract the first two bits and check whether they are 10
if ((data[i] & 0xC0) == 0x80)
--charLength;
}
}
}
return charLength;
}
////////////////////////////////////////////////////////////////////////////////
unsigned int utf8_text_width (const std::string& str)
{
bool in_color = false;
unsigned int length = 0;
std::string::size_type i = 0;
unsigned int c;
while ((c = utf8_next_char (str, i)))
{
if (in_color)
{
if (c == 'm')
in_color = false;
}
else if (c == 033)
{
in_color = true;
}
else
length += mk_wcwidth (c);
}
return length;
}
////////////////////////////////////////////////////////////////////////////////
std::string utf8_substr (
const std::string& input,
unsigned int start,
unsigned int length /* = 0 */)
{
// Find the starting index.
std::string::size_type index_start = 0;
for (unsigned int i = 0; i < start; i++)
utf8_next_char (input, index_start);
std::string result;
if (length)
{
std::string::size_type index_end = index_start;
for (unsigned int i = 0; i < length; i++)
utf8_next_char (input, index_end);
result = input.substr (index_start, index_end - index_start);
}
else
result = input.substr (index_start);
return result;
}
////////////////////////////////////////////////////////////////////////////////
// Truncate a UTF-8 string to fit within a target display width.
// Unlike substr which counts characters, this counts display columns.
const std::string utf8_truncate_to_width (
const std::string& input,
unsigned int target_width)
{
unsigned int current_width = 0;
std::string::size_type i = 0;
std::string::size_type last_safe = 0;
unsigned int c;
while ((c = utf8_next_char (input, i)))
{
int w = mk_wcwidth (c);
if (w < 0) w = 0;
if (current_width + w > target_width)
break;
current_width += w;
last_safe = i;
}
return input.substr (0, last_safe);
}
////////////////////////////////////////////////////////////////////////////////
int mk_wcwidth(wchar_t ucs)
{
int width = widechar_wcwidth (ucs);
if (width >= 0)
return width;
// Interpret widened characters as 2 chars
if (width == widechar_widened_in_9)
return 2;
// Interpret ambiguous east-asian characters as 1 char
// This includes accented characters like á or é
if (width == widechar_ambiguous)
return 1;
// Interpret Private Use Area (PUA) characters as 1 char for Nerd Fonts
if (width == widechar_private_use)
return 1;
// Emoji pictographs (U+1F300+) — width 2 in modern terminals
if ((ucs >= 0x1F300 && ucs <= 0x1F9FF) || // Misc Symbols, Pictographs, Emoticons, Supplemental
(ucs >= 0x1FA00 && ucs <= 0x1FAFF)) // Symbols and Pictographs Extended-A
return 2;
// Common symbols — width 1 in modern terminals
if ((ucs >= 0x2190 && ucs <= 0x21FF) || // Arrows
(ucs >= 0x2300 && ucs <= 0x23FF) || // Misc Technical
(ucs >= 0x25A0 && ucs <= 0x25FF) || // Geometric Shapes
(ucs >= 0x2600 && ucs <= 0x26FF) || // Misc Symbols
(ucs >= 0x2700 && ucs <= 0x27BF)) // Dingbats
return 1;
// Variation selectors — zero width
if (ucs >= 0xFE00 && ucs <= 0xFE0F)
return 0;
// All other negative values
return 0;
}
////////////////////////////////////////////////////////////////////////////////