text: 
  // Copyright (c) 2010-2021 niXman (github dot nixman at pm dot me). All
  // rights reserved.
  //
  // This file is part of YAS(https://github.com/niXman/yas) project.
  //
  // Distributed under the Boost Software License, Version 1.0. (See accompanying
  // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  //
  //
  //
  // Boost Software License - Version 1.0 - August 17th, 2003
  //
  // Permission is hereby granted, free of charge, to any person or organization
  // obtaining a copy of the software and accompanying documentation covered by
  // this license (the "Software") to use, reproduce, display, distribute,
  // execute, and transmit the Software, and to prepare derivative works of the
  // Software, and to permit third-parties to whom the Software is furnished to
  // do so, all subject to the following:
  //
  // The copyright notices in the Software and this entire statement, including
  // the above license grant, this restriction and the following disclaimer,
  // must be included in all copies of the Software, in whole or in part, and
  // all derivative works of the Software, unless such copies or derivative
  // works are solely in the form of machine-executable object code generated by
  // a source language processor.
  //
  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  // DEALINGS IN THE SOFTWARE.
  
  #ifndef __yas__detail__tools__utf8conv_hpp
  #define __yas__detail__tools__utf8conv_hpp
  
  #include <yas/detail/tools/cast.hpp>
  
  #include <string>
  
  #if defined(YAS_SERIALIZE_BOOST_TYPES)
  #	include <boost/container/string.hpp>
  #endif
  
  namespace yas {
  namespace detail {
  
  // Based on the code by Avanesov Tigran, 2009-2011:
  // http://forum.vingrad.ru/index.php?showtopic=249965&view=findpost&p=1803380
  // http://forum.vingrad.ru/index.php?showtopic=249965&view=findpost&p=1803439
  
  /***************************************************************************/
  
  template<typename D, typename S>
  void to_utf8(D &dst, const S &src) {
  	for ( auto it = src.begin(); it != src.end(); ++it ) {
  		std::wstring::value_type nchar = *it;
  		if (nchar <= 0x7F) {
  			dst += __YAS_SCAST(std::string::value_type, nchar);
  		} else if (nchar <= 0x07FF) {
  			dst += __YAS_SCAST(char, 0xC0 | (nchar >> 6));
  			dst += __YAS_SCAST(char, 0x80 | (nchar & 0x3F));
  		} else if (nchar <= 0xFFFF) {
  			dst += __YAS_SCAST(char, 0xE0 | (nchar >> 12));
  			dst += __YAS_SCAST(char, 0x80 | ((nchar >> 6) & 0x3F));
  			dst += __YAS_SCAST(char, 0x80 | (nchar & 0x3F));
  		}
  #if WCHAR_MAX > 0xFFFF 
  		else if (nchar  <= 0x1FFFFF) {
  			dst += __YAS_SCAST(char, 0xF0 | (nchar >> 18));
  			dst += __YAS_SCAST(char, 0x80 | ((nchar >> 12) & 0x3F));
  			dst += __YAS_SCAST(char, 0x80 | ((nchar >> 6) & 0x3F));
  			dst += __YAS_SCAST(char, 0x80 | (nchar & 0x3F));
  		}
  #endif
  	}
  }
  
  template<typename D, typename S>
  void from_utf8(D &dst, const S &src) {
  	std::wstring::value_type tmp = 0;
  	for ( auto it = src.begin(); it != src.end(); ++it ) {
  		unsigned char nchar = __YAS_SCAST(unsigned char, *it);
  		if (nchar <= 0x7F) {
  			tmp = nchar;
  		} else if ((nchar & 0xE0) == 0xC0) {
  			tmp = (nchar & 0x1F) << 6;
  			nchar = __YAS_SCAST(unsigned char, *(++it));
  			tmp |= nchar & 0x3F;
  		} else if ((nchar & 0xF0) == 0xE0) {
  			tmp = (nchar & 0x0F) << 12;
  			nchar = __YAS_SCAST(unsigned char, *(++it));
  			tmp |= (nchar & 0x3F) << 6;
  			nchar = __YAS_SCAST(unsigned char, *(++it));
  			tmp |= (nchar & 0x3F);
  		} else if ((nchar & 0xF8) == 0xF0) {
  			tmp = (nchar & 0x0F) << 18;
  			nchar = __YAS_SCAST(unsigned char, *(++it));
  			tmp |= (nchar & 0x3F) << 12;
  			nchar = __YAS_SCAST(unsigned char, *(++it));
  			tmp |= (nchar & 0x3F) << 6;
  			nchar = __YAS_SCAST(unsigned char, *(++it));
  			tmp |= (nchar & 0x3F);
  		}
  		dst += tmp;
  	}
  }
  
  /***************************************************************************/
  
  template<typename To, typename From>
  struct TypeConverter;
  
  template<>
  struct TypeConverter<std::string, std::wstring> {
  	static void Convert(std::string &dst, const std::wstring &src) {
  		to_utf8(dst, src);
  	}
  };
  
  template<>
  struct TypeConverter<std::wstring, std::string> {
  	static void Convert(std::wstring &dst, const std::string &src) {
  		from_utf8(dst, src);
  	}
  };
  
  template<>
  struct TypeConverter<std::string, std::u16string> {
  	static void Convert(std::string &dst, const std::u16string &src) {
  		to_utf8(dst, src);
  	}
  };
  
  template<>
  struct TypeConverter<std::u16string, std::string> {
  	static void Convert(std::u16string &dst, const std::string &src) {
  		from_utf8(dst, src);
  	}
  };
  
  /***************************************************************************/
  
  #if defined(YAS_SERIALIZE_BOOST_TYPES)
  
  template<>
  struct TypeConverter<boost::container::basic_string<char>, boost::container::basic_string<wchar_t>> {
  	static void Convert(boost::container::basic_string<char> &dst, const boost::container::basic_string<wchar_t> &src) {
  		to_utf8(dst, src);
  	}
  };
  
  template<>
  struct TypeConverter<boost::container::basic_string<wchar_t>, boost::container::basic_string<char>> {
  	static void Convert(boost::container::basic_string<wchar_t> &dst, const boost::container::basic_string<char> &src) {
  		from_utf8(dst, src);
  	}
  };
  
  #endif // YAS_SERIALIZE_BOOST_TYPES
  
  /***************************************************************************/
  
  } // namespace detail
  } // namespace yas
  
  #endif // __yas__detail__tools__utf8conv_hpp
  