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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include "common/assert.h"
namespace FileSys {
template <typename T>
constexpr int Strlen(const T* str) {
ASSERT(str != nullptr);
int length = 0;
while (*str++) {
++length;
}
return length;
}
template <typename T>
constexpr int Strnlen(const T* str, std::size_t count) {
return Strnlen(str, static_cast<int>(count));
}
template <typename T>
constexpr int Strnlen(const T* str, int count) {
ASSERT(str != nullptr);
ASSERT(count >= 0);
int length = 0;
while (count-- && *str++) {
++length;
}
return length;
}
template <typename T>
constexpr int Strncmp(const T* lhs, const T* rhs, std::size_t count) {
return Strncmp(lhs, rhs, static_cast<int>(count));
}
template <typename T>
constexpr int Strncmp(const T* lhs, const T* rhs, int count) {
ASSERT(lhs != nullptr);
ASSERT(rhs != nullptr);
ASSERT(count >= 0);
if (count == 0) {
return 0;
}
T l, r;
do {
l = *(lhs++);
r = *(rhs++);
} while (l && (l == r) && (--count));
return l - r;
}
template <typename T>
static constexpr int Strlcpy(T* dst, const T* src, std::size_t count) {
return Strlcpy<T>(dst, src, static_cast<int>(count));
}
template <typename T>
static constexpr int Strlcpy(T* dst, const T* src, int count) {
ASSERT(dst != nullptr);
ASSERT(src != nullptr);
const T* cur = src;
if (count > 0) {
while ((--count) && *cur) {
*(dst++) = *(cur++);
}
*dst = 0;
}
while (*cur) {
cur++;
}
return static_cast<int>(cur - src);
}
enum CharacterEncodingResult {
CharacterEncodingResult_Success = 0,
CharacterEncodingResult_InsufficientLength = 1,
CharacterEncodingResult_InvalidFormat = 2,
};
namespace impl {
class CharacterEncodingHelper {
public:
static constexpr int8_t Utf8NBytesInnerTable[0x100 + 1] = {
-1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 7, 8,
};
static constexpr char GetUtf8NBytes(size_t i) {
return static_cast<char>(Utf8NBytesInnerTable[1 + i]);
}
};
} // namespace impl
constexpr inline CharacterEncodingResult ConvertCharacterUtf8ToUtf32(u32* dst, const char* src) {
// Check pre-conditions
ASSERT(dst != nullptr);
ASSERT(src != nullptr);
// Perform the conversion
const auto* p = src;
switch (impl::CharacterEncodingHelper::GetUtf8NBytes(static_cast<unsigned char>(p[0]))) {
case 1:
*dst = static_cast<u32>(p[0]);
return CharacterEncodingResult_Success;
case 2:
if ((static_cast<u32>(p[0]) & 0x1E) != 0) {
if (impl::CharacterEncodingHelper::GetUtf8NBytes(static_cast<unsigned char>(p[1])) ==
0) {
*dst = (static_cast<u32>(p[0] & 0x1F) << 6) | (static_cast<u32>(p[1] & 0x3F) << 0);
return CharacterEncodingResult_Success;
}
}
break;
case 3:
if (impl::CharacterEncodingHelper::GetUtf8NBytes(static_cast<unsigned char>(p[1])) == 0 &&
impl::CharacterEncodingHelper::GetUtf8NBytes(static_cast<unsigned char>(p[2])) == 0) {
const u32 c = (static_cast<u32>(p[0] & 0xF) << 12) |
(static_cast<u32>(p[1] & 0x3F) << 6) |
(static_cast<u32>(p[2] & 0x3F) << 0);
if ((c & 0xF800) != 0 && (c & 0xF800) != 0xD800) {
*dst = c;
return CharacterEncodingResult_Success;
}
}
return CharacterEncodingResult_InvalidFormat;
case 4:
if (impl::CharacterEncodingHelper::GetUtf8NBytes(static_cast<unsigned char>(p[1])) == 0 &&
impl::CharacterEncodingHelper::GetUtf8NBytes(static_cast<unsigned char>(p[2])) == 0 &&
impl::CharacterEncodingHelper::GetUtf8NBytes(static_cast<unsigned char>(p[3])) == 0) {
const u32 c =
(static_cast<u32>(p[0] & 0x7) << 18) | (static_cast<u32>(p[1] & 0x3F) << 12) |
(static_cast<u32>(p[2] & 0x3F) << 6) | (static_cast<u32>(p[3] & 0x3F) << 0);
if (c >= 0x10000 && c < 0x110000) {
*dst = c;
return CharacterEncodingResult_Success;
}
}
return CharacterEncodingResult_InvalidFormat;
default:
break;
}
// We failed to convert
return CharacterEncodingResult_InvalidFormat;
}
constexpr inline CharacterEncodingResult PickOutCharacterFromUtf8String(char* dst,
const char** str) {
// Check pre-conditions
ASSERT(dst != nullptr);
ASSERT(str != nullptr);
ASSERT(*str != nullptr);
// Clear the output
dst[0] = 0;
dst[1] = 0;
dst[2] = 0;
dst[3] = 0;
// Perform the conversion
const auto* p = *str;
u32 c = static_cast<u32>(*p);
switch (impl::CharacterEncodingHelper::GetUtf8NBytes(c)) {
case 1:
dst[0] = (*str)[0];
++(*str);
break;
case 2:
if ((p[0] & 0x1E) != 0) {
if (impl::CharacterEncodingHelper::GetUtf8NBytes(static_cast<unsigned char>(p[1])) ==
0) {
c = (static_cast<u32>(p[0] & 0x1F) << 6) | (static_cast<u32>(p[1] & 0x3F) << 0);
dst[0] = (*str)[0];
dst[1] = (*str)[1];
(*str) += 2;
break;
}
}
return CharacterEncodingResult_InvalidFormat;
case 3:
if (impl::CharacterEncodingHelper::GetUtf8NBytes(static_cast<unsigned char>(p[1])) == 0 &&
impl::CharacterEncodingHelper::GetUtf8NBytes(static_cast<unsigned char>(p[2])) == 0) {
c = (static_cast<u32>(p[0] & 0xF) << 12) | (static_cast<u32>(p[1] & 0x3F) << 6) |
(static_cast<u32>(p[2] & 0x3F) << 0);
if ((c & 0xF800) != 0 && (c & 0xF800) != 0xD800) {
dst[0] = (*str)[0];
dst[1] = (*str)[1];
dst[2] = (*str)[2];
(*str) += 3;
break;
}
}
return CharacterEncodingResult_InvalidFormat;
case 4:
if (impl::CharacterEncodingHelper::GetUtf8NBytes(static_cast<unsigned char>(p[1])) == 0 &&
impl::CharacterEncodingHelper::GetUtf8NBytes(static_cast<unsigned char>(p[2])) == 0 &&
impl::CharacterEncodingHelper::GetUtf8NBytes(static_cast<unsigned char>(p[3])) == 0) {
c = (static_cast<u32>(p[0] & 0x7) << 18) | (static_cast<u32>(p[1] & 0x3F) << 12) |
(static_cast<u32>(p[2] & 0x3F) << 6) | (static_cast<u32>(p[3] & 0x3F) << 0);
if (c >= 0x10000 && c < 0x110000) {
dst[0] = (*str)[0];
dst[1] = (*str)[1];
dst[2] = (*str)[2];
dst[3] = (*str)[3];
(*str) += 4;
break;
}
}
return CharacterEncodingResult_InvalidFormat;
default:
return CharacterEncodingResult_InvalidFormat;
}
return CharacterEncodingResult_Success;
}
} // namespace FileSys
|