-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutf8_advance_forward.h
More file actions
144 lines (133 loc) · 5.39 KB
/
utf8_advance_forward.h
File metadata and controls
144 lines (133 loc) · 5.39 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
/*
* Copyright (c) 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_ADVANCE_FORWARD_H
#define UTF8_ADVANCE_FORWARD_H
#include <stddef.h>
#include <stdint.h>
#include <string.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_advance_forward.h"
#endif
#ifdef __cplusplus
extern "C" {
#endif
/*
* utf8_advance_forward -- advance forward by distance codepoints.
*
* Returns the byte offset of the start of the codepoint distance positions
* ahead in src[0..len), or len if distance exceeds the number of codepoints
* in the buffer. Returns (size_t)-1 if src[0..len) contains ill-formed UTF-8.
* If advanced is non-NULL, writes the number of codepoints actually advanced
* before stopping.
*/
static inline size_t utf8_advance_forward(const char *src,
size_t len,
size_t distance,
size_t *advanced) {
const uint8_t *bytes = (const uint8_t *)src;
utf8_dfa_state_t state = UTF8_DFA_ACCEPT;
size_t pos = 0;
size_t count = 0;
while (distance - count >= 4 && len - pos >= 4) {
utf8_dfa_state_t s0 = utf8_dfa_step(state, bytes[pos + 0]);
utf8_dfa_state_t s1 = utf8_dfa_step(s0, bytes[pos + 1]);
utf8_dfa_state_t s2 = utf8_dfa_step(s1, bytes[pos + 2]);
utf8_dfa_state_t s3 = utf8_dfa_step(s2, bytes[pos + 3]);
count += (s0 == UTF8_DFA_ACCEPT) + (s1 == UTF8_DFA_ACCEPT)
+ (s2 == UTF8_DFA_ACCEPT) + (s3 == UTF8_DFA_ACCEPT);
state = s3;
pos += 4;
}
while (pos < len && count < distance) {
state = utf8_dfa_step(state, bytes[pos++]);
if (state == UTF8_DFA_ACCEPT)
count++;
}
if (advanced)
*advanced = count;
return state == UTF8_DFA_ACCEPT ? pos : (size_t)-1;
}
/*
* utf8_advance_forward_ascii -- advance forward by distance codepoints
* with an ASCII fast path.
*
* Processes 8 bytes at a time; when all 8 are ASCII and the DFA is in the
* accept state, increments count by 8 without entering the DFA. Falls back
* to byte-wise DFA stepping otherwise.
*
* Returns the byte offset of the start of the codepoint distance positions
* ahead in src[0..len), or len if distance exceeds the number of codepoints
* in the buffer. Returns (size_t)-1 if src[0..len) contains ill-formed UTF-8.
* If advanced is non-NULL, writes the number of codepoints actually advanced
* before stopping.
*/
static inline size_t utf8_advance_forward_ascii(const char *src,
size_t len,
size_t distance,
size_t *advanced) {
const uint8_t *bytes = (const uint8_t *)src;
utf8_dfa_state_t state = UTF8_DFA_ACCEPT;
size_t pos = 0;
size_t count = 0;
while (distance - count >= 8 && len - pos >= 8) {
if (state == UTF8_DFA_ACCEPT) {
uint64_t v;
memcpy(&v, bytes + pos, sizeof(v));
if ((v & UINT64_C(0x8080808080808080)) == 0) {
count += 8;
pos += 8;
continue;
}
}
{
utf8_dfa_state_t s0 = utf8_dfa_step(state, bytes[pos + 0]);
utf8_dfa_state_t s1 = utf8_dfa_step(s0, bytes[pos + 1]);
utf8_dfa_state_t s2 = utf8_dfa_step(s1, bytes[pos + 2]);
utf8_dfa_state_t s3 = utf8_dfa_step(s2, bytes[pos + 3]);
utf8_dfa_state_t s4 = utf8_dfa_step(s3, bytes[pos + 4]);
utf8_dfa_state_t s5 = utf8_dfa_step(s4, bytes[pos + 5]);
utf8_dfa_state_t s6 = utf8_dfa_step(s5, bytes[pos + 6]);
utf8_dfa_state_t s7 = utf8_dfa_step(s6, bytes[pos + 7]);
count += (s0 == UTF8_DFA_ACCEPT) + (s1 == UTF8_DFA_ACCEPT)
+ (s2 == UTF8_DFA_ACCEPT) + (s3 == UTF8_DFA_ACCEPT)
+ (s4 == UTF8_DFA_ACCEPT) + (s5 == UTF8_DFA_ACCEPT)
+ (s6 == UTF8_DFA_ACCEPT) + (s7 == UTF8_DFA_ACCEPT);
state = s7;
}
pos += 8;
}
while (pos < len && count < distance) {
state = utf8_dfa_step(state, bytes[pos++]);
if (state == UTF8_DFA_ACCEPT)
count++;
}
if (advanced)
*advanced = count;
return state == UTF8_DFA_ACCEPT ? pos : (size_t)-1;
}
#ifdef __cplusplus
}
#endif
#endif /* UTF8_ADVANCE_FORWARD_H */