| 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 |
|
| |
| 29 |
|
| #if defined(UTF8_DFA32_H) && defined(UTF8_DFA64_H) |
| 30 |
|
| # error "utf8_dfa32.h and utf8_dfa64.h are mutually exclusive" |
| 31 |
|
| #elif !defined(UTF8_DFA32_H) && !defined(UTF8_DFA64_H) |
| 32 |
|
| # error "include utf8_dfa32.h or utf8_dfa64.h before utf8_valid.h" |
| 33 |
|
| #endif |
| 34 |
|
| |
| 35 |
|
| #ifdef __cplusplus |
| 36 |
|
| extern "C" { |
| 37 |
|
| #endif |
| 38 |
|
| |
| 39 |
28446 |
| static inline size_t utf8_maximal_subpart(const char* src, size_t len) { |
| 40 |
28446 |
| const uint8_t* bytes = (const uint8_t*)src; |
| 41 |
28446 |
| utf8_dfa_state_t state = UTF8_DFA_ACCEPT; |
| 42 |
|
| |
| 43 |
34135 |
▶ | for (size_t i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t i = 0; i < len; i++) { |
33209 |
926 |
34135 |
|
| 44 |
33209 |
| state = utf8_dfa_step(state, bytes[i]); |
| 45 |
33209 |
| switch (state) { |
| 46 |
0 |
| case UTF8_DFA_ACCEPT: |
| 47 |
0 |
| return i + 1; |
| 48 |
27520 |
| case UTF8_DFA_REJECT: |
| 49 |
27520 |
▶ | return i > 0 ? i : 1; |
Branch: 1 branch
| Branch | True | False | Total |
| return i > 0 ? i : 1; |
3525 |
23995 |
27520 |
|
| 50 |
|
| } |
| 51 |
|
| } |
| 52 |
926 |
| return len; |
| 53 |
|
| } |
| 54 |
|
| |
| 55 |
31813 |
| static inline size_t utf8_maximal_prefix(const char* src, size_t len) { |
| 56 |
31813 |
| const uint8_t* bytes = (const uint8_t*)src; |
| 57 |
31813 |
| utf8_dfa_state_t state = UTF8_DFA_ACCEPT; |
| 58 |
31813 |
| size_t prefix = 0; |
| 59 |
|
| |
| 60 |
278256 |
▶ | for (size_t i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t i = 0; i < len; i++) { |
277058 |
1198 |
278256 |
|
| 61 |
277058 |
| state = utf8_dfa_step(state, bytes[i]); |
| 62 |
277058 |
▶ | if (state == UTF8_DFA_ACCEPT) |
Branch: 1 branch
| Branch | True | False | Total |
| if (state == UTF8_DFA_ACCEPT) |
127751 |
149307 |
277058 |
|
| 63 |
127751 |
| prefix = i + 1; |
| 64 |
149307 |
▶ | else if (state == UTF8_DFA_REJECT) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (state == UTF8_DFA_REJECT) |
30615 |
118692 |
149307 |
|
| 65 |
30615 |
| break; |
| 66 |
|
| } |
| 67 |
31813 |
| return prefix; |
| 68 |
|
| } |
| 69 |
|
| |
| 70 |
|
| static inline bool utf8_check(const char* src, |
| 71 |
|
| size_t len, |
| 72 |
|
| size_t* cursor) { |
| 73 |
|
| const uint8_t* bytes = (const uint8_t*)src; |
| 74 |
|
| utf8_dfa_state_t state = UTF8_DFA_ACCEPT; |
| 75 |
|
| |
| 76 |
|
| state = utf8_dfa_run_dual(state, bytes, len); |
| 77 |
|
| if (state == UTF8_DFA_ACCEPT) { |
| 78 |
|
| if (cursor) |
| 79 |
|
| *cursor = len; |
| 80 |
|
| return true; |
| 81 |
|
| } |
| 82 |
|
| |
| 83 |
|
| if (cursor) |
| 84 |
|
| *cursor = utf8_maximal_prefix(src, len); |
| 85 |
|
| return false; |
| 86 |
|
| } |
| 87 |
|
| |
| 88 |
|
| static inline bool utf8_valid(const char* src, size_t len) { |
| 89 |
|
| return utf8_check(src, len, NULL); |
| 90 |
|
| } |
| 91 |
|
| |
| 92 |
37034 |
| static inline bool utf8_check_ascii(const char* src, size_t len, size_t* cursor) { |
| 93 |
|
| utf8_dfa_state_t state; |
| 94 |
|
| |
| 95 |
37034 |
| state = utf8_dfa_run_ascii(UTF8_DFA_ACCEPT, (const uint8_t *)src, len); |
| 96 |
37034 |
▶ | if (state == UTF8_DFA_ACCEPT) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (state == UTF8_DFA_ACCEPT) { |
2886 |
34148 |
37034 |
|
| 97 |
2886 |
▶ | if (cursor) |
Branch: 1 branch
| Branch | True | False | Total |
| if (cursor) |
2546 |
340 |
2886 |
|
| 98 |
2546 |
| *cursor = len; |
| 99 |
2886 |
| return true; |
| 100 |
|
| } |
| 101 |
|
| |
| 102 |
34148 |
▶ | if (cursor) |
Branch: 1 branch
| Branch | True | False | Total |
| if (cursor) |
31813 |
2335 |
34148 |
|
| 103 |
31813 |
| *cursor = utf8_maximal_prefix(src, len); |
| 104 |
34148 |
| return false; |
| 105 |
|
| } |
| 106 |
|
| |
| 107 |
2675 |
| static inline bool utf8_valid_ascii(const char *src, size_t len) { |
| 108 |
2675 |
| return utf8_check_ascii(src, len, NULL); |
| 109 |
|
| } |
| 110 |
|
| |
| 111 |
|
| #ifdef __cplusplus |
| 112 |
|
| } |
| 113 |
|
| #endif |
| 114 |
|
| #endif |