123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454 |
- #ifndef BOOST_NOWIDE_UTF_HPP_INCLUDED
- #define BOOST_NOWIDE_UTF_HPP_INCLUDED
- #include <boost/nowide/config.hpp>
- #include <cstdint>
- namespace boost {
- namespace nowide {
-
-
-
-
-
-
- namespace utf {
-
-
-
- using code_point = uint32_t;
-
-
-
- static const code_point illegal = 0xFFFFFFFFu;
-
-
-
- static const code_point incomplete = 0xFFFFFFFEu;
-
-
-
- inline bool is_valid_codepoint(code_point v)
- {
- if(v > 0x10FFFF)
- return false;
- if(0xD800 <= v && v <= 0xDFFF)
- return false;
- return true;
- }
- #ifdef BOOST_NOWIDE_DOXYGEN
-
-
-
- template<typename CharType, int size = sizeof(CharType)>
- struct utf_traits
- {
-
-
-
- using char_type = CharType;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- template<typename Iterator>
- static code_point decode(Iterator& p, Iterator e);
-
-
-
-
-
-
-
- static const int max_width;
-
-
-
-
-
-
- static int width(code_point value);
-
-
-
-
-
- static int trail_length(char_type c);
-
-
-
- static bool is_trail(char_type c);
-
-
-
- static bool is_lead(char_type c);
-
-
-
-
-
-
-
-
-
-
- template<typename Iterator>
- static Iterator encode(code_point value, Iterator out);
-
-
-
-
-
- template<typename Iterator>
- static code_point decode_valid(Iterator& p);
- };
- #else
- template<typename CharType, int size = sizeof(CharType)>
- struct utf_traits;
- template<typename CharType>
- struct utf_traits<CharType, 1>
- {
- using char_type = CharType;
- static int trail_length(char_type ci)
- {
- unsigned char c = ci;
- if(c < 128)
- return 0;
- if(BOOST_UNLIKELY(c < 194))
- return -1;
- if(c < 224)
- return 1;
- if(c < 240)
- return 2;
- if(BOOST_LIKELY(c <= 244))
- return 3;
- return -1;
- }
- static const int max_width = 4;
- static int width(code_point value)
- {
- if(value <= 0x7F)
- {
- return 1;
- } else if(value <= 0x7FF)
- {
- return 2;
- } else if(BOOST_LIKELY(value <= 0xFFFF))
- {
- return 3;
- } else
- {
- return 4;
- }
- }
- static bool is_trail(char_type ci)
- {
- unsigned char c = ci;
- return (c & 0xC0) == 0x80;
- }
- static bool is_lead(char_type ci)
- {
- return !is_trail(ci);
- }
- template<typename Iterator>
- static code_point decode(Iterator& p, Iterator e)
- {
- if(BOOST_UNLIKELY(p == e))
- return incomplete;
- unsigned char lead = *p++;
-
- int trail_size = trail_length(lead);
- if(BOOST_UNLIKELY(trail_size < 0))
- return illegal;
-
-
- if(trail_size == 0)
- return lead;
- code_point c = lead & ((1 << (6 - trail_size)) - 1);
-
- unsigned char tmp;
- switch(trail_size)
- {
- case 3:
- if(BOOST_UNLIKELY(p == e))
- return incomplete;
- tmp = *p++;
- if(!is_trail(tmp))
- return illegal;
- c = (c << 6) | (tmp & 0x3F);
- BOOST_NOWIDE_FALLTHROUGH;
- case 2:
- if(BOOST_UNLIKELY(p == e))
- return incomplete;
- tmp = *p++;
- if(!is_trail(tmp))
- return illegal;
- c = (c << 6) | (tmp & 0x3F);
- BOOST_NOWIDE_FALLTHROUGH;
- case 1:
- if(BOOST_UNLIKELY(p == e))
- return incomplete;
- tmp = *p++;
- if(!is_trail(tmp))
- return illegal;
- c = (c << 6) | (tmp & 0x3F);
- }
-
-
-
- if(BOOST_UNLIKELY(!is_valid_codepoint(c)) || BOOST_UNLIKELY(width(c) != trail_size + 1))
- {
- p -= trail_size;
- return illegal;
- }
- return c;
- }
- template<typename Iterator>
- static code_point decode_valid(Iterator& p)
- {
- unsigned char lead = *p++;
- if(lead < 192)
- return lead;
- int trail_size;
- if(lead < 224)
- trail_size = 1;
- else if(BOOST_LIKELY(lead < 240))
- trail_size = 2;
- else
- trail_size = 3;
- code_point c = lead & ((1 << (6 - trail_size)) - 1);
- switch(trail_size)
- {
- case 3: c = (c << 6) | (static_cast<unsigned char>(*p++) & 0x3F); BOOST_NOWIDE_FALLTHROUGH;
- case 2: c = (c << 6) | (static_cast<unsigned char>(*p++) & 0x3F); BOOST_NOWIDE_FALLTHROUGH;
- case 1: c = (c << 6) | (static_cast<unsigned char>(*p++) & 0x3F);
- }
- return c;
- }
- template<typename Iterator>
- static Iterator encode(code_point value, Iterator out)
- {
- if(value <= 0x7F)
- {
- *out++ = static_cast<char_type>(value);
- } else if(value <= 0x7FF)
- {
- *out++ = static_cast<char_type>((value >> 6) | 0xC0);
- *out++ = static_cast<char_type>((value & 0x3F) | 0x80);
- } else if(BOOST_LIKELY(value <= 0xFFFF))
- {
- *out++ = static_cast<char_type>((value >> 12) | 0xE0);
- *out++ = static_cast<char_type>(((value >> 6) & 0x3F) | 0x80);
- *out++ = static_cast<char_type>((value & 0x3F) | 0x80);
- } else
- {
- *out++ = static_cast<char_type>((value >> 18) | 0xF0);
- *out++ = static_cast<char_type>(((value >> 12) & 0x3F) | 0x80);
- *out++ = static_cast<char_type>(((value >> 6) & 0x3F) | 0x80);
- *out++ = static_cast<char_type>((value & 0x3F) | 0x80);
- }
- return out;
- }
- };
- template<typename CharType>
- struct utf_traits<CharType, 2>
- {
- using char_type = CharType;
-
- static bool is_single_codepoint(uint16_t x)
- {
-
- return x <= 0xD7FF || x >= 0xE000;
- }
- static bool is_first_surrogate(uint16_t x)
- {
-
- return 0xD800 <= x && x <= 0xDBFF;
- }
- static bool is_second_surrogate(uint16_t x)
- {
-
- return 0xDC00 <= x && x <= 0xDFFF;
- }
- static code_point combine_surrogate(uint16_t w1, uint16_t w2)
- {
- return ((code_point(w1 & 0x3FF) << 10) | (w2 & 0x3FF)) + 0x10000;
- }
- static int trail_length(char_type c)
- {
- if(is_first_surrogate(c))
- return 1;
- if(is_second_surrogate(c))
- return -1;
- return 0;
- }
-
- static bool is_trail(char_type c)
- {
- return is_second_surrogate(c);
- }
-
- static bool is_lead(char_type c)
- {
- return !is_second_surrogate(c);
- }
- template<typename It>
- static code_point decode(It& current, It last)
- {
- if(BOOST_UNLIKELY(current == last))
- return incomplete;
- uint16_t w1 = *current++;
- if(BOOST_LIKELY(is_single_codepoint(w1)))
- {
- return w1;
- }
-
- if(w1 >= 0xDC00)
- return illegal;
- if(current == last)
- return incomplete;
- uint16_t w2 = *current++;
- if(!is_second_surrogate(w2))
- return illegal;
- return combine_surrogate(w1, w2);
- }
- template<typename It>
- static code_point decode_valid(It& current)
- {
- uint16_t w1 = *current++;
- if(BOOST_LIKELY(is_single_codepoint(w1)))
- {
- return w1;
- }
- uint16_t w2 = *current++;
- return combine_surrogate(w1, w2);
- }
- static const int max_width = 2;
- static int width(code_point u)
- {
- return u >= 0x10000 ? 2 : 1;
- }
- template<typename It>
- static It encode(code_point u, It out)
- {
- if(BOOST_LIKELY(u <= 0xFFFF))
- {
- *out++ = static_cast<char_type>(u);
- } else
- {
- u -= 0x10000;
- *out++ = static_cast<char_type>(0xD800 | (u >> 10));
- *out++ = static_cast<char_type>(0xDC00 | (u & 0x3FF));
- }
- return out;
- }
- };
- template<typename CharType>
- struct utf_traits<CharType, 4>
- {
- using char_type = CharType;
- static int trail_length(char_type c)
- {
- if(is_valid_codepoint(c))
- return 0;
- return -1;
- }
- static bool is_trail(char_type )
- {
- return false;
- }
- static bool is_lead(char_type )
- {
- return true;
- }
- template<typename It>
- static code_point decode_valid(It& current)
- {
- return *current++;
- }
- template<typename It>
- static code_point decode(It& current, It last)
- {
- if(BOOST_UNLIKELY(current == last))
- return incomplete;
- code_point c = *current++;
- if(BOOST_UNLIKELY(!is_valid_codepoint(c)))
- return illegal;
- return c;
- }
- static const int max_width = 1;
- static int width(code_point )
- {
- return 1;
- }
- template<typename It>
- static It encode(code_point u, It out)
- {
- *out++ = static_cast<char_type>(u);
- return out;
- }
- };
- #endif
- }
- }
- }
- #endif
|