-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutf8_valid.h
More file actions
188 lines (160 loc) · 5.5 KB
/
Copy pathutf8_valid.h
File metadata and controls
188 lines (160 loc) · 5.5 KB
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
/*
* Copyright (c) 2017-2026 Christian Hansen <chansen@cpan.org>
* <https://github.com/chansen/c-utf8>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef UTF8_VALID_H
#define UTF8_VALID_H
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#if defined(UTF8_DFA32_H) && defined(UTF8_DFA64_H)
# error "utf8_dfa32.h and utf8_dfa64.h are mutually exclusive"
#elif !defined(UTF8_DFA32_H) && !defined(UTF8_DFA64_H)
# error "include utf8_dfa32.h or utf8_dfa64.h before utf8_valid.h"
#endif
#ifndef UTF8_VALID_BOUNDED_BLOCK_SIZE
# define UTF8_VALID_BOUNDED_BLOCK_SIZE 256
#endif
#ifdef __cplusplus
extern "C" {
#endif
static inline size_t utf8_maximal_subpart(const char* src, size_t len) {
const uint8_t* bytes = (const uint8_t*)src;
utf8_dfa_state_t state = UTF8_DFA_ACCEPT;
for (size_t i = 0; i < len; i++) {
state = utf8_dfa_step(state, bytes[i]);
switch (state) {
case UTF8_DFA_ACCEPT:
return i + 1;
case UTF8_DFA_REJECT:
return i > 0 ? i : 1;
}
}
return len;
}
static inline size_t utf8_maximal_prefix(const char* src, size_t len) {
const uint8_t* bytes = (const uint8_t*)src;
utf8_dfa_state_t state = UTF8_DFA_ACCEPT;
size_t prefix = 0;
for (size_t i = 0; i < len; i++) {
state = utf8_dfa_step(state, bytes[i]);
if (state == UTF8_DFA_ACCEPT)
prefix = i + 1;
else if (state == UTF8_DFA_REJECT)
break;
}
return prefix;
}
static inline bool utf8_check(const char* src,
size_t len,
size_t* cursor) {
const uint8_t* bytes = (const uint8_t*)src;
utf8_dfa_state_t state = UTF8_DFA_ACCEPT;
state = utf8_dfa_run_dual(state, bytes, len);
if (state == UTF8_DFA_ACCEPT) {
if (cursor)
*cursor = len;
return true;
}
if (cursor)
*cursor = utf8_maximal_prefix(src, len);
return false;
}
static inline bool utf8_valid(const char* src, size_t len) {
return utf8_check(src, len, NULL);
}
static inline bool utf8_check_bounded(const char* src,
size_t slen,
size_t* cursor) {
const uint8_t* bytes = (const uint8_t*)src;
size_t len = slen;
size_t checkpoint = 0;
utf8_dfa_state_t state = UTF8_DFA_ACCEPT;
const size_t block_size = UTF8_VALID_BOUNDED_BLOCK_SIZE;
for (; len >= block_size; bytes += block_size, len -= block_size) {
state = utf8_dfa_run_dual(state, bytes, block_size);
if (state == UTF8_DFA_REJECT)
break;
if (state == UTF8_DFA_ACCEPT)
checkpoint = (size_t)(bytes - (const uint8_t*)src);
}
if (state != UTF8_DFA_REJECT)
state = utf8_dfa_run_dual(state, bytes, len);
if (state == UTF8_DFA_ACCEPT) {
if (cursor)
*cursor = slen;
return true;
}
if (cursor)
*cursor = checkpoint + utf8_maximal_prefix(src + checkpoint, slen - checkpoint);
return false;
}
static inline bool utf8_valid_bounded(const char* src, size_t len) {
return utf8_check_bounded(src, len, NULL);
}
static inline bool utf8_check_ascii(const char* src, size_t len, size_t* cursor) {
utf8_dfa_state_t state;
state = utf8_dfa_run_ascii(UTF8_DFA_ACCEPT, (const uint8_t *)src, len);
if (state == UTF8_DFA_ACCEPT) {
if (cursor)
*cursor = len;
return true;
}
if (cursor)
*cursor = utf8_maximal_prefix(src, len);
return false;
}
static inline bool utf8_valid_ascii(const char *src, size_t len) {
return utf8_check_ascii(src, len, NULL);
}
static inline bool utf8_check_ascii_bounded(const char* src,
size_t slen,
size_t* cursor) {
const uint8_t* bytes = (const uint8_t*)src;
size_t len = slen;
size_t checkpoint = 0;
utf8_dfa_state_t state = UTF8_DFA_ACCEPT;
const size_t block_size = UTF8_VALID_BOUNDED_BLOCK_SIZE;
for (; len >= block_size; bytes += block_size, len -= block_size) {
state = utf8_dfa_run_ascii(state, bytes, block_size);
if (state == UTF8_DFA_REJECT)
break;
if (state == UTF8_DFA_ACCEPT)
checkpoint = (size_t)(bytes - (const uint8_t*)src);
}
if (state != UTF8_DFA_REJECT)
state = utf8_dfa_run_ascii(state, bytes, len);
if (state == UTF8_DFA_ACCEPT) {
if (cursor)
*cursor = slen;
return true;
}
if (cursor)
*cursor = checkpoint + utf8_maximal_prefix(src + checkpoint, slen - checkpoint);
return false;
}
static inline bool utf8_valid_ascii_bounded(const char* src, size_t len) {
return utf8_check_ascii_bounded(src, len, NULL);
}
#ifdef __cplusplus
}
#endif
#endif