| 1 |
|
| /* |
| 2 |
|
| * Copyright (c) 2017-2026 Christian Hansen <chansen@cpan.org> |
| 3 |
|
| * <https://github.com/chansen/c-utf8> |
| 4 |
|
| * |
| 5 |
|
| * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 |
|
| * of this software and associated documentation files (the "Software"), to deal |
| 7 |
|
| * in the Software without restriction, including without limitation the rights |
| 8 |
|
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 |
|
| * copies of the Software, and to permit persons to whom the Software is |
| 10 |
|
| * furnished to do so, subject to the following conditions: |
| 11 |
|
| * |
| 12 |
|
| * The above copyright notice and this permission notice shall be included in all |
| 13 |
|
| * copies or substantial portions of the Software. |
| 14 |
|
| * |
| 15 |
|
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 |
|
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 |
|
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 |
|
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 |
|
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 |
|
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 |
|
| * SOFTWARE. |
| 22 |
|
| */ |
| 23 |
|
| #ifndef UTF8_VALID_H |
| 24 |
|
| #define UTF8_VALID_H |
| 25 |
|
| #include <stddef.h> |
| 26 |
|
| #include <stdint.h> |
| 27 |
|
| #include <stdbool.h> |
| 28 |
|
| #include <string.h> |
| 29 |
|
| |
| 30 |
|
| #if defined(UTF8_DFA32_H) && defined(UTF8_DFA64_H) |
| 31 |
|
| # error "utf8_dfa32.h and utf8_dfa64.h are mutually exclusive" |
| 32 |
|
| #elif !defined(UTF8_DFA32_H) && !defined(UTF8_DFA64_H) |
| 33 |
|
| # error "include utf8_dfa32.h or utf8_dfa64.h before utf8_valid.h" |
| 34 |
|
| #endif |
| 35 |
|
| |
| 36 |
|
| #ifdef UTF8_VALID_USE_SIMD |
| 37 |
|
| # if defined(__SSE2__) || defined(_M_X64) || (defined(_M_IX86_FP) && (_M_IX86_FP >= 2)) |
| 38 |
|
| # define UTF8_VALID_HAS_SSE2 1 |
| 39 |
|
| # include <emmintrin.h> |
| 40 |
|
| # elif defined(__aarch64__) |
| 41 |
|
| # define UTF8_VALID_HAS_NEON 1 |
| 42 |
|
| # include <arm_neon.h> |
| 43 |
|
| # endif |
| 44 |
|
| #endif |
| 45 |
|
| |
| 46 |
|
| #ifdef __cplusplus |
| 47 |
|
| extern "C" { |
| 48 |
|
| #endif |
| 49 |
|
| |
| 50 |
8427 |
| static inline size_t utf8_maximal_subpart(const char* src, size_t len) { |
| 51 |
8427 |
| const unsigned char* s = (const unsigned char*)src; |
| 52 |
8427 |
| utf8_dfa_state_t state = UTF8_DFA_ACCEPT; |
| 53 |
|
| |
| 54 |
14106 |
▶ | for (size_t i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t i = 0; i < len; i++) { |
13184 |
922 |
14106 |
|
| 55 |
13184 |
| state = utf8_dfa_step(state, s[i]); |
| 56 |
13184 |
| switch (state) { |
| 57 |
0 |
| case UTF8_DFA_ACCEPT: |
| 58 |
0 |
| return i + 1; |
| 59 |
7505 |
| case UTF8_DFA_REJECT: |
| 60 |
7505 |
▶ | return i > 0 ? i : 1; |
Branch: 1 branch
| Branch | True | False | Total |
| return i > 0 ? i : 1; |
3521 |
3984 |
7505 |
|
| 61 |
|
| } |
| 62 |
|
| } |
| 63 |
922 |
| return len; |
| 64 |
|
| } |
| 65 |
|
| |
| 66 |
11794 |
| static inline size_t utf8_maximal_prefix(const char* src, size_t len) { |
| 67 |
11794 |
| const unsigned char* s = (const unsigned char*)src; |
| 68 |
11794 |
| utf8_dfa_state_t state = UTF8_DFA_ACCEPT; |
| 69 |
11794 |
| size_t prefix = 0; |
| 70 |
|
| |
| 71 |
38198 |
▶ | for (size_t i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t i = 0; i < len; i++) { |
37004 |
1194 |
38198 |
|
| 72 |
37004 |
| state = utf8_dfa_step(state, s[i]); |
| 73 |
37004 |
▶ | if (state == UTF8_DFA_ACCEPT) |
Branch: 1 branch
| Branch | True | False | Total |
| if (state == UTF8_DFA_ACCEPT) |
7733 |
29271 |
37004 |
|
| 74 |
7733 |
| prefix = i + 1; |
| 75 |
29271 |
▶ | else if (state == UTF8_DFA_REJECT) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (state == UTF8_DFA_REJECT) |
10600 |
18671 |
29271 |
|
| 76 |
10600 |
| break; |
| 77 |
|
| } |
| 78 |
11794 |
| return prefix; |
| 79 |
|
| } |
| 80 |
|
| |
| 81 |
|
| static inline bool utf8_check(const char* src, |
| 82 |
|
| size_t slen, |
| 83 |
|
| size_t* cursor) { |
| 84 |
|
| const unsigned char* s = (const unsigned char*)src; |
| 85 |
|
| size_t len = slen; |
| 86 |
|
| utf8_dfa_state_t state = UTF8_DFA_ACCEPT; |
| 87 |
|
| |
| 88 |
|
| // Process 16-byte chunks |
| 89 |
|
| while (len >= 16) { |
| 90 |
|
| state = utf8_dfa_run16(state, s); |
| 91 |
|
| s += 16; |
| 92 |
|
| len -= 16; |
| 93 |
|
| } |
| 94 |
|
| |
| 95 |
|
| state = utf8_dfa_run(state, s, len); |
| 96 |
|
| if (state == UTF8_DFA_ACCEPT) { |
| 97 |
|
| if (cursor) |
| 98 |
|
| *cursor = slen; |
| 99 |
|
| return true; |
| 100 |
|
| } |
| 101 |
|
| |
| 102 |
|
| if (cursor) |
| 103 |
|
| *cursor = utf8_maximal_prefix(src, slen); |
| 104 |
|
| return false; |
| 105 |
|
| } |
| 106 |
|
| |
| 107 |
|
| static inline bool utf8_valid(const char* src, size_t len) { |
| 108 |
|
| return utf8_check(src, len, NULL); |
| 109 |
|
| } |
| 110 |
|
| |
| 111 |
4709 |
| static inline bool utf8_check_ascii_block16(const unsigned char *s) { |
| 112 |
|
| #if defined(UTF8_VALID_HAS_SSE2) |
| 113 |
|
| __m128i v = _mm_loadu_si128((const __m128i *)s); |
| 114 |
|
| return _mm_movemask_epi8(v) == 0; |
| 115 |
|
| #elif defined(UTF8_VALID_HAS_NEON) |
| 116 |
|
| uint8x16_t v = vld1q_u8(s); |
| 117 |
|
| uint8x16_t high = vshrq_n_u8(v, 7); |
| 118 |
|
| return vmaxvq_u8(high) == 0; |
| 119 |
|
| #else |
| 120 |
|
| uint64_t v1, v2; |
| 121 |
4709 |
| memcpy(&v1, s, sizeof(v1)); |
| 122 |
4709 |
| memcpy(&v2, s + sizeof(v1), sizeof(v2)); |
| 123 |
4709 |
| v1 |= v2; |
| 124 |
4709 |
| return (v1 & UINT64_C(0x8080808080808080)) == 0; |
| 125 |
|
| #endif |
| 126 |
|
| } |
| 127 |
|
| |
| 128 |
17001 |
| static inline bool utf8_check_ascii(const char* src, size_t slen, size_t* cursor) { |
| 129 |
17001 |
| const unsigned char* s = (const unsigned char*)src; |
| 130 |
17001 |
| size_t len = slen; |
| 131 |
17001 |
| utf8_dfa_state_t state = UTF8_DFA_ACCEPT; |
| 132 |
|
| |
| 133 |
|
| // Process 16-byte chunks; skip DFA when state is clean and chunk is ASCII |
| 134 |
1144930 |
▶ | while (len >= 16) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (len >= 16) { |
1127929 |
17001 |
1144930 |
|
| 135 |
1127929 |
▶ | if (state != UTF8_DFA_ACCEPT || !utf8_check_ascii_block16(s)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (state != UTF8_DFA_ACCEPT || !utf8_check_ascii_block16(s)) |
4709 |
1123220 |
1127929 |
| if (state != UTF8_DFA_ACCEPT || !utf8_check_ascii_block16(s)) |
3090 |
1619 |
4709 |
|
| 136 |
1126310 |
| state = utf8_dfa_run16(state, s); |
| 137 |
1127929 |
| s += 16; |
| 138 |
1127929 |
| len -= 16; |
| 139 |
|
| } |
| 140 |
|
| |
| 141 |
17001 |
| state = utf8_dfa_run(state, s, len); |
| 142 |
17001 |
▶ | if (state == UTF8_DFA_ACCEPT) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (state == UTF8_DFA_ACCEPT) { |
2872 |
14129 |
17001 |
|
| 143 |
2872 |
▶ | if (cursor) |
Branch: 1 branch
| Branch | True | False | Total |
| if (cursor) |
2532 |
340 |
2872 |
|
| 144 |
2532 |
| *cursor = slen; |
| 145 |
2872 |
| return true; |
| 146 |
|
| } |
| 147 |
|
| |
| 148 |
14129 |
▶ | if (cursor) |
Branch: 1 branch
| Branch | True | False | Total |
| if (cursor) |
11794 |
2335 |
14129 |
|
| 149 |
11794 |
| *cursor = utf8_maximal_prefix(src, slen); |
| 150 |
14129 |
| return false; |
| 151 |
|
| } |
| 152 |
|
| |
| 153 |
2675 |
| static inline bool utf8_valid_ascii(const char *src, size_t len) { |
| 154 |
2675 |
| return utf8_check_ascii(src, len, NULL); |
| 155 |
|
| } |
| 156 |
|
| |
| 157 |
|
| #ifdef __cplusplus |
| 158 |
|
| } |
| 159 |
|
| #endif |
| 160 |
|
| #endif |