-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutf8_transcode_unsafe.h
More file actions
205 lines (196 loc) · 7.75 KB
/
Copy pathutf8_transcode_unsafe.h
File metadata and controls
205 lines (196 loc) · 7.75 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* 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_TRANSCODE_UNSAFE_H
#define UTF8_TRANSCODE_UNSAFE_H
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "utf8_transcode_common.h"
#ifdef __cplusplus
extern "C" {
#endif
/*
* utf8_transcode_utf32_unsafe -- transcode well-formed UTF-8 to UTF-32.
*
* src[0..src_len) MUST contain well-formed UTF-8. No validation is performed.
* dst[0..dst_len) receives UTF-32 code units. Both src and dst are fully
* caller-owned.
*
* Processes 8 bytes at a time; when all 8 are ASCII, widens to dst without
* decoding. Batches consecutive sequences of the same length class to avoid
* re-entering the lead-byte classifier.
*
* Returns a utf8_transcode_result_t describing the outcome:
*
* status:
* UTF8_TRANSCODE_OK src fully consumed.
* UTF8_TRANSCODE_EXHAUSTED dst full before src was consumed.
*
* consumed: bytes read from src.
* decoded: codepoints decoded from src.
* written: UTF-32 code units written to dst (equals decoded for UTF-32).
* advance: always 0.
*/
static inline utf8_transcode_result_t utf8_transcode_utf32_unsafe(const char *src,
size_t src_len,
uint32_t *dst,
size_t dst_len) {
const uint8_t *bytes = (const uint8_t *)src;
size_t r = 0, w = 0;
while (r < src_len && w < dst_len) {
unsigned int lead = bytes[r];
if (lead < 0x80u) {
while (r + 8 <= src_len && w + 8 <= dst_len) {
uint64_t v;
memcpy(&v, bytes + r, sizeof(v));
if (v & UINT64_C(0x8080808080808080))
break;
for (size_t i = 0; i < 8; i++)
dst[w + i] = bytes[r + i];
r += 8;
w += 8;
}
while (r < src_len && w < dst_len && bytes[r] < 0x80u)
dst[w++] = bytes[r++];
} else if (lead < 0xE0u) {
do {
dst[w++] = ((uint32_t)(bytes[r + 0] & 0x1Fu) << 6)
| (uint32_t)(bytes[r + 1] & 0x3Fu);
r += 2;
} while (r < src_len && w < dst_len && (unsigned)bytes[r] - 0xC0u < 0x20u);
} else if (lead < 0xF0u) {
do {
dst[w++] = ((uint32_t)(bytes[r + 0] & 0x0Fu) << 12)
| ((uint32_t)(bytes[r + 1] & 0x3Fu) << 6)
| (uint32_t)(bytes[r + 2] & 0x3Fu);
r += 3;
} while (r < src_len && w < dst_len && (unsigned)bytes[r] - 0xE0u < 0x10u);
} else {
do {
dst[w++] = ((uint32_t)(bytes[r + 0] & 0x07u) << 18)
| ((uint32_t)(bytes[r + 1] & 0x3Fu) << 12)
| ((uint32_t)(bytes[r + 2] & 0x3Fu) << 6)
| (uint32_t)(bytes[r + 3] & 0x3Fu);
r += 4;
} while (r < src_len && w < dst_len && (unsigned)bytes[r] >= 0xF0u);
}
}
utf8_transcode_status_t status = r < src_len ? UTF8_TRANSCODE_EXHAUSTED
: UTF8_TRANSCODE_OK;
return (utf8_transcode_result_t){.status = status,
.consumed = r,
.decoded = w,
.written = w,
.advance = 0};
}
/*
* utf8_transcode_utf16_unsafe -- transcode well-formed UTF-8 to UTF-16.
*
* src[0..src_len) MUST contain well-formed UTF-8. No validation is performed.
* dst[0..dst_len) receives UTF-16 code units. Codepoints above U+FFFF are
* encoded as surrogate pairs and consume two units. Both src and dst are fully
* caller-owned.
*
* Processes 8 bytes at a time; when all 8 are ASCII, widens to dst without
* decoding. Batches consecutive sequences of the same length class to avoid
* re-entering the lead-byte classifier.
*
* Returns a utf8_transcode_result_t describing the outcome:
*
* status:
* UTF8_TRANSCODE_OK src fully consumed.
* UTF8_TRANSCODE_EXHAUSTED dst full before src was consumed.
*
* consumed: bytes read from src.
* decoded: codepoints decoded from src.
* written: UTF-16 code units written to dst.
* advance: always 0.
*/
static inline utf8_transcode_result_t utf8_transcode_utf16_unsafe(const char *src,
size_t src_len,
uint16_t *dst,
size_t dst_len) {
const uint8_t *bytes = (const uint8_t *)src;
size_t r = 0, w = 0, n = 0;
while (r < src_len && w < dst_len) {
unsigned int lead = bytes[r];
if (lead < 0x80u) {
while (r + 8 <= src_len && w + 8 <= dst_len) {
uint64_t v;
memcpy(&v, bytes + r, sizeof(v));
if (v & UINT64_C(0x8080808080808080))
break;
for (size_t i = 0; i < 8; i++)
dst[w + i] = bytes[r + i];
r += 8;
w += 8;
n += 8;
}
while (r < src_len && w < dst_len && bytes[r] < 0x80u) {
dst[w++] = bytes[r++];
n++;
}
} else if (lead < 0xE0u) {
do {
dst[w++] = (uint16_t)(((uint32_t)(bytes[r + 0] & 0x1Fu) << 6)
| (uint32_t)(bytes[r + 1] & 0x3Fu));
r += 2;
n++;
} while (r < src_len && w < dst_len && (unsigned)bytes[r] - 0xC0u < 0x20u);
} else if (lead < 0xF0u) {
do {
dst[w++] = (uint16_t)(((uint32_t)(bytes[r + 0] & 0x0Fu) << 12)
| ((uint32_t)(bytes[r + 1] & 0x3Fu) << 6)
| (uint32_t)(bytes[r + 2] & 0x3Fu));
r += 3;
n++;
} while (r < src_len && w < dst_len && (unsigned)bytes[r] - 0xE0u < 0x10u);
} else {
do {
if (w + 1 >= dst_len)
goto exhausted;
uint32_t cp = ((uint32_t)(bytes[r + 0] & 0x07u) << 18)
| ((uint32_t)(bytes[r + 1] & 0x3Fu) << 12)
| ((uint32_t)(bytes[r + 2] & 0x3Fu) << 6)
| (uint32_t)(bytes[r + 3] & 0x3Fu);
cp -= 0x10000u;
dst[w++] = (uint16_t)(0xD800u + (cp >> 10));
dst[w++] = (uint16_t)(0xDC00u + (cp & 0x3FFu));
r += 4;
n++;
} while (r < src_len && (unsigned)bytes[r] >= 0xF0u);
}
}
exhausted:;
utf8_transcode_status_t status = r < src_len ? UTF8_TRANSCODE_EXHAUSTED
: UTF8_TRANSCODE_OK;
return (utf8_transcode_result_t){.status = status,
.consumed = r,
.decoded = n,
.written = w,
.advance = 0};
}
#ifdef __cplusplus
}
#endif
#endif /* UTF8_TRANSCODE_UNSAFE_H */