-
Notifications
You must be signed in to change notification settings - Fork 70
/
code_point.hpp
310 lines (260 loc) · 9.41 KB
/
code_point.hpp
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Copyright (C) 2020-2022 Jonathan Müller and lexy contributors
// SPDX-License-Identifier: BSL-1.0
#ifndef LEXY_CODE_POINT_HPP_INCLUDED
#define LEXY_CODE_POINT_HPP_INCLUDED
#include <cstdint>
#include <lexy/_detail/assert.hpp>
#include <lexy/_detail/config.hpp>
#ifndef LEXY_HAS_UNICODE_DATABASE
# define LEXY_HAS_UNICODE_DATABASE 0
#endif
#if LEXY_HAS_UNICODE_DATABASE
# define LEXY_UNICODE_CONSTEXPR constexpr
#else
# define LEXY_UNICODE_CONSTEXPR
#endif
namespace lexy
{
/// A unicode code point.
class code_point
{
public:
constexpr code_point() noexcept : _value(0xFFFF'FFFF) {}
constexpr explicit code_point(char32_t value) noexcept : _value(value) {}
constexpr auto value() const noexcept
{
return _value;
}
//=== classification ===//
constexpr bool is_ascii() const noexcept
{
return _value <= 0x7F;
}
constexpr bool is_bmp() const noexcept
{
return _value <= 0xFFFF;
}
constexpr bool is_valid() const noexcept
{
return _value <= 0x10'FFFF;
}
constexpr bool is_control() const noexcept
{
return _value <= 0x1F || (0x7F <= _value && _value <= 0x9F);
}
constexpr bool is_surrogate() const noexcept
{
return 0xD800 <= _value && _value <= 0xDFFF;
}
constexpr bool is_private_use() const noexcept
{
return (0xE000 <= _value && _value <= 0xF8FF)
|| (0x0F'0000 <= _value && _value <= 0x0F'FFFD)
|| (0x10'0000 <= _value && _value <= 0x10'FFFD);
}
constexpr bool is_noncharacter() const noexcept
{
// Contiguous range of 32 non-characters.
if (0xFDD0 <= _value && _value <= 0xFDEF)
return true;
// Last two code points of every plane.
auto in_plane = _value & 0xFFFF;
return in_plane == 0xFFFE || in_plane == 0xFFFF;
}
constexpr bool is_scalar() const noexcept
{
return is_valid() && !is_surrogate();
}
//=== general category ===//
enum general_category_t
{
// NOLINTNEXTLINE: can't use parentheses here
#define LEXY_UNICODE_CATEGORY(Short, Long) Short, Long = Short
LEXY_UNICODE_CATEGORY(Lu, uppercase_letter),
LEXY_UNICODE_CATEGORY(Ll, lowercase_letter),
LEXY_UNICODE_CATEGORY(Lt, titlecase_letter),
LEXY_UNICODE_CATEGORY(Lm, modifier_letter),
LEXY_UNICODE_CATEGORY(Lo, other_letter),
LEXY_UNICODE_CATEGORY(Mn, nonspacing_mark),
LEXY_UNICODE_CATEGORY(Mc, spaing_mark),
LEXY_UNICODE_CATEGORY(Me, enclosing_mark),
LEXY_UNICODE_CATEGORY(Nd, decimal_number),
LEXY_UNICODE_CATEGORY(Nl, letter_number),
LEXY_UNICODE_CATEGORY(No, other_number),
LEXY_UNICODE_CATEGORY(Pc, connector_punctuation),
LEXY_UNICODE_CATEGORY(Pd, dash_punctuation),
LEXY_UNICODE_CATEGORY(Ps, open_punctuation),
LEXY_UNICODE_CATEGORY(Pe, closing_punctuation),
LEXY_UNICODE_CATEGORY(Pi, initial_puncutation),
LEXY_UNICODE_CATEGORY(Pf, final_puncutation),
LEXY_UNICODE_CATEGORY(Po, other_punctuation),
LEXY_UNICODE_CATEGORY(Sm, math_symbol),
LEXY_UNICODE_CATEGORY(Sc, currency_symbol),
LEXY_UNICODE_CATEGORY(Sk, modifier_symbol),
LEXY_UNICODE_CATEGORY(So, other_symbol),
LEXY_UNICODE_CATEGORY(Zs, space_separator),
LEXY_UNICODE_CATEGORY(Zl, line_separator),
LEXY_UNICODE_CATEGORY(Zp, paragraph_separator),
LEXY_UNICODE_CATEGORY(Cc, control),
LEXY_UNICODE_CATEGORY(Cf, format),
LEXY_UNICODE_CATEGORY(Cs, surrogate),
LEXY_UNICODE_CATEGORY(Co, private_use),
LEXY_UNICODE_CATEGORY(Cn, unassigned),
#undef LEXY_UNICODE_CATEGORY
};
template <general_category_t... Cats>
struct _gc_group
{
const char* name;
friend constexpr bool operator==(_gc_group, general_category_t cat)
{
return ((cat == Cats) || ...);
}
friend constexpr bool operator==(general_category_t cat, _gc_group)
{
return ((cat == Cats) || ...);
}
friend constexpr bool operator!=(_gc_group, general_category_t cat)
{
return !(_gc_group{} == cat);
}
friend constexpr bool operator!=(general_category_t cat, _gc_group)
{
return !(_gc_group{} == cat);
}
};
#define LEXY_UNICODE_CATEGORY_GROUP(Name, Short, Long, ...) \
static constexpr _gc_group<__VA_ARGS__> Short{"code-point." Name}; \
static constexpr _gc_group<__VA_ARGS__> Long = Short
LEXY_UNICODE_CATEGORY_GROUP("cased-letter", LC, cased_letter, Lu, Ll, Lt);
LEXY_UNICODE_CATEGORY_GROUP("letter", L, letter, Lu, Ll, Lt, Lm, Lo);
LEXY_UNICODE_CATEGORY_GROUP("mark", M, mark, Mn, Mc, Me);
LEXY_UNICODE_CATEGORY_GROUP("number", N, number, Nd, Nl, No);
LEXY_UNICODE_CATEGORY_GROUP("punctuation", P, punctuation, Pc, Pd, Ps, Pe, Pi, Pf, Po);
LEXY_UNICODE_CATEGORY_GROUP("symbol", S, symbol, Sm, Sc, Sk, So);
LEXY_UNICODE_CATEGORY_GROUP("separator", Z, separator, Zs, Zl, Zp);
LEXY_UNICODE_CATEGORY_GROUP("other", C, other, Cc, Cf, Cs, Co, Cn);
#undef LEXY_UNICODE_CATEGORY_GROUP
LEXY_UNICODE_CONSTEXPR general_category_t general_category() const noexcept;
//=== comparision ===//
friend constexpr bool operator==(code_point lhs, code_point rhs) noexcept
{
return lhs._value == rhs._value;
}
friend constexpr bool operator!=(code_point lhs, code_point rhs) noexcept
{
return lhs._value != rhs._value;
}
private:
char32_t _value;
};
LEXY_UNICODE_CONSTEXPR code_point simple_case_fold(code_point cp) noexcept;
} // namespace lexy
namespace lexy::_detail
{
constexpr const char* general_category_name(lexy::code_point::general_category_t category)
{
switch (category)
{
case lexy::code_point::Lu:
return "code-point.uppercase-letter";
case lexy::code_point::Ll:
return "code-point.lowercase-letter";
case lexy::code_point::Lt:
return "code-point.titlecase-letter";
case lexy::code_point::Lm:
return "code-point.modifier-letter";
case lexy::code_point::Lo:
return "code-point.other-letter";
case lexy::code_point::Mn:
return "code-point.nonspacing-mark";
case lexy::code_point::Mc:
return "code-point.combining-mark";
case lexy::code_point::Me:
return "code-point.enclosing-mark";
case lexy::code_point::Nd:
return "code-point.decimal-number";
case lexy::code_point::Nl:
return "code-point.letter-number";
case lexy::code_point::No:
return "code-point.other-number";
case lexy::code_point::Pc:
return "code-point.connector-punctuation";
case lexy::code_point::Pd:
return "code-point.dash-punctuation";
case lexy::code_point::Ps:
return "code-point.open-punctuation";
case lexy::code_point::Pe:
return "code-point.close-punctuation";
case lexy::code_point::Pi:
return "code-point.initial-quote-punctuation";
case lexy::code_point::Pf:
return "code-point.final-quote-punctuation";
case lexy::code_point::Po:
return "code-point.other-punctuation";
case lexy::code_point::Sm:
return "code-point.math-symbol";
case lexy::code_point::Sc:
return "code-point.currency-symbol";
case lexy::code_point::Sk:
return "code-point.modifier-symbol";
case lexy::code_point::So:
return "code-point.other-symbol";
case lexy::code_point::Zs:
return "code-point.space-separator";
case lexy::code_point::Zl:
return "code-point.line-separator";
case lexy::code_point::Zp:
return "code-point.paragraph-separator";
case lexy::code_point::Cc:
return "code-point.control";
case lexy::code_point::Cf:
return "code-point.format";
case lexy::code_point::Cs:
return "code-point.surrogate";
case lexy::code_point::Co:
return "code-point.private-use";
case lexy::code_point::Cn:
return "code-point.not-assigned";
}
return nullptr; // unreachable
}
} // namespace lexy::_detail
#if LEXY_HAS_UNICODE_DATABASE
# include <lexy/_detail/unicode_database.hpp>
constexpr lexy::code_point::general_category_t lexy::code_point::general_category() const noexcept
{
if (!is_valid())
return general_category_t::unassigned;
auto idx = _unicode_db::property_index(_value);
return _unicode_db::category[idx];
}
constexpr lexy::code_point lexy::simple_case_fold(code_point cp) noexcept
{
if (!cp.is_valid())
return cp;
auto idx = _unicode_db::property_index(cp.value());
auto offset = _unicode_db::case_folding_offset[idx];
return code_point(char32_t(std::int_least32_t(cp.value()) + offset));
}
namespace lexy::_detail
{
template <lexy::_unicode_db::binary_properties_t... Props>
LEXY_FORCE_INLINE constexpr bool code_point_has_properties(char32_t cp)
{
constexpr auto mask = ((1 << Props) | ...);
auto idx = _unicode_db::property_index(cp);
auto props = _unicode_db::binary_properties[idx];
return (props & mask) != 0;
}
} // namespace lexy::_detail
# define LEXY_UNICODE_PROPERTY(Name) ::lexy::_unicode_db::Name
#else
namespace lexy::_detail
{
template <int... Props>
bool code_point_has_properties(char32_t cp); // not implemented
} // namespace lexy::_detail
# define LEXY_UNICODE_PROPERTY(Name) 0
#endif
#endif // LEXY_CODE_POINT_HPP_INCLUDED