convert.hpp 5.4 KB

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