2 // Copyright (c) 2015 Artyom Beilis (Tonkikh)
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
8 #ifndef BOOST_LOCALE_UTF8_CODECVT_HPP
9 #define BOOST_LOCALE_UTF8_CODECVT_HPP
11 #include <boost/locale/utf.hpp>
12 #include <boost/locale/generic_codecvt.hpp>
13 #include <boost/cstdint.hpp>
20 /// \brief Geneneric utf8 codecvt facet, it allows to convert UTF-8 strings to UTF-16 and UTF-32 using wchar_t, char32_t and char16_t
22 template<typename CharType>
23 class utf8_codecvt : public generic_codecvt<CharType,utf8_codecvt<CharType> >
29 utf8_codecvt(size_t refs = 0) : generic_codecvt<CharType,utf8_codecvt<CharType> >(refs)
33 static int max_encoding_length()
38 static state_type initial_state(generic_codecvt_base::initial_convertion_state /* unused */)
42 static utf::code_point to_unicode(state_type &,char const *&begin,char const *end)
46 utf::code_point c = utf::utf_traits<char>::decode(p,end);
47 if(c!=utf::illegal && c!=utf::incomplete)
52 static utf::code_point from_unicode(state_type &,utf::code_point u,char *begin,char const *end)
54 if(!utf::is_valid_codepoint(u))
57 if((width=utf::utf_traits<char>::width(u)) > end - begin)
58 return utf::incomplete;
59 utf::utf_traits<char>::encode(u,begin);
69 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4