31# pragma warning(disable : 4255)
35# pragma warning(disable : 4668)
38# pragma warning(disable : 4820)
41#if defined(__cplusplus)
43# define utf8_cplusplus _MSVC_LANG
45# define utf8_cplusplus __cplusplus
56#if defined(_MSC_VER) && (_MSC_VER < 1920)
64# pragma clang diagnostic push
65# pragma clang diagnostic ignored "-Wold-style-cast"
66# pragma clang diagnostic ignored "-Wcast-qual"
68# if __has_warning("-Wunsafe-buffer-usage")
69# pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
78# define UTF8_ATTRIBUTE(a) __attribute((a))
80# define UTF8_ATTRIBUTE(a) __attribute__((a))
86# define utf8_restrict __restrict
87# define utf8_weak __inline
88#elif defined(__clang__) || defined(__GNUC__)
89# define utf8_nonnull UTF8_ATTRIBUTE(nonnull)
90# define utf8_pure UTF8_ATTRIBUTE(pure)
91# define utf8_restrict __restrict__
92# define utf8_weak UTF8_ATTRIBUTE(weak)
93#elif defined(__TINYC__)
94# define utf8_nonnull UTF8_ATTRIBUTE(nonnull)
95# define utf8_pure UTF8_ATTRIBUTE(pure)
97# define utf8_weak UTF8_ATTRIBUTE(weak)
98#elif defined(__IAR_SYSTEMS_ICC__)
100# define utf8_pure UTF8_ATTRIBUTE(pure)
101# define utf8_restrict __restrict
102# define utf8_weak UTF8_ATTRIBUTE(weak)
104# error Non clang, non gcc, non MSVC, non tcc, non iar compiler found!
108# define utf8_null NULL
113#if defined(utf8_cplusplus) && utf8_cplusplus >= 201402L && \
114 (!defined(_MSC_VER) || (defined(_MSC_VER) && _MSC_VER >= 1910))
115# define utf8_constexpr14 constexpr
116# define utf8_constexpr14_impl constexpr
119# define utf8_constexpr14 utf8_weak
120# define utf8_constexpr14_impl
123#if defined(utf8_cplusplus) && utf8_cplusplus >= 202002L && defined(__cpp_char8_t)
306 utf8_int32_t src1_lwr_cp = 0, src2_lwr_cp = 0, src1_upr_cp = 0, src2_upr_cp = 0, src1_orig_cp = 0,
322 if ((0 == src1_orig_cp) && (0 == src2_orig_cp)) {
324 }
else if ((src1_lwr_cp == src2_lwr_cp) || (src1_upr_cp == src2_upr_cp)) {
330 return src1_lwr_cp - src2_lwr_cp;
342 while (
'\0' != *src) {
358 while (
'\0' != *src) {
393 while ((
'\0' != *src1) || (
'\0' != *src2)) {
396 }
else if (*src1 > *src2) {
415 while (
'\0' != *src) {
428 while (
'\0' != *src) {
436 if ((0x80 != (0xc0 & *r)) && (0 < offset)) {
439 if (*r == src[offset]) {
450 }
while (0x80 == (0xc0 & *r));
468 }
while ((0x80 == (0xc0 & *src)));
487 if (alloc_func_ptr) {
488 n = alloc_func_ptr(user_data, bytes);
490#if !defined(UTF8_NO_STD_MALLOC)
504 while (
'\0' != src[bytes]) {
505 n[bytes] = src[bytes];
523 while ((
size_t)(str - t) <
n &&
'\0' != *str) {
524 if (0xf0 == (0xf8 & *str)) {
527 }
else if (0xe0 == (0xf0 & *str)) {
530 }
else if (0xc0 == (0xe0 & *str)) {
543 if ((
size_t)(str - t) >
n) {
550 utf8_int32_t src1_lwr_cp = 0, src2_lwr_cp = 0, src1_upr_cp = 0, src2_upr_cp = 0, src1_orig_cp = 0,
563 if ((1 ==
n) && ((0xc0 == (0xe0 & *s1)) || (0xc0 == (0xe0 & *s2)))) {
574 if ((2 >=
n) && ((0xe0 == (0xf0 & *s1)) || (0xe0 == (0xf0 & *s2)))) {
585 if ((3 >=
n) && ((0xf0 == (0xf8 & *s1)) || (0xf0 == (0xf8 & *s2)))) {
607 if ((0 == src1_orig_cp) && (0 == src2_orig_cp)) {
609 }
else if ((src1_lwr_cp == src2_lwr_cp) || (src1_upr_cp == src2_upr_cp)) {
615 return src1_lwr_cp - src2_lwr_cp;
633 while ((
'\0' != *src) && (0 !=
n--)) {
644 while ((0 !=
n--) && ((
'\0' != *src1) || (
'\0' != *src2))) {
647 }
else if (*src1 > *src2) {
662 size_t index = 0, check_index = 0;
670 for (index = 0; index <
n; index++) {
671 d[index] = src[index];
672 if (
'\0' == src[index]) {
677 for (check_index = index - 1; check_index > 0 && 0x80 == (0xc0 & d[check_index]); check_index--) {
682 check_index < index &&
689 for (; index <
n; index++) {
708 while (
'\0' != src[bytes] && bytes <
n) {
716 if (alloc_func_ptr) {
717 c = alloc_func_ptr(user_data, bytes + 1);
719#if !defined(UTF8_NO_STD_MALLOC)
734 while (
'\0' != src[bytes] && bytes <
n) {
735 c[bytes] = src[bytes];
752 while (
'\0' != *src) {
756 }
else if (0 == ((
int)0xffffff80 & chr)) {
760 }
else if (0 == ((
int)0xfffff800 & chr)) {
765 }
else if (0 == ((
int)0xffff0000 & chr)) {
784 while (
'\0' != *src) {
787 while ((src[offset] == c[offset]) && (
'\0' != src[offset])) {
791 if (
'\0' == c[offset]) {
807 }
while (0x80 == (0xc0 & *src));
817 while (
'\0' != *str) {
825 if ((0x80 != (0xc0 & *a)) && (0 < offset)) {
828 if (*a == str[offset]) {
839 }
while (0x80 == (0xc0 & *a));
857 }
while ((0x80 == (0xc0 & *str)));
871 while (size <
n &&
'\0' != str[size]) {
880 while (
'\0' != *src) {
888 if ((0x80 != (0xc0 & *a)) && (0 < offset)) {
897 if (*a == src[offset]) {
905 }
while (0x80 == (0xc0 & *a));
935 if (
'\0' == *needle) {
939 while (
'\0' != *haystack) {
943 while (*haystack == *
n && (*haystack !=
'\0' && *
n !=
'\0')) {
968 if (
'\0' == *needle) {
981 while ((0 != h_cp) && (0 != n_cp)) {
1016 size_t consumed = 0;
1018 while ((
void)(consumed = (
size_t)(str - t)), consumed <
n &&
'\0' != *str) {
1019 const size_t remaining =
n - consumed;
1021 if (0xf0 == (0xf8 & *str)) {
1023 if (remaining < 4) {
1029 if ((0x80 != (0xc0 & str[1])) || (0x80 != (0xc0 & str[2])) || (0x80 != (0xc0 & str[3]))) {
1034 if ((remaining != 4) && (0x80 == (0xc0 & str[4]))) {
1041 if ((0 == (0x07 & str[0])) && (0 == (0x30 & str[1]))) {
1047 }
else if (0xe0 == (0xf0 & *str)) {
1049 if (remaining < 3) {
1055 if ((0x80 != (0xc0 & str[1])) || (0x80 != (0xc0 & str[2]))) {
1060 if ((remaining != 3) && (0x80 == (0xc0 & str[3]))) {
1067 if ((0 == (0x0f & str[0])) && (0 == (0x20 & str[1]))) {
1073 }
else if (0xc0 == (0xe0 & *str)) {
1075 if (remaining < 2) {
1081 if (0x80 != (0xc0 & str[1])) {
1086 if ((remaining != 2) && (0x80 == (0xc0 & str[2]))) {
1093 if (0 == (0x1e & str[0])) {
1099 }
else if (0x00 == (0x80 & *str)) {
1117 if (replacement > 0x7f) {
1121 while (
'\0' != *read) {
1122 if (0xf0 == (0xf8 & *read)) {
1125 if ((0x80 != (0xc0 & read[1])) || (0x80 != (0xc0 & read[2])) || (0x80 != (0xc0 & read[3]))) {
1134 }
else if (0xe0 == (0xf0 & *read)) {
1137 if ((0x80 != (0xc0 & read[1])) || (0x80 != (0xc0 & read[2]))) {
1146 }
else if (0xc0 == (0xe0 & *read)) {
1149 if (0x80 != (0xc0 & read[1])) {
1158 }
else if (0x00 == (0x80 & *read)) {
1177 if (0xf0 == (0xf8 & str[0])) {
1180 ((0x07 & str[0]) << 18) | ((0x3f & str[1]) << 12) | ((0x3f & str[2]) << 6) | (0x3f & str[3]);
1182 }
else if (0xe0 == (0xf0 & str[0])) {
1184 *out_codepoint = ((0x0f & str[0]) << 12) | ((0x3f & str[1]) << 6) | (0x3f & str[2]);
1186 }
else if (0xc0 == (0xe0 & str[0])) {
1188 *out_codepoint = ((0x1f & str[0]) << 6) | (0x3f & str[1]);
1192 *out_codepoint = str[0];
1200 if (0xf0 == (0xf8 & str[0])) {
1203 }
else if (0xe0 == (0xf0 & str[0])) {
1206 }
else if (0xc0 == (0xe0 & str[0])) {
1311 ((0x0041 <= cp) && (0x005a >= cp)) || ((0x00c0 <= cp) && (0x00d6 >= cp)) ||
1312 ((0x00d8 <= cp) && (0x00de >= cp)) || ((0x0391 <= cp) && (0x03a1 >= cp)) ||
1313 ((0x03a3 <= cp) && (0x03ab >= cp)) || ((0x0410 <= cp) && (0x042f >= cp))
1316 }
else if ((0x0400 <= cp) && (0x040f >= cp)) {
1319 ((0x0100 <= cp) && (0x012f >= cp)) || ((0x0132 <= cp) && (0x0137 >= cp)) ||
1320 ((0x014a <= cp) && (0x0177 >= cp)) || ((0x0182 <= cp) && (0x0185 >= cp)) ||
1321 ((0x01a0 <= cp) && (0x01a5 >= cp)) || ((0x01de <= cp) && (0x01ef >= cp)) ||
1322 ((0x01f8 <= cp) && (0x021f >= cp)) || ((0x0222 <= cp) && (0x0233 >= cp)) ||
1323 ((0x0246 <= cp) && (0x024f >= cp)) || ((0x03d8 <= cp) && (0x03ef >= cp)) ||
1324 ((0x0460 <= cp) && (0x0481 >= cp)) || ((0x048a <= cp) && (0x04ff >= cp))
1328 ((0x0139 <= cp) && (0x0148 >= cp)) || ((0x0179 <= cp) && (0x017e >= cp)) ||
1329 ((0x01af <= cp) && (0x01b0 >= cp)) || ((0x01b3 <= cp) && (0x01b6 >= cp)) ||
1330 ((0x01cd <= cp) && (0x01dc >= cp))
1469 ((0x0061 <= cp) && (0x007a >= cp)) || ((0x00e0 <= cp) && (0x00f6 >= cp)) ||
1470 ((0x00f8 <= cp) && (0x00fe >= cp)) || ((0x03b1 <= cp) && (0x03c1 >= cp)) ||
1471 ((0x03c3 <= cp) && (0x03cb >= cp)) || ((0x0430 <= cp) && (0x044f >= cp))
1474 }
else if ((0x0450 <= cp) && (0x045f >= cp)) {
1477 ((0x0100 <= cp) && (0x012f >= cp)) || ((0x0132 <= cp) && (0x0137 >= cp)) ||
1478 ((0x014a <= cp) && (0x0177 >= cp)) || ((0x0182 <= cp) && (0x0185 >= cp)) ||
1479 ((0x01a0 <= cp) && (0x01a5 >= cp)) || ((0x01de <= cp) && (0x01ef >= cp)) ||
1480 ((0x01f8 <= cp) && (0x021f >= cp)) || ((0x0222 <= cp) && (0x0233 >= cp)) ||
1481 ((0x0246 <= cp) && (0x024f >= cp)) || ((0x03d8 <= cp) && (0x03ef >= cp)) ||
1482 ((0x0460 <= cp) && (0x0481 >= cp)) || ((0x048a <= cp) && (0x04ff >= cp))
1486 ((0x0139 <= cp) && (0x0148 >= cp)) || ((0x0179 <= cp) && (0x017e >= cp)) ||
1487 ((0x01af <= cp) && (0x01b0 >= cp)) || ((0x01b3 <= cp) && (0x01b6 >= cp)) ||
1488 ((0x01cd <= cp) && (0x01dc >= cp))
1629 if (0xf0 == (0xf8 & s[0])) {
1632 ((0x07 & s[0]) << 18) | ((0x3f & s[1]) << 12) | ((0x3f & s[2]) << 6) | (0x3f & s[3]);
1633 }
else if (0xe0 == (0xf0 & s[0])) {
1635 *out_codepoint = ((0x0f & s[0]) << 12) | ((0x3f & s[1]) << 6) | (0x3f & s[2]);
1636 }
else if (0xc0 == (0xe0 & s[0])) {
1638 *out_codepoint = ((0x1f & s[0]) << 6) | (0x3f & s[1]);
1641 *out_codepoint = s[0];
1646 }
while ((0 != (0x80 & s[0])) && (0x80 == (0xc0 & s[0])));
1652#undef utf8_constexpr14
1655#ifdef utf8_cplusplus
1659#if defined(__clang__)
1660# pragma clang diagnostic pop
utf8_constexpr14 utf8_nonnull utf8_pure utf8_int8_t * utf8chr(const utf8_int8_t *src, utf8_int32_t chr)
utf8_nonnull utf8_weak utf8_int8_t * utf8catcodepoint(utf8_int8_t *str, utf8_int32_t chr, size_t n)
utf8_weak utf8_int8_t * utf8dup(const utf8_int8_t *src)
utf8_constexpr14 utf8_nonnull utf8_pure size_t utf8len(const utf8_int8_t *str)
utf8_constexpr14 utf8_nonnull size_t utf8codepointcalcsize(const utf8_int8_t *str)
utf8_nonnull utf8_weak utf8_int8_t * utf8ncat(utf8_int8_t *utf8_restrict dst, const utf8_int8_t *utf8_restrict src, size_t n)
utf8_constexpr14 utf8_nonnull utf8_pure utf8_int8_t * utf8valid(const utf8_int8_t *str)
utf8_constexpr14 utf8_int32_t utf8uprcodepoint(utf8_int32_t cp)
utf8_constexpr14 utf8_nonnull utf8_pure int utf8ncmp(const utf8_int8_t *src1, const utf8_int8_t *src2, size_t n)
utf8_constexpr14 size_t utf8codepointsize(utf8_int32_t chr)
utf8_nonnull utf8_weak void utf8lwr(utf8_int8_t *utf8_restrict str)
utf8_nonnull utf8_weak void utf8upr(utf8_int8_t *utf8_restrict str)
utf8_constexpr14_impl utf8_int8_t * utf8fry(const utf8_int8_t *str)
utf8_constexpr14 utf8_nonnull utf8_pure size_t utf8size_lazy(const utf8_int8_t *str)
utf8_constexpr14 utf8_nonnull utf8_pure size_t utf8size(const utf8_int8_t *str)
utf8_nonnull utf8_weak utf8_int8_t * utf8cat(utf8_int8_t *utf8_restrict dst, const utf8_int8_t *utf8_restrict src)
utf8_constexpr14 utf8_nonnull utf8_pure size_t utf8cspn(const utf8_int8_t *src, const utf8_int8_t *reject)
utf8_constexpr14 utf8_nonnull utf8_int8_t * utf8codepoint(const utf8_int8_t *utf8_restrict str, utf8_int32_t *utf8_restrict out_codepoint)
utf8_constexpr14 int utf8isupper(utf8_int32_t chr)
utf8_constexpr14 utf8_nonnull utf8_int8_t * utf8rcodepoint(const utf8_int8_t *utf8_restrict str, utf8_int32_t *utf8_restrict out_codepoint)
utf8_constexpr14 int utf8islower(utf8_int32_t chr)
utf8_constexpr14 utf8_nonnull utf8_pure int utf8ncasecmp(const utf8_int8_t *src1, const utf8_int8_t *src2, size_t n)
utf8_constexpr14 utf8_nonnull utf8_pure utf8_int8_t * utf8str(const utf8_int8_t *haystack, const utf8_int8_t *needle)
utf8_constexpr14 utf8_nonnull utf8_pure int utf8casecmp(const utf8_int8_t *src1, const utf8_int8_t *src2)
utf8_weak utf8_int8_t * utf8dup_ex(const utf8_int8_t *src, utf8_int8_t *(*alloc_func_ptr)(utf8_int8_t *, size_t), utf8_int8_t *user_data)
utf8_weak utf8_int8_t * utf8ndup(const utf8_int8_t *src, size_t n)
utf8_nonnull utf8_weak int utf8makevalid(utf8_int8_t *str, const utf8_int32_t replacement)
utf8_weak utf8_int8_t * utf8ndup_ex(const utf8_int8_t *src, size_t n, utf8_int8_t *(*alloc_func_ptr)(utf8_int8_t *, size_t), utf8_int8_t *user_data)
utf8_nonnull utf8_weak utf8_int8_t * utf8ncpy(utf8_int8_t *utf8_restrict dst, const utf8_int8_t *utf8_restrict src, size_t n)
utf8_constexpr14 utf8_nonnull utf8_pure utf8_int8_t * utf8rchr(const utf8_int8_t *src, int chr)
utf8_constexpr14 utf8_int32_t utf8lwrcodepoint(utf8_int32_t cp)
#define utf8_constexpr14_impl
utf8_constexpr14 utf8_nonnull utf8_pure int utf8cmp(const utf8_int8_t *src1, const utf8_int8_t *src2)
utf8_constexpr14 utf8_nonnull utf8_pure utf8_int8_t * utf8casestr(const utf8_int8_t *haystack, const utf8_int8_t *needle)
utf8_nonnull utf8_weak utf8_int8_t * utf8cpy(utf8_int8_t *utf8_restrict dst, const utf8_int8_t *utf8_restrict src)
utf8_constexpr14_impl int utf8coll(const utf8_int8_t *src1, const utf8_int8_t *src2)
utf8_constexpr14 utf8_nonnull utf8_pure utf8_int8_t * utf8pbrk(const utf8_int8_t *str, const utf8_int8_t *accept)
utf8_constexpr14 utf8_nonnull utf8_pure size_t utf8spn(const utf8_int8_t *src, const utf8_int8_t *accept)
utf8_constexpr14 utf8_nonnull utf8_pure utf8_int8_t * utf8nvalid(const utf8_int8_t *str, size_t n)
utf8_constexpr14 utf8_nonnull utf8_pure size_t utf8nlen(const utf8_int8_t *str, size_t n)
utf8_constexpr14 utf8_nonnull utf8_pure size_t utf8nsize_lazy(const utf8_int8_t *str, size_t n)