convert.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // Copyright (c) 2012 Artyom Beilis (Tonkikh)
  3. // Copyright (c) 2020 Alexander Grund
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. #ifndef BOOST_NOWIDE_CONVERT_HPP_INCLUDED
  10. #define BOOST_NOWIDE_CONVERT_HPP_INCLUDED
  11. #include <boost/nowide/detail/is_string_container.hpp>
  12. #include <boost/nowide/utf/convert.hpp>
  13. #include <string>
  14. namespace boost {
  15. namespace nowide {
  16. ///
  17. /// Convert wide string (UTF-16/32) in range [begin,end) to NULL terminated narrow string (UTF-8)
  18. /// stored in \a output of size \a output_size (including NULL)
  19. ///
  20. /// If there is not enough room NULL is returned, else output is returned.
  21. /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER
  22. ///
  23. inline char* narrow(char* output, size_t output_size, const wchar_t* begin, const wchar_t* end)
  24. {
  25. return utf::convert_buffer(output, output_size, begin, end);
  26. }
  27. ///
  28. /// Convert NULL terminated wide string (UTF-16/32) to NULL terminated narrow string (UTF-8)
  29. /// stored in \a output of size \a output_size (including NULL)
  30. ///
  31. /// If there is not enough room NULL is returned, else output is returned.
  32. /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER
  33. ///
  34. inline char* narrow(char* output, size_t output_size, const wchar_t* source)
  35. {
  36. return narrow(output, output_size, source, source + utf::strlen(source));
  37. }
  38. ///
  39. /// Convert narrow string (UTF-8) in range [begin,end) to NULL terminated wide string (UTF-16/32)
  40. /// stored in \a output of size \a output_size (including NULL)
  41. ///
  42. /// If there is not enough room NULL is returned, else output is returned.
  43. /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER
  44. ///
  45. inline wchar_t* widen(wchar_t* output, size_t output_size, const char* begin, const char* end)
  46. {
  47. return utf::convert_buffer(output, output_size, begin, end);
  48. }
  49. ///
  50. /// Convert NULL terminated narrow string (UTF-8) to NULL terminated wide string (UTF-16/32)
  51. /// most output_size (including NULL)
  52. ///
  53. /// If there is not enough room NULL is returned, else output is returned.
  54. /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER
  55. ///
  56. inline wchar_t* widen(wchar_t* output, size_t output_size, const char* source)
  57. {
  58. return widen(output, output_size, source, source + utf::strlen(source));
  59. }
  60. ///
  61. /// Convert wide string (UTF-16/32) to narrow string (UTF-8).
  62. ///
  63. /// \param s Input string
  64. /// \param count Number of characters to convert
  65. /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER
  66. ///
  67. template<typename T_Char, typename = detail::requires_wide_char<T_Char>>
  68. inline std::string narrow(const T_Char* s, size_t count)
  69. {
  70. return utf::convert_string<char>(s, s + count);
  71. }
  72. ///
  73. /// Convert wide string (UTF-16/32) to narrow string (UTF-8).
  74. ///
  75. /// \param s NULL terminated input string
  76. /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER
  77. ///
  78. template<typename T_Char, typename = detail::requires_wide_char<T_Char>>
  79. inline std::string narrow(const T_Char* s)
  80. {
  81. return narrow(s, utf::strlen(s));
  82. }
  83. ///
  84. /// Convert wide string (UTF-16/32) to narrow string (UTF-8).
  85. ///
  86. /// \param s Input string
  87. /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER
  88. ///
  89. template<typename StringOrStringView, typename = detail::requires_wide_string_container<StringOrStringView>>
  90. inline std::string narrow(const StringOrStringView& s)
  91. {
  92. return utf::convert_string<char>(s.data(), s.data() + s.size());
  93. }
  94. ///
  95. /// Convert narrow string (UTF-8) to wide string (UTF-16/32).
  96. ///
  97. /// \param s Input string
  98. /// \param count Number of characters to convert
  99. /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER
  100. ///
  101. template<typename T_Char, typename = detail::requires_narrow_char<T_Char>>
  102. inline std::wstring widen(const T_Char* s, size_t count)
  103. {
  104. return utf::convert_string<wchar_t>(s, s + count);
  105. }
  106. ///
  107. /// Convert narrow string (UTF-8) to wide string (UTF-16/32).
  108. ///
  109. /// \param s NULL terminated input string
  110. /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER
  111. ///
  112. template<typename T_Char, typename = detail::requires_narrow_char<T_Char>>
  113. inline std::wstring widen(const T_Char* s)
  114. {
  115. return widen(s, utf::strlen(s));
  116. }
  117. ///
  118. /// Convert narrow string (UTF-8) to wide string (UTF-16/32).
  119. ///
  120. /// \param s Input string
  121. /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER
  122. ///
  123. template<typename StringOrStringView, typename = detail::requires_narrow_string_container<StringOrStringView>>
  124. inline std::wstring widen(const StringOrStringView& s)
  125. {
  126. return utf::convert_string<wchar_t>(s.data(), s.data() + s.size());
  127. }
  128. } // namespace nowide
  129. } // namespace boost
  130. #endif