summaryrefslogtreecommitdiff
path: root/src/common/utf8.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/utf8.cpp')
-rw-r--r--src/common/utf8.cpp459
1 files changed, 0 insertions, 459 deletions
diff --git a/src/common/utf8.cpp b/src/common/utf8.cpp
deleted file mode 100644
index 56609634c..000000000
--- a/src/common/utf8.cpp
+++ /dev/null
@@ -1,459 +0,0 @@
1/*
2 Basic UTF-8 manipulation routines
3 by Jeff Bezanson
4 placed in the public domain Fall 2005
5
6 This code is designed to provide the utilities you need to manipulate
7 UTF-8 as an internal string encoding. These functions do not perform the
8 error checking normally needed when handling UTF-8 data, so if you happen
9 to be from the Unicode Consortium you will want to flay me alive.
10 I do this because error checking can be performed at the boundaries (I/O),
11 with these routines reserved for higher performance on data known to be
12 valid.
13*/
14
15#ifdef _WIN32
16#include <windows.h>
17#undef min
18#undef max
19#endif
20
21#include <cstdlib>
22#include <cstring>
23#include <algorithm>
24
25#include "common/common_types.h"
26#include "common/utf8.h"
27
28// is start of UTF sequence
29inline bool isutf(char c) {
30 return (c & 0xC0) != 0x80;
31}
32
33static const u32 offsetsFromUTF8[6] = {
34 0x00000000UL, 0x00003080UL, 0x000E2080UL,
35 0x03C82080UL, 0xFA082080UL, 0x82082080UL
36};
37
38static const u8 trailingBytesForUTF8[256] = {
39 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
40 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
41 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
42 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
43 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
44 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
45 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
46 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5,
47};
48
49/* returns length of next utf-8 sequence */
50int u8_seqlen(const char *s)
51{
52 return trailingBytesForUTF8[(unsigned int)(unsigned char)s[0]] + 1;
53}
54
55/* conversions without error checking
56 only works for valid UTF-8, i.e. no 5- or 6-byte sequences
57 srcsz = source size in bytes, or -1 if 0-terminated
58 sz = dest size in # of wide characters
59
60 returns # characters converted
61 dest will always be L'\0'-terminated, even if there isn't enough room
62 for all the characters.
63 if sz = srcsz+1 (i.e. 4*srcsz+4 bytes), there will always be enough space.
64*/
65int u8_toucs(u32 *dest, int sz, const char *src, int srcsz)
66{
67 u32 ch;
68 const char *src_end = src + srcsz;
69 int nb;
70 int i=0;
71
72 while (i < sz-1) {
73 nb = trailingBytesForUTF8[(unsigned char)*src];
74 if (srcsz == -1) {
75 if (*src == 0)
76 goto done_toucs;
77 }
78 else {
79 if (src + nb >= src_end)
80 goto done_toucs;
81 }
82 ch = 0;
83 switch (nb) {
84 /* these fall through deliberately */
85 case 3: ch += (unsigned char)*src++; ch <<= 6;
86 case 2: ch += (unsigned char)*src++; ch <<= 6;
87 case 1: ch += (unsigned char)*src++; ch <<= 6;
88 case 0: ch += (unsigned char)*src++;
89 }
90 ch -= offsetsFromUTF8[nb];
91 dest[i++] = ch;
92 }
93 done_toucs:
94 dest[i] = 0;
95 return i;
96}
97
98/* srcsz = number of source characters, or -1 if 0-terminated
99 sz = size of dest buffer in bytes
100
101 returns # characters converted
102 dest will only be '\0'-terminated if there is enough space. this is
103 for consistency; imagine there are 2 bytes of space left, but the next
104 character requires 3 bytes. in this case we could NUL-terminate, but in
105 general we can't when there's insufficient space. therefore this function
106 only NUL-terminates if all the characters fit, and there's space for
107 the NUL as well.
108 the destination string will never be bigger than the source string.
109*/
110int u8_toutf8(char *dest, int sz, u32 *src, int srcsz)
111{
112 u32 ch;
113 int i = 0;
114 char *dest_end = dest + sz;
115
116 while (srcsz<0 ? src[i]!=0 : i < srcsz) {
117 ch = src[i];
118 if (ch < 0x80) {
119 if (dest >= dest_end)
120 return i;
121 *dest++ = (char)ch;
122 }
123 else if (ch < 0x800) {
124 if (dest >= dest_end-1)
125 return i;
126 *dest++ = (ch>>6) | 0xC0;
127 *dest++ = (ch & 0x3F) | 0x80;
128 }
129 else if (ch < 0x10000) {
130 if (dest >= dest_end-2)
131 return i;
132 *dest++ = (ch>>12) | 0xE0;
133 *dest++ = ((ch>>6) & 0x3F) | 0x80;
134 *dest++ = (ch & 0x3F) | 0x80;
135 }
136 else if (ch < 0x110000) {
137 if (dest >= dest_end-3)
138 return i;
139 *dest++ = (ch>>18) | 0xF0;
140 *dest++ = ((ch>>12) & 0x3F) | 0x80;
141 *dest++ = ((ch>>6) & 0x3F) | 0x80;
142 *dest++ = (ch & 0x3F) | 0x80;
143 }
144 i++;
145 }
146 if (dest < dest_end)
147 *dest = '\0';
148 return i;
149}
150
151int u8_wc_toutf8(char *dest, u32 ch)
152{
153 if (ch < 0x80) {
154 dest[0] = (char)ch;
155 return 1;
156 }
157 if (ch < 0x800) {
158 dest[0] = (ch>>6) | 0xC0;
159 dest[1] = (ch & 0x3F) | 0x80;
160 return 2;
161 }
162 if (ch < 0x10000) {
163 dest[0] = (ch>>12) | 0xE0;
164 dest[1] = ((ch>>6) & 0x3F) | 0x80;
165 dest[2] = (ch & 0x3F) | 0x80;
166 return 3;
167 }
168 if (ch < 0x110000) {
169 dest[0] = (ch>>18) | 0xF0;
170 dest[1] = ((ch>>12) & 0x3F) | 0x80;
171 dest[2] = ((ch>>6) & 0x3F) | 0x80;
172 dest[3] = (ch & 0x3F) | 0x80;
173 return 4;
174 }
175 return 0;
176}
177
178/* charnum => byte offset */
179int u8_offset(const char *str, int charnum)
180{
181 int offs=0;
182
183 while (charnum > 0 && str[offs]) {
184 (void)(isutf(str[++offs]) || isutf(str[++offs]) ||
185 isutf(str[++offs]) || ++offs);
186 charnum--;
187 }
188 return offs;
189}
190
191/* byte offset => charnum */
192int u8_charnum(const char *s, int offset)
193{
194 int charnum = 0, offs=0;
195
196 while (offs < offset && s[offs]) {
197 (void)(isutf(s[++offs]) || isutf(s[++offs]) ||
198 isutf(s[++offs]) || ++offs);
199 charnum++;
200 }
201 return charnum;
202}
203
204/* number of characters */
205int u8_strlen(const char *s)
206{
207 int count = 0;
208 int i = 0;
209
210 while (u8_nextchar(s, &i) != 0)
211 count++;
212
213 return count;
214}
215
216/* reads the next utf-8 sequence out of a string, updating an index */
217u32 u8_nextchar(const char *s, int *i)
218{
219 u32 ch = 0;
220 int sz = 0;
221
222 do {
223 ch <<= 6;
224 ch += (unsigned char)s[(*i)++];
225 sz++;
226 } while (s[*i] && !isutf(s[*i]));
227 ch -= offsetsFromUTF8[sz-1];
228
229 return ch;
230}
231
232void u8_inc(const char *s, int *i)
233{
234 (void)(isutf(s[++(*i)]) || isutf(s[++(*i)]) ||
235 isutf(s[++(*i)]) || ++(*i));
236}
237
238void u8_dec(const char *s, int *i)
239{
240 (void)(isutf(s[--(*i)]) || isutf(s[--(*i)]) ||
241 isutf(s[--(*i)]) || --(*i));
242}
243
244int octal_digit(char c)
245{
246 return (c >= '0' && c <= '7');
247}
248
249int hex_digit(char c)
250{
251 return ((c >= '0' && c <= '9') ||
252 (c >= 'A' && c <= 'F') ||
253 (c >= 'a' && c <= 'f'));
254}
255
256/* assumes that src points to the character after a backslash
257 returns number of input characters processed */
258int u8_read_escape_sequence(const char *str, u32 *dest)
259{
260 u32 ch;
261 char digs[9]="\0\0\0\0\0\0\0\0";
262 int dno=0, i=1;
263
264 ch = (u32)str[0]; /* take literal character */
265 if (str[0] == 'n')
266 ch = L'\n';
267 else if (str[0] == 't')
268 ch = L'\t';
269 else if (str[0] == 'r')
270 ch = L'\r';
271 else if (str[0] == 'b')
272 ch = L'\b';
273 else if (str[0] == 'f')
274 ch = L'\f';
275 else if (str[0] == 'v')
276 ch = L'\v';
277 else if (str[0] == 'a')
278 ch = L'\a';
279 else if (octal_digit(str[0])) {
280 i = 0;
281 do {
282 digs[dno++] = str[i++];
283 } while (octal_digit(str[i]) && dno < 3);
284 ch = strtol(digs, nullptr, 8);
285 }
286 else if (str[0] == 'x') {
287 while (hex_digit(str[i]) && dno < 2) {
288 digs[dno++] = str[i++];
289 }
290 if (dno > 0)
291 ch = strtol(digs, nullptr, 16);
292 }
293 else if (str[0] == 'u') {
294 while (hex_digit(str[i]) && dno < 4) {
295 digs[dno++] = str[i++];
296 }
297 if (dno > 0)
298 ch = strtol(digs, nullptr, 16);
299 }
300 else if (str[0] == 'U') {
301 while (hex_digit(str[i]) && dno < 8) {
302 digs[dno++] = str[i++];
303 }
304 if (dno > 0)
305 ch = strtol(digs, nullptr, 16);
306 }
307 *dest = ch;
308
309 return i;
310}
311
312/* convert a string with literal \uxxxx or \Uxxxxxxxx characters to UTF-8
313 example: u8_unescape(mybuf, 256, "hello\\u220e")
314 note the double backslash is needed if called on a C string literal */
315int u8_unescape(char *buf, int sz, char *src)
316{
317 int c=0, amt;
318 u32 ch;
319 char temp[4];
320
321 while (*src && c < sz) {
322 if (*src == '\\') {
323 src++;
324 amt = u8_read_escape_sequence(src, &ch);
325 }
326 else {
327 ch = (u32)*src;
328 amt = 1;
329 }
330 src += amt;
331 amt = u8_wc_toutf8(temp, ch);
332 if (amt > sz-c)
333 break;
334 memcpy(&buf[c], temp, amt);
335 c += amt;
336 }
337 if (c < sz)
338 buf[c] = '\0';
339 return c;
340}
341
342const char *u8_strchr(const char *s, u32 ch, int *charn)
343{
344 int i = 0, lasti=0;
345 u32 c;
346
347 *charn = 0;
348 while (s[i]) {
349 c = u8_nextchar(s, &i);
350 if (c == ch) {
351 return &s[lasti];
352 }
353 lasti = i;
354 (*charn)++;
355 }
356 return nullptr;
357}
358
359const char *u8_memchr(const char *s, u32 ch, size_t sz, int *charn)
360{
361 u32 i = 0, lasti=0;
362 u32 c;
363 int csz;
364
365 *charn = 0;
366 while (i < sz) {
367 c = csz = 0;
368 do {
369 c <<= 6;
370 c += (unsigned char)s[i++];
371 csz++;
372 } while (i < sz && !isutf(s[i]));
373 c -= offsetsFromUTF8[csz-1];
374
375 if (c == ch) {
376 return &s[lasti];
377 }
378 lasti = i;
379 (*charn)++;
380 }
381 return nullptr;
382}
383
384int u8_is_locale_utf8(const char *locale)
385{
386 /* this code based on libutf8 */
387 const char* cp = locale;
388
389 for (; *cp != '\0' && *cp != '@' && *cp != '+' && *cp != ','; cp++) {
390 if (*cp == '.') {
391 const char* encoding = ++cp;
392 for (; *cp != '\0' && *cp != '@' && *cp != '+' && *cp != ','; cp++)
393 ;
394 if ((cp-encoding == 5 && !strncmp(encoding, "UTF-8", 5))
395 || (cp-encoding == 4 && !strncmp(encoding, "utf8", 4)))
396 return 1; /* it's UTF-8 */
397 break;
398 }
399 }
400 return 0;
401}
402
403int UTF8StringNonASCIICount(const char *utf8string) {
404 UTF8 utf(utf8string);
405 int count = 0;
406 while (!utf.end()) {
407 int c = utf.next();
408 if (c > 127)
409 ++count;
410 }
411 return count;
412}
413
414bool UTF8StringHasNonASCII(const char *utf8string) {
415 return UTF8StringNonASCIICount(utf8string) > 0;
416}
417
418#ifdef _WIN32
419
420std::string ConvertWStringToUTF8(const wchar_t *wstr) {
421 int len = (int)wcslen(wstr);
422 int size = (int)WideCharToMultiByte(CP_UTF8, 0, wstr, len, 0, 0, nullptr, nullptr);
423 std::string s;
424 s.resize(size);
425 if (size > 0) {
426 WideCharToMultiByte(CP_UTF8, 0, wstr, len, &s[0], size, nullptr, nullptr);
427 }
428 return s;
429}
430
431std::string ConvertWStringToUTF8(const std::wstring &wstr) {
432 int len = (int)wstr.size();
433 int size = (int)WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), len, 0, 0, nullptr, nullptr);
434 std::string s;
435 s.resize(size);
436 if (size > 0) {
437 WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), len, &s[0], size, nullptr, nullptr);
438 }
439 return s;
440}
441
442void ConvertUTF8ToWString(wchar_t *dest, size_t destSize, const std::string &source) {
443 int len = (int)source.size();
444 int size = (int)MultiByteToWideChar(CP_UTF8, 0, source.c_str(), len, nullptr, 0);
445 MultiByteToWideChar(CP_UTF8, 0, source.c_str(), len, dest, std::min((int)destSize, size));
446}
447
448std::wstring ConvertUTF8ToWString(const std::string &source) {
449 int len = (int)source.size();
450 int size = (int)MultiByteToWideChar(CP_UTF8, 0, source.c_str(), len, nullptr, 0);
451 std::wstring str;
452 str.resize(size);
453 if (size > 0) {
454 MultiByteToWideChar(CP_UTF8, 0, source.c_str(), len, &str[0], size);
455 }
456 return str;
457}
458
459#endif