7#ifndef BOOST_LOCALE_UTF_HPP_INCLUDED
8#define BOOST_LOCALE_UTF_HPP_INCLUDED
10#include <boost/locale/config.hpp>
13namespace boost {
namespace locale {
34 if(0xD800 <= v && v <= 0xDFFF)
39#ifdef BOOST_LOCALE_DOXYGEN
42 template<
typename CharType,
int size = sizeof(CharType)>
59 template<
typename Iterator>
92 template<
typename Iterator>
98 template<
typename Iterator>
104 template<
typename CharType,
int size = sizeof(CharType)>
107 template<
typename CharType>
113 unsigned char c = ci;
116 if(BOOST_UNLIKELY(c < 194))
122 if(BOOST_LIKELY(c <= 244))
133 else if(value <= 0x7FF)
135 else if(BOOST_LIKELY(value <= 0xFFFF))
143 unsigned char c = ci;
144 return (c & 0xC0) == 0x80;
149 template<
typename Iterator>
152 if(BOOST_UNLIKELY(p == e))
155 unsigned char lead = *p++;
160 if(BOOST_UNLIKELY(trail_size < 0))
168 code_point c = lead & ((1 << (6 - trail_size)) - 1);
174 if(BOOST_UNLIKELY(p == e))
179 c = (c << 6) | (tmp & 0x3F);
182 if(BOOST_UNLIKELY(p == e))
187 c = (c << 6) | (tmp & 0x3F);
190 if(BOOST_UNLIKELY(p == e))
195 c = (c << 6) | (tmp & 0x3F);
204 if(BOOST_UNLIKELY(
width(c) != trail_size + 1))
210 template<
typename Iterator>
213 unsigned char lead = *p++;
221 else if(BOOST_LIKELY(lead < 240))
226 code_point c = lead & ((1 << (6 - trail_size)) - 1);
229 case 3: c = (c << 6) | (static_cast<unsigned char>(*p++) & 0x3F); BOOST_FALLTHROUGH;
230 case 2: c = (c << 6) | (static_cast<unsigned char>(*p++) & 0x3F); BOOST_FALLTHROUGH;
231 case 1: c = (c << 6) | (static_cast<unsigned char>(*p++) & 0x3F);
237 template<
typename Iterator>
242 else if(value <= 0x7FF) {
243 *out++ =
static_cast<char_type>((value >> 6) | 0xC0);
244 *out++ =
static_cast<char_type>((value & 0x3F) | 0x80);
245 }
else if(BOOST_LIKELY(value <= 0xFFFF)) {
246 *out++ =
static_cast<char_type>((value >> 12) | 0xE0);
247 *out++ =
static_cast<char_type>(((value >> 6) & 0x3F) | 0x80);
248 *out++ =
static_cast<char_type>((value & 0x3F) | 0x80);
250 *out++ =
static_cast<char_type>((value >> 18) | 0xF0);
251 *out++ =
static_cast<char_type>(((value >> 12) & 0x3F) | 0x80);
252 *out++ =
static_cast<char_type>(((value >> 6) & 0x3F) | 0x80);
253 *out++ =
static_cast<char_type>((value & 0x3F) | 0x80);
259 template<
typename CharType>
260 struct utf_traits<CharType, 2> {
264 static bool is_first_surrogate(uint16_t x) {
return 0xD800 <= x && x <= 0xDBFF; }
265 static bool is_second_surrogate(uint16_t x) {
return 0xDC00 <= x && x <= 0xDFFF; }
266 static code_point combine_surrogate(uint16_t w1, uint16_t w2)
268 return ((
code_point(w1 & 0x3FF) << 10) | (w2 & 0x3FF)) + 0x10000;
272 if(is_first_surrogate(c))
274 if(is_second_surrogate(c))
284 template<
typename It>
287 if(BOOST_UNLIKELY(current == last))
289 uint16_t w1 = *current++;
290 if(BOOST_LIKELY(w1 < 0xD800 || 0xDFFF < w1))
296 uint16_t w2 = *current++;
297 if(w2 < 0xDC00 || 0xDFFF < w2)
299 return combine_surrogate(w1, w2);
301 template<
typename It>
304 uint16_t w1 = *current++;
305 if(BOOST_LIKELY(w1 < 0xD800 || 0xDFFF < w1))
307 uint16_t w2 = *current++;
308 return combine_surrogate(w1, w2);
313 template<
typename It>
316 if(BOOST_LIKELY(u <= 0xFFFF))
320 *out++ =
static_cast<char_type>(0xD800 | (u >> 10));
321 *out++ =
static_cast<char_type>(0xDC00 | (u & 0x3FF));
327 template<
typename CharType>
328 struct utf_traits<CharType, 4> {
339 template<
typename It>
345 template<
typename It>
348 if(BOOST_UNLIKELY(current == last))
357 template<
typename It>
uint32_t code_point
The integral type that can hold a Unicode code point.
Definition: utf.hpp:19
bool is_valid_codepoint(code_point v)
the function checks if v is a valid code point
Definition: utf.hpp:30
code_point len_or_error
Either a length/size or an error (illegal/incomplete)
Definition: utf.hpp:27
constexpr code_point illegal
Special constant that defines illegal code point.
Definition: utf.hpp:22
constexpr code_point incomplete
Special constant that defines incomplete code point.
Definition: utf.hpp:24
UTF Traits class - functions to convert UTF sequences to and from Unicode code points.
Definition: utf.hpp:43
static bool is_lead(char_type c)
Returns true if c is lead code unit, always true of UTF-32.
static int width(code_point value)
static int trail_length(char_type c)
static code_point decode_valid(Iterator &p)
static code_point decode(Iterator &p, Iterator e)
static constexpr int max_width
Definition: utf.hpp:67
CharType char_type
The type of the character.
Definition: utf.hpp:45
static Iterator encode(code_point value, Iterator out)
static bool is_trail(char_type c)
Returns true if c is trail code unit, always false for UTF-32.