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 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
|
/* copyright (c) 2022 - 2026 grunfink et al. / MIT license */
#ifndef _XS_UNICODE_H
#define _XS_UNICODE_H
int xs_utf8_enc(char buf[4], unsigned int cpoint);
int xs_is_utf8_cont_byte(char c);
unsigned int xs_utf8_dec(const char **str);
int xs_unicode_width(unsigned int cpoint);
int xs_is_surrogate(unsigned int cpoint);
int xs_is_diacritic(unsigned int cpoint);
unsigned int xs_surrogate_dec(unsigned int p1, unsigned int p2);
unsigned int xs_surrogate_enc(unsigned int cpoint);
unsigned int *_xs_unicode_upper_search(unsigned int cpoint);
unsigned int *_xs_unicode_lower_search(unsigned int cpoint);
#define xs_unicode_is_upper(cpoint) (!!_xs_unicode_upper_search(cpoint))
#define xs_unicode_is_lower(cpoint) (!!_xs_unicode_lower_search(cpoint))
unsigned int xs_unicode_to_upper(unsigned int cpoint);
unsigned int xs_unicode_to_lower(unsigned int cpoint);
int xs_unicode_nfd(unsigned int cpoint, unsigned int *base, unsigned int *diac);
int xs_unicode_nfc(unsigned int base, unsigned int diac, unsigned int *cpoint);
int xs_unicode_is_alpha(unsigned int cpoint);
int xs_unicode_is_right_to_left(unsigned int cpoint);
int xs_is_emoji(unsigned int cpoint);
#ifdef _XS_H
xs_str *xs_utf8_insert(xs_str *str, unsigned int cpoint, int *offset);
xs_str *xs_utf8_cat(xs_str *str, unsigned int cpoint);
xs_str *xs_utf8_to_upper(const char *str);
xs_str *xs_utf8_to_lower(const char *str);
xs_str *xs_utf8_to_nfd(const char *str);
xs_str *xs_utf8_to_nfc(const char *str);
#endif
#ifdef XS_IMPLEMENTATION
#include <ctype.h>
#ifndef xs_countof
#define xs_countof(a) (sizeof((a)) / sizeof((*a)))
#endif
int xs_utf8_enc(char buf[4], unsigned int cpoint)
/* encodes an Unicode codepoint to utf-8 into buf and returns the size in bytes */
{
char *p = buf;
if (cpoint < 0x80) /* 1 byte char */
*p++ = cpoint & 0xff;
else {
if (cpoint < 0x800) /* 2 byte char */
*p++ = 0xc0 | (cpoint >> 6);
else {
if (cpoint < 0x10000) /* 3 byte char */
*p++ = 0xe0 | (cpoint >> 12);
else { /* 4 byte char */
*p++ = 0xf0 | (cpoint >> 18);
*p++ = 0x80 | ((cpoint >> 12) & 0x3f);
}
*p++ = 0x80 | ((cpoint >> 6) & 0x3f);
}
*p++ = 0x80 | (cpoint & 0x3f);
}
return p - buf;
}
int xs_is_utf8_cont_byte(char c)
/* returns true if c is an utf8 continuation byte */
{
return ((c & 0xc0) == 0x80);
}
unsigned int xs_utf8_dec(const char **str)
/* decodes an utf-8 char inside str and updates the pointer */
{
const char *p = *str;
if (!xs_is_string(p))
return 0;
unsigned int cpoint = 0;
unsigned char c = *p++;
int cb = 0;
if ((c & 0x80) == 0) { /* 1 byte char */
cpoint = c;
}
else
if ((c & 0xe0) == 0xc0) { /* 2 byte char */
cpoint = (c & 0x1f) << 6;
cb = 1;
}
else
if ((c & 0xf0) == 0xe0) { /* 3 byte char */
cpoint = (c & 0x0f) << 12;
cb = 2;
}
else
if ((c & 0xf8) == 0xf0) { /* 4 byte char */
cpoint = (c & 0x07) << 18;
cb = 3;
}
/* process the continuation bytes */
while (cb > 0 && *p && xs_is_utf8_cont_byte(*p))
cpoint |= (*p++ & 0x3f) << (--cb * 6);
/* incomplete or broken? */
if (cb)
cpoint = 0xfffd;
*str = p;
return cpoint;
}
/** Unicode character width: intentionally dead simple **/
static unsigned int xs_unicode_width_table[] = {
0x300, 0x36f, 0, /* diacritics */
0x1100, 0x11ff, 2, /* Hangul */
0x2e80, 0xa4cf, 2, /* CJK */
0xac00, 0xd7a3, 2, /* more Hangul */
0xe000, 0xf8ff, 0, /* private use */
0xf900, 0xfaff, 2, /* CJK compatibility */
0xff00, 0xff60, 2, /* full width things */
0xffdf, 0xffe6, 2, /* full width things */
0x1f200, 0x1ffff, 2, /* emojis */
0x20000, 0x2fffd, 2 /* more CJK */
};
/* magic number from https://en.wikipedia.org/wiki/Emoji#Unicode_blocks */
int xs_is_emoji(unsigned int cpoint) {
/* returns wether the input is an utf8 emoji */
return cpoint > 0x00A9 && cpoint < 0x1ffff;
}
int xs_unicode_width(unsigned int cpoint)
/* returns the width in columns of a Unicode codepoint (somewhat simplified) */
{
int b = 0;
int t = xs_countof(xs_unicode_width_table) / 3 - 1;
while (t >= b) {
int n = (b + t) / 2;
unsigned int *p = &xs_unicode_width_table[n * 3];
if (cpoint < p[0])
t = n - 1;
else
if (cpoint > p[1])
b = n + 1;
else
return p[2];
}
return 1;
}
int xs_is_diacritic(unsigned int cpoint)
{
return cpoint >= 0x300 && cpoint <= 0x36f;
}
/** surrogate pairs **/
int xs_is_surrogate(unsigned int cpoint)
/* checks if cpoint is the first element of a Unicode surrogate pair */
{
return cpoint >= 0xd800 && cpoint <= 0xdfff;
}
unsigned int xs_surrogate_dec(unsigned int p1, unsigned int p2)
/* "decodes" a surrogate pair into a codepoint */
{
return 0x10000 | ((p1 & 0x3ff) << 10) | (p2 & 0x3ff);
}
unsigned int xs_surrogate_enc(unsigned int cpoint)
/* "encodes" a Unicode into a surrogate pair (p1 in the MSB word) */
{
unsigned int p1 = 0xd7c0 + (cpoint >> 10);
unsigned int p2 = 0xdc00 + (cpoint & 0x3ff);
return (p1 << 16) | p2;
}
#ifdef _XS_H
xs_str *xs_utf8_insert(xs_str *str, unsigned int cpoint, int *offset)
/* encodes an Unicode codepoint to utf-8 into str */
{
char tmp[4];
int c = xs_utf8_enc(tmp, cpoint);
str = xs_insert_m(str, *offset, tmp, c);
*offset += c;
return str;
}
xs_str *xs_utf8_cat(xs_str *str, unsigned int cpoint)
/* encodes an Unicode codepoint to utf-8 into str */
{
int offset = strlen(str);
return xs_utf8_insert(str, cpoint, &offset);
}
#endif /* _XS_H */
#ifdef _XS_UNICODE_TBL_H
/* include xs_unicode_tbl.h before this one to use these functions */
unsigned int *_xs_unicode_upper_search(unsigned int cpoint)
/* searches for an uppercase codepoint in the case fold table */
{
int b = 0;
int t = xs_countof(xs_unicode_case_fold_table) / 2 + 1;
while (t >= b) {
int n = (b + t) / 2;
unsigned int *p = &xs_unicode_case_fold_table[n * 2];
if (cpoint < p[0])
t = n - 1;
else
if (cpoint > p[0])
b = n + 1;
else
return p;
}
return NULL;
}
unsigned int *_xs_unicode_lower_search(unsigned int cpoint)
/* searches for a lowercase codepoint in the case fold table */
{
unsigned int *p = xs_unicode_case_fold_table;
unsigned int *e = p + xs_countof(xs_unicode_case_fold_table);
while (p < e) {
if (cpoint == p[1])
return p;
p += 2;
}
return NULL;
}
unsigned int xs_unicode_to_lower(unsigned int cpoint)
/* returns the cpoint to lowercase */
{
if (cpoint < 0x80)
return tolower(cpoint);
unsigned int *p = _xs_unicode_upper_search(cpoint);
return p == NULL ? cpoint : p[1];
}
unsigned int xs_unicode_to_upper(unsigned int cpoint)
/* returns the cpoint to uppercase */
{
if (cpoint < 0x80)
return toupper(cpoint);
unsigned int *p = _xs_unicode_lower_search(cpoint);
return p == NULL ? cpoint : p[0];
}
int xs_unicode_nfd(unsigned int cpoint, unsigned int *base, unsigned int *diac)
/* applies unicode Normalization Form D */
{
int b = 0;
int t = xs_countof(xs_unicode_nfd_table) / 3 - 1;
while (t >= b) {
int n = (b + t) / 2;
unsigned int *p = &xs_unicode_nfd_table[n * 3];
int c = cpoint - p[0];
if (c < 0)
t = n - 1;
else
if (c > 0)
b = n + 1;
else {
*base = p[1];
*diac = p[2];
return 1;
}
}
return 0;
}
int xs_unicode_nfc(unsigned int base, unsigned int diac, unsigned int *cpoint)
/* applies unicode Normalization Form C */
{
unsigned int *p = xs_unicode_nfd_table;
unsigned int *e = p + xs_countof(xs_unicode_nfd_table);
while (p < e) {
if (p[1] == base && p[2] == diac) {
*cpoint = p[0];
return 1;
}
p += 3;
}
return 0;
}
int xs_unicode_is_alpha(unsigned int cpoint)
/* checks if a codepoint is an alpha (i.e. a letter) */
{
int b = 0;
int t = xs_countof(xs_unicode_alpha_table) / 2 - 1;
while (t >= b) {
int n = (b + t) / 2;
unsigned int *p = &xs_unicode_alpha_table[n * 2];
if (cpoint < p[0])
t = n - 1;
else
if (cpoint > p[1])
b = n + 1;
else
return 1;
}
return 0;
}
int xs_unicode_is_right_to_left(unsigned int cpoint)
/* checks if a codepoint is a right-to-left letter */
{
int b = 0;
int t = xs_countof(xs_unicode_right_to_left_table) / 2 - 1;
while (t >= b) {
int n = (b + t) / 2;
unsigned int *p = &xs_unicode_right_to_left_table[n * 2];
if (cpoint < p[0])
t = n - 1;
else
if (cpoint > p[1])
b = n + 1;
else
return 1;
}
return 0;
}
#ifdef _XS_H
xs_str *xs_utf8_to_upper(const char *str)
{
xs_str *s = xs_str_new(NULL);
unsigned int cpoint;
int offset = 0;
while ((cpoint = xs_utf8_dec(&str))) {
cpoint = xs_unicode_to_upper(cpoint);
s = xs_utf8_insert(s, cpoint, &offset);
}
return s;
}
xs_str *xs_utf8_to_lower(const char *str)
{
xs_str *s = xs_str_new(NULL);
unsigned int cpoint;
int offset = 0;
while ((cpoint = xs_utf8_dec(&str))) {
cpoint = xs_unicode_to_lower(cpoint);
s = xs_utf8_insert(s, cpoint, &offset);
}
return s;
}
xs_str *xs_utf8_to_nfd(const char *str)
{
xs_str *s = xs_str_new(NULL);
unsigned int cpoint;
int offset = 0;
while ((cpoint = xs_utf8_dec(&str))) {
unsigned int base;
unsigned int diac;
if (xs_unicode_nfd(cpoint, &base, &diac)) {
s = xs_utf8_insert(s, base, &offset);
s = xs_utf8_insert(s, diac, &offset);
}
else
s = xs_utf8_insert(s, cpoint, &offset);
}
return s;
}
xs_str *xs_utf8_to_nfc(const char *str)
{
xs_str *s = xs_str_new(NULL);
unsigned int cpoint;
unsigned int base = 0;
int offset = 0;
while ((cpoint = xs_utf8_dec(&str))) {
if (xs_is_diacritic(cpoint)) {
if (xs_unicode_nfc(base, cpoint, &base))
continue;
}
if (base)
s = xs_utf8_insert(s, base, &offset);
base = cpoint;
}
if (base)
s = xs_utf8_insert(s, base, &offset);
return s;
}
#endif /* _XS_H */
#endif /* _XS_UNICODE_TBL_H */
#endif /* XS_IMPLEMENTATION */
#endif /* _XS_UNICODE_H */
|