Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:39:17

0001 //
0002 // Copyright (c) 2015 Artyom Beilis (Tonkikh)
0003 //
0004 // Distributed under the Boost Software License, Version 1.0.
0005 // https://www.boost.org/LICENSE_1_0.txt
0006 
0007 #ifndef BOOST_LOCALE_UTF8_CODECVT_HPP
0008 #define BOOST_LOCALE_UTF8_CODECVT_HPP
0009 
0010 #include <boost/locale/generic_codecvt.hpp>
0011 #include <boost/locale/utf.hpp>
0012 #include <boost/assert.hpp>
0013 #include <cstdint>
0014 #include <locale>
0015 
0016 namespace boost { namespace locale {
0017 
0018     /// \brief Generic utf8 codecvt facet, it allows to convert UTF-8 strings to UTF-16 and UTF-32 using wchar_t,
0019     /// char32_t and char16_t
0020     template<typename CharType>
0021     class utf8_codecvt : public generic_codecvt<CharType, utf8_codecvt<CharType>> {
0022     public:
0023         struct state_type {};
0024 
0025         utf8_codecvt(size_t refs = 0) : generic_codecvt<CharType, utf8_codecvt<CharType>>(refs) {}
0026 
0027         static int max_encoding_length() { return 4; }
0028 
0029         static state_type initial_state(generic_codecvt_base::initial_convertion_state /* unused */)
0030         {
0031             return state_type();
0032         }
0033         static utf::code_point to_unicode(state_type&, const char*& begin, const char* end)
0034         {
0035             const char* p = begin;
0036 
0037             utf::code_point c = utf::utf_traits<char>::decode(p, end);
0038             if(c != utf::illegal && c != utf::incomplete)
0039                 begin = p;
0040             return c;
0041         }
0042 
0043         static utf::len_or_error from_unicode(state_type&, utf::code_point u, char* begin, const char* end)
0044         {
0045             BOOST_ASSERT(utf::is_valid_codepoint(u));
0046             const auto width = utf::utf_traits<char>::width(u);
0047             if(width > end - begin)
0048                 return utf::incomplete;
0049             utf::utf_traits<char>::encode(u, begin);
0050             return width;
0051         }
0052     };
0053 
0054 }} // namespace boost::locale
0055 
0056 #endif