25#ifndef JOIN_DATA_UTF8_HPP
26#define JOIN_DATA_UTF8_HPP
59 ,
lower{0x80, 0xA0, 0x80, 0x90, 0x80}
60 ,
span{0x3F, 0x1F, 0x1F, 0x2F, 0x0F}
62 for (
unsigned i = 0xC2; i < 0xE0; ++i)
66 for (
unsigned i = 0xE1; i < 0xF0; ++i)
70 for (
unsigned i = 0xF1; i < 0xF4; ++i)
74 data[0xE0] = 3 | (1 << 3);
75 data[0xED] = 3 | (2 << 3);
76 data[0xF0] = 4 | (3 << 3);
77 data[0xF4] = 4 | (4 << 3);
99 for (
unsigned i = 0; i < 0xEE; ++i)
101 data[i] =
static_cast<uint8_t
> (i);
103 for (
unsigned i = 0xF0; i < 0x100; ++i)
105 data[i] =
static_cast<uint8_t
> (i - 2);
120 constexpr uint64_t
highBits = 0x8080808080808080ULL;
131 uint32_t n = entry & 7;
133 if (
JOIN_UNLIKELY (n == 0 ||
static_cast<size_t> (end - cur) < n))
138 uint32_t range = entry >> 3;
143 bad |= (n >= 3 ? (cur[2] & 0xC0) ^ 0x80 : 0) | (n == 4 ? (cur[3] & 0xC0) ^ 0x80 : 0);
147 for (uint32_t i = 2; i < n; ++i)
149 bad |= (cur[i] & 0xC0) ^ 0x80;
169 std::memcpy (&word, cur, 8);
192 while (cur < end && *cur >= 0x80);
206 inline int decode (
const char*& cur,
const char* end, uint32_t& codepoint)
noexcept
208 const uint8_t* p =
reinterpret_cast<const uint8_t*
> (cur);
223 uint32_t cp = p[0] & (0xFF >> (n + 1));
224 for (uint32_t i = 1; i < n; ++i)
226 cp = (cp << 6) | (p[i] & 0x3F);
240 inline size_t encode (uint32_t codepoint,
char* out)
noexcept
244 out[0] =
static_cast<char> (codepoint);
248 if (codepoint < 0x800)
250 out[0] =
static_cast<char> (0xC0 | (codepoint >> 6));
251 out[1] =
static_cast<char> (0x80 | (codepoint & 0x3F));
255 if (codepoint < 0x10000)
257 out[0] =
static_cast<char> (0xE0 | (codepoint >> 12));
258 out[1] =
static_cast<char> (0x80 | ((codepoint >> 6) & 0x3F));
259 out[2] =
static_cast<char> (0x80 | (codepoint & 0x3F));
263 out[0] =
static_cast<char> (0xF0 | (codepoint >> 18));
264 out[1] =
static_cast<char> (0x80 | ((codepoint >> 12) & 0x3F));
265 out[2] =
static_cast<char> (0x80 | ((codepoint >> 6) & 0x3F));
266 out[3] =
static_cast<char> (0x80 | (codepoint & 0x3F));
276 inline int validate (
const char* first,
const char* last)
noexcept
278 const uint8_t* cur =
reinterpret_cast<const uint8_t*
> (first);
279 const uint8_t* end =
reinterpret_cast<const uint8_t*
> (last);
285 for (
const uint8_t* p = cur; p < end; ++p)
292 while (end - cur >= 8)
294 std::memcpy (&word, cur, 8);
302 std::memcpy (&word, end - 8, 8);
315 const char* last2)
noexcept
317 auto mis = std::mismatch (first1, last1, first2, last2);
319 if (mis.first == last1)
321 return (mis.second == last2) ? 0 : -1;
324 if (mis.second == last2)
int validateMixed(const uint8_t *cur, const uint8_t *end) noexcept
validate UTF-8 input that may contain multibyte sequences.
Definition utf8.hpp:162
constexpr uint64_t highBits
mask of the high bit of each byte of a 64 bits word.
Definition utf8.hpp:120
uint32_t sequenceLength(const uint8_t *cur, const uint8_t *end) noexcept
get the length of a multibyte sequence.
Definition utf8.hpp:128
constexpr OrderTable orderLookup
UTF-16 code unit order lookup table.
Definition utf8.hpp:115
constexpr SequenceTable sequenceLookup
UTF-8 multibyte sequences lookup table.
Definition utf8.hpp:84
int compareUtf16Order(const char *first1, const char *last1, const char *first2, const char *last2) noexcept
compare two UTF-8 inputs in UTF-16 code unit order.
Definition utf8.hpp:314
int validate(const char *first, const char *last) noexcept
validate UTF-8 input.
Definition utf8.hpp:276
int decode(const char *&cur, const char *end, uint32_t &codepoint) noexcept
decode one codepoint from UTF-8.
Definition utf8.hpp:206
size_t encode(uint32_t codepoint, char *out) noexcept
encode one codepoint to UTF-8.
Definition utf8.hpp:240
Definition acceptor.hpp:32
rank of UTF-8 bytes in UTF-16 code unit order.
Definition utf8.hpp:90
constexpr OrderTable()
create the lookup table.
Definition utf8.hpp:96
uint8_t data[256]
Definition utf8.hpp:91
UTF-8 well-formed multibyte sequences indexed by lead byte.
Definition utf8.hpp:49
constexpr SequenceTable()
create the lookup table.
Definition utf8.hpp:57
uint8_t span[8]
Definition utf8.hpp:52
uint8_t data[256]
Definition utf8.hpp:50
uint8_t lower[8]
Definition utf8.hpp:51
#define JOIN_LIKELY(x)
Definition utils.hpp:50
#define JOIN_UNLIKELY(x)
Definition utils.hpp:51