code_conversion.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2015.
  3. * Distributed under the Boost Software License, Version 1.0.
  4. * (See accompanying file LICENSE_1_0.txt or copy at
  5. * http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. /*!
  8. * \file code_conversion.hpp
  9. * \author Andrey Semashev
  10. * \date 08.11.2008
  11. *
  12. * \brief This header is the Boost.Log library implementation, see the library documentation
  13. * at http://www.boost.org/doc/libs/release/libs/log/doc/html/index.html.
  14. */
  15. #ifndef BOOST_LOG_DETAIL_CODE_CONVERSION_HPP_INCLUDED_
  16. #define BOOST_LOG_DETAIL_CODE_CONVERSION_HPP_INCLUDED_
  17. #include <cstddef>
  18. #include <locale>
  19. #include <string>
  20. #include <boost/core/enable_if.hpp>
  21. #include <boost/log/detail/config.hpp>
  22. #include <boost/log/detail/is_character_type.hpp>
  23. #include <boost/log/detail/header.hpp>
  24. #ifdef BOOST_HAS_PRAGMA_ONCE
  25. #pragma once
  26. #endif
  27. namespace boost {
  28. BOOST_LOG_OPEN_NAMESPACE
  29. namespace aux {
  30. // Implementation note: We have to implement char<->wchar_t conversions even in the absence of the native wchar_t
  31. // type. These conversions are used in sinks, e.g. to convert multibyte strings to wide-character filesystem paths.
  32. //! The function converts one string to the character type of another
  33. BOOST_LOG_API bool code_convert_impl(const wchar_t* str1, std::size_t len, std::string& str2, std::size_t max_size, std::locale const& loc = std::locale());
  34. //! The function converts one string to the character type of another
  35. BOOST_LOG_API bool code_convert_impl(const char* str1, std::size_t len, std::wstring& str2, std::size_t max_size, std::locale const& loc = std::locale());
  36. #if !defined(BOOST_LOG_NO_CXX11_CODECVT_FACETS)
  37. #if !defined(BOOST_NO_CXX11_CHAR16_T)
  38. //! The function converts one string to the character type of another
  39. BOOST_LOG_API bool code_convert_impl(const char16_t* str1, std::size_t len, std::string& str2, std::size_t max_size, std::locale const& loc = std::locale());
  40. //! The function converts one string to the character type of another
  41. BOOST_LOG_API bool code_convert_impl(const char* str1, std::size_t len, std::u16string& str2, std::size_t max_size, std::locale const& loc = std::locale());
  42. //! The function converts one string to the character type of another
  43. BOOST_LOG_API bool code_convert_impl(const char16_t* str1, std::size_t len, std::wstring& str2, std::size_t max_size, std::locale const& loc = std::locale());
  44. #endif
  45. #if !defined(BOOST_NO_CXX11_CHAR32_T)
  46. //! The function converts one string to the character type of another
  47. BOOST_LOG_API bool code_convert_impl(const char32_t* str1, std::size_t len, std::string& str2, std::size_t max_size, std::locale const& loc = std::locale());
  48. //! The function converts one string to the character type of another
  49. BOOST_LOG_API bool code_convert_impl(const char* str1, std::size_t len, std::u32string& str2, std::size_t max_size, std::locale const& loc = std::locale());
  50. //! The function converts one string to the character type of another
  51. BOOST_LOG_API bool code_convert_impl(const char32_t* str1, std::size_t len, std::wstring& str2, std::size_t max_size, std::locale const& loc = std::locale());
  52. #endif
  53. #if !defined(BOOST_NO_CXX11_CHAR16_T) && !defined(BOOST_NO_CXX11_CHAR32_T)
  54. //! The function converts one string to the character type of another
  55. BOOST_LOG_API bool code_convert_impl(const char16_t* str1, std::size_t len, std::u32string& str2, std::size_t max_size, std::locale const& loc = std::locale());
  56. //! The function converts one string to the character type of another
  57. BOOST_LOG_API bool code_convert_impl(const char32_t* str1, std::size_t len, std::u16string& str2, std::size_t max_size, std::locale const& loc = std::locale());
  58. #endif
  59. #endif // !defined(BOOST_LOG_NO_CXX11_CODECVT_FACETS)
  60. //! The function converts one string to the character type of another
  61. template< typename SourceCharT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT >
  62. inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) == sizeof(TargetCharT), bool >::type
  63. code_convert(const SourceCharT* str1, std::size_t len, std::basic_string< TargetCharT, TargetTraitsT, TargetAllocatorT >& str2, std::size_t max_size, std::locale const& = std::locale())
  64. {
  65. std::size_t size_left = str2.size() < max_size ? max_size - str2.size() : static_cast< std::size_t >(0u);
  66. const bool overflow = len > size_left;
  67. str2.append(reinterpret_cast< const TargetCharT* >(str1), overflow ? size_left : len);
  68. return !overflow;
  69. }
  70. //! The function converts one string to the character type of another
  71. template< typename SourceCharT, typename SourceTraitsT, typename SourceAllocatorT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT >
  72. inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) == sizeof(TargetCharT), bool >::type
  73. code_convert(std::basic_string< SourceCharT, SourceTraitsT, SourceAllocatorT > const& str1, std::basic_string< TargetCharT, TargetTraitsT, TargetAllocatorT >& str2, std::size_t max_size, std::locale const& loc = std::locale())
  74. {
  75. return aux::code_convert(str1.data(), str1.size(), str2, max_size, loc);
  76. }
  77. //! The function converts one string to the character type of another
  78. template< typename SourceCharT, typename SourceTraitsT, typename SourceAllocatorT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT >
  79. inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) == sizeof(TargetCharT), bool >::type
  80. code_convert(std::basic_string< SourceCharT, SourceTraitsT, SourceAllocatorT > const& str1, std::basic_string< TargetCharT, TargetTraitsT, TargetAllocatorT >& str2, std::locale const& loc = std::locale())
  81. {
  82. return aux::code_convert(str1.data(), str1.size(), str2, str2.max_size(), loc);
  83. }
  84. //! The function converts one string to the character type of another
  85. template< typename SourceCharT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT >
  86. inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) == sizeof(TargetCharT), bool >::type
  87. code_convert(const SourceCharT* str1, std::size_t len, std::basic_string< TargetCharT, TargetTraitsT, TargetAllocatorT >& str2, std::locale const& loc = std::locale())
  88. {
  89. return aux::code_convert(str1, len, str2, str2.max_size(), loc);
  90. }
  91. //! The function converts one string to the character type of another
  92. template< typename SourceCharT, typename SourceTraitsT, typename SourceAllocatorT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT >
  93. inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) != sizeof(TargetCharT), bool >::type
  94. code_convert(std::basic_string< SourceCharT, SourceTraitsT, SourceAllocatorT > const& str1, std::basic_string< TargetCharT, TargetTraitsT, TargetAllocatorT >& str2, std::locale const& loc = std::locale())
  95. {
  96. return aux::code_convert_impl(str1.c_str(), str1.size(), str2, str2.max_size(), loc);
  97. }
  98. //! The function converts one string to the character type of another
  99. template< typename SourceCharT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT >
  100. inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) != sizeof(TargetCharT), bool >::type
  101. code_convert(const SourceCharT* str1, std::size_t len, std::basic_string< TargetCharT, TargetTraitsT, TargetAllocatorT >& str2, std::locale const& loc = std::locale())
  102. {
  103. return aux::code_convert_impl(str1, len, str2, str2.max_size(), loc);
  104. }
  105. //! The function converts one string to the character type of another
  106. template< typename SourceCharT, typename SourceTraitsT, typename SourceAllocatorT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT >
  107. inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) != sizeof(TargetCharT), bool >::type
  108. code_convert(std::basic_string< SourceCharT, SourceTraitsT, SourceAllocatorT > const& str1, std::basic_string< TargetCharT, TargetTraitsT, TargetAllocatorT >& str2, std::size_t max_size, std::locale const& loc = std::locale())
  109. {
  110. return aux::code_convert_impl(str1.c_str(), str1.size(), str2, max_size, loc);
  111. }
  112. //! The function converts one string to the character type of another
  113. template< typename SourceCharT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT >
  114. inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) != sizeof(TargetCharT), bool >::type
  115. code_convert(const SourceCharT* str1, std::size_t len, std::basic_string< TargetCharT, TargetTraitsT, TargetAllocatorT >& str2, std::size_t max_size, std::locale const& loc = std::locale())
  116. {
  117. return aux::code_convert_impl(str1, len, str2, max_size, loc);
  118. }
  119. //! The function converts the passed string to the narrow-character encoding
  120. inline std::string const& to_narrow(std::string const& str)
  121. {
  122. return str;
  123. }
  124. //! The function converts the passed string to the narrow-character encoding
  125. inline std::string const& to_narrow(std::string const& str, std::locale const&)
  126. {
  127. return str;
  128. }
  129. //! The function converts the passed string to the narrow-character encoding
  130. inline std::string to_narrow(std::wstring const& str, std::locale const& loc = std::locale())
  131. {
  132. std::string res;
  133. aux::code_convert_impl(str.c_str(), str.size(), res, res.max_size(), loc);
  134. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_HAS_NRVO)
  135. return static_cast< std::string&& >(res);
  136. #else
  137. return res;
  138. #endif
  139. }
  140. //! The function converts the passed string to the wide-character encoding
  141. inline std::wstring const& to_wide(std::wstring const& str)
  142. {
  143. return str;
  144. }
  145. //! The function converts the passed string to the wide-character encoding
  146. inline std::wstring const& to_wide(std::wstring const& str, std::locale const&)
  147. {
  148. return str;
  149. }
  150. //! The function converts the passed string to the wide-character encoding
  151. inline std::wstring to_wide(std::string const& str, std::locale const& loc = std::locale())
  152. {
  153. std::wstring res;
  154. aux::code_convert_impl(str.c_str(), str.size(), res, res.max_size(), loc);
  155. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_HAS_NRVO)
  156. return static_cast< std::wstring&& >(res);
  157. #else
  158. return res;
  159. #endif
  160. }
  161. } // namespace aux
  162. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  163. } // namespace boost
  164. #include <boost/log/detail/footer.hpp>
  165. #endif // BOOST_LOG_DETAIL_CODE_CONVERSION_HPP_INCLUDED_