| 1 | /* The five unmodified upstream C sources form one private translation unit. |
| 2 | * No system library or runtime-generated tables are required. */ |
| 3 | #include "unicode_backend.h" |
| 4 | #include <string.h> |
| 5 | #include "unicode_prefix.h" |
| 6 | #define UTF8PROC_STATIC |
| 7 | #include "../thirdparty/utf8proc/utf8proc.c" |
| 8 | #include "../thirdparty/libgrapheme/src/case.c" |
| 9 | #include "../thirdparty/libgrapheme/src/word.c" |
| 10 | #include "../thirdparty/libgrapheme/src/utf8.c" |
| 11 | #include "../thirdparty/libgrapheme/src/util.c" |
| 12 | |
| 13 | int32_t vile_unicode17_scalar(int32_t cp, int mode) { |
| 14 | /* UnicodeData17 simple mappings leave U+00DF unchanged. utf8proc adds |
| 15 | * the reverse of U+1E9E's lower mapping; full string casing stays separate. */ |
| 16 | if (cp == 0xdf && mode != 1) return cp; |
| 17 | return mode == 0 ? utf8proc_toupper(cp) : mode == 1 ? utf8proc_tolower(cp) : utf8proc_totitle(cp); |
| 18 | } |
| 19 | int32_t vile_unicode17_property(int32_t cp, int mode) { |
| 20 | return mode == 0 ? utf8proc_isupper(cp) : utf8proc_islower(cp); |
| 21 | } |
| 22 | int32_t vile_unicode17_category(int32_t cp) { return utf8proc_category(cp); } |
| 23 | int32_t vile_unicode17_fold(int32_t cp, int32_t *pending) { |
| 24 | return (int32_t)utf8proc_decompose_char(cp, pending, 3, UTF8PROC_CASEFOLD, NULL); |
| 25 | } |
| 26 | int64_t vile_unicode17_case(int mode, const uint8_t *source, int64_t length, uint8_t *dest, int64_t capacity) { |
| 27 | if (length < 0 || capacity < 0 || (uint64_t)length > SIZE_MAX || (uint64_t)capacity > SIZE_MAX) return -2; |
| 28 | size_t result = mode == 0 ? grapheme_to_uppercase_utf8((const char*)source, (size_t)length, (char*)dest, (size_t)capacity) |
| 29 | : mode == 1 ? grapheme_to_lowercase_utf8((const char*)source, (size_t)length, (char*)dest, (size_t)capacity) |
| 30 | : grapheme_to_titlecase_utf8((const char*)source, (size_t)length, (char*)dest, (size_t)capacity); |
| 31 | return result > INT64_MAX ? -2 : (int64_t)result; |
| 32 | } |
| 33 | int64_t vile_unicode17_normalize(int mode, const uint8_t *source, int64_t length, int32_t *buffer, int64_t capacity, int64_t *characters) { |
| 34 | if (length < 0 || capacity < 0 || (uint64_t)length > PTRDIFF_MAX || (uint64_t)capacity > PTRDIFF_MAX) return -2; |
| 35 | utf8proc_option_t options = mode == 4 ? UTF8PROC_CASEFOLD : (utf8proc_option_t)(UTF8PROC_STABLE | ((mode & 1) ? UTF8PROC_COMPOSE : UTF8PROC_DECOMPOSE) | ((mode & 2) ? UTF8PROC_COMPAT : 0)); |
| 36 | if (!buffer) return utf8proc_decompose(source, (ptrdiff_t)length, NULL, 0, options); |
| 37 | /* One spare i32 leaves room for reencode's trailing NUL byte. */ |
| 38 | if (capacity < 1) return -2; |
| 39 | ptrdiff_t count = utf8proc_decompose(source, (ptrdiff_t)length, buffer, (ptrdiff_t)capacity - 1, options); |
| 40 | if (count < 0 || count >= capacity) return count < 0 ? count : -2; |
| 41 | count = utf8proc_normalize_utf32(buffer, count, options); |
| 42 | if (count < 0) return count; |
| 43 | *characters = count; |
| 44 | return utf8proc_reencode(buffer, count, 0); |
| 45 | } |
| 46 | |
| 47 | int32_t vile_unicode17_encode(int32_t cp, uint8_t *dest) { return utf8proc_encode_char(cp, dest); } |
| 48 | /* One already-framed scalar only; callers retain subsequence/error policy. |
| 49 | * Borrowed prefix/target pointers are consumed synchronously and never kept. */ |
| 50 | int32_t vile_unicode17_decode(const uint8_t *source, int32_t length, int32_t *scalar) { |
| 51 | if (length < 1 || length > 4) return -1; |
| 52 | return (int32_t)utf8proc_iterate(source, length, scalar); |
| 53 | } |
| 54 | |
| 55 | int32_t vile_unicode17_equal(const uint8_t *a, const uint8_t *b, int64_t n) { return memcmp(a, b, (size_t)n) == 0; } |
| 56 | int64_t vile_unicode17_length(const uint8_t *text, int64_t n) { int64_t count = 0; for (int64_t i = 0; i < n; i++) count += (text[i] & 0xc0) != 0x80; return count; } |
| 57 | int64_t vile_unicode17_word_break(const uint8_t *text, int64_t n) { return (int64_t)grapheme_next_word_break_utf8((const char*)text, (size_t)n); } |
| 58 | |