utf8_codecvt.hpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. //
  2. // Copyright (c) 2015 Artyom Beilis (Tonkikh)
  3. //
  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)
  7. //
  8. #ifndef BOOST_LOCALE_UTF8_CODECVT_HPP
  9. #define BOOST_LOCALE_UTF8_CODECVT_HPP
  10. #include <boost/locale/utf.hpp>
  11. #include <boost/locale/generic_codecvt.hpp>
  12. #include <boost/cstdint.hpp>
  13. #include <locale>
  14. namespace boost {
  15. namespace locale {
  16. ///
  17. /// \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
  18. ///
  19. template<typename CharType>
  20. class utf8_codecvt : public generic_codecvt<CharType,utf8_codecvt<CharType> >
  21. {
  22. public:
  23. struct state_type {};
  24. utf8_codecvt(size_t refs = 0) : generic_codecvt<CharType,utf8_codecvt<CharType> >(refs)
  25. {
  26. }
  27. static int max_encoding_length()
  28. {
  29. return 4;
  30. }
  31. static state_type initial_state(generic_codecvt_base::initial_convertion_state /* unused */)
  32. {
  33. return state_type();
  34. }
  35. static utf::code_point to_unicode(state_type &,char const *&begin,char const *end)
  36. {
  37. char const *p=begin;
  38. utf::code_point c = utf::utf_traits<char>::decode(p,end);
  39. if(c!=utf::illegal && c!=utf::incomplete)
  40. begin = p;
  41. return c;
  42. }
  43. static utf::code_point from_unicode(state_type &,utf::code_point u,char *begin,char const *end)
  44. {
  45. if(!utf::is_valid_codepoint(u))
  46. return utf::illegal;
  47. int width;
  48. if((width=utf::utf_traits<char>::width(u)) > end - begin)
  49. return utf::incomplete;
  50. utf::utf_traits<char>::encode(u,begin);
  51. return width;
  52. }
  53. };
  54. } // locale
  55. } // namespace boost
  56. #endif
  57. ///
  58. // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4