join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
utf16.hpp
Go to the documentation of this file.
1
25#ifndef JOIN_DATA_UTF16_HPP
26#define JOIN_DATA_UTF16_HPP
27
28// libjoin.
29#include <join/utils.hpp>
30
31// C.
32#include <cstddef>
33#include <cstdint>
34
35namespace join
36{
37 namespace utf16
38 {
44 constexpr bool isSurrogate (uint32_t unit) noexcept
45 {
46 return (unit - 0xD800) < 0x800;
47 }
48
54 constexpr bool isHighSurrogate (uint32_t unit) noexcept
55 {
56 return (unit - 0xD800) < 0x400;
57 }
58
64 constexpr bool isLowSurrogate (uint32_t unit) noexcept
65 {
66 return (unit - 0xDC00) < 0x400;
67 }
68
74 constexpr uint32_t highSurrogate (uint32_t codepoint) noexcept
75 {
76 return 0xD800 + ((codepoint - 0x10000) >> 10);
77 }
78
84 constexpr uint32_t lowSurrogate (uint32_t codepoint) noexcept
85 {
86 return 0xDC00 + (codepoint & 0x3FF);
87 }
88
95 constexpr uint32_t combine (uint32_t high, uint32_t low) noexcept
96 {
97 return 0x10000 + (((high - 0xD800) << 10) | (low - 0xDC00));
98 }
99
107 inline int decode (const char16_t*& cur, const char16_t* end, uint32_t& codepoint) noexcept
108 {
109 uint32_t u0 = cur[0];
110
111 if (JOIN_LIKELY (!isSurrogate (u0)))
112 {
113 codepoint = u0;
114 ++cur;
115 return 0;
116 }
117
118 if (JOIN_UNLIKELY (!isHighSurrogate (u0) || (end - cur) < 2 || !isLowSurrogate (cur[1])))
119 {
120 return -1;
121 }
122
123 codepoint = combine (u0, cur[1]);
124 cur += 2;
125 return 0;
126 }
127
134 inline size_t encode (uint32_t codepoint, char16_t* out) noexcept
135 {
136 if (JOIN_LIKELY (codepoint < 0x10000))
137 {
138 out[0] = static_cast<char16_t> (codepoint);
139 return 1;
140 }
141
142 out[0] = static_cast<char16_t> (highSurrogate (codepoint));
143 out[1] = static_cast<char16_t> (lowSurrogate (codepoint));
144 return 2;
145 }
146 }
147}
148
149#endif
size_t encode(uint32_t codepoint, char16_t *out) noexcept
encode one codepoint to UTF-16.
Definition utf16.hpp:134
constexpr uint32_t highSurrogate(uint32_t codepoint) noexcept
get the high surrogate of a supplementary codepoint.
Definition utf16.hpp:74
int decode(const char16_t *&cur, const char16_t *end, uint32_t &codepoint) noexcept
decode one codepoint from UTF-16.
Definition utf16.hpp:107
constexpr uint32_t combine(uint32_t high, uint32_t low) noexcept
combine a surrogate pair into a codepoint.
Definition utf16.hpp:95
constexpr bool isHighSurrogate(uint32_t unit) noexcept
check if code unit is a high surrogate.
Definition utf16.hpp:54
constexpr uint32_t lowSurrogate(uint32_t codepoint) noexcept
get the low surrogate of a supplementary codepoint.
Definition utf16.hpp:84
constexpr bool isSurrogate(uint32_t unit) noexcept
check if code unit is a surrogate.
Definition utf16.hpp:44
constexpr bool isLowSurrogate(uint32_t unit) noexcept
check if code unit is a low surrogate.
Definition utf16.hpp:64
Definition acceptor.hpp:32
#define JOIN_LIKELY(x)
Definition utils.hpp:50
#define JOIN_UNLIKELY(x)
Definition utils.hpp:51