convert.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // Copyright (c) 2012 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_NOWIDE_CONVERT_HPP_INCLUDED
  9. #define BOOST_NOWIDE_CONVERT_HPP_INCLUDED
  10. #include <boost/nowide/detail/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 detail::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 + detail::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 detail::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 + detail::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. inline std::string narrow(const wchar_t* s, size_t count)
  66. {
  67. return detail::convert_string<char>(s, s + count);
  68. }
  69. ///
  70. /// Convert wide string (UTF-16/32) to narrow string (UTF-8).
  71. ///
  72. /// \param s NULL terminated input string
  73. /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER
  74. ///
  75. inline std::string narrow(const wchar_t* s)
  76. {
  77. return narrow(s, detail::strlen(s));
  78. }
  79. ///
  80. /// Convert wide string (UTF-16/32) to narrow string (UTF-8).
  81. ///
  82. /// \param s Input string
  83. /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER
  84. ///
  85. inline std::string narrow(const std::wstring& s)
  86. {
  87. return narrow(s.c_str(), s.size());
  88. }
  89. ///
  90. /// Convert narrow string (UTF-8) to wide string (UTF-16/32).
  91. ///
  92. /// \param s Input string
  93. /// \param count Number of characters to convert
  94. /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER
  95. ///
  96. inline std::wstring widen(const char* s, size_t count)
  97. {
  98. return detail::convert_string<wchar_t>(s, s + count);
  99. }
  100. ///
  101. /// Convert narrow string (UTF-8) to wide string (UTF-16/32).
  102. ///
  103. /// \param s NULL terminated input string
  104. /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER
  105. ///
  106. inline std::wstring widen(const char* s)
  107. {
  108. return widen(s, detail::strlen(s));
  109. }
  110. ///
  111. /// Convert narrow string (UTF-8) to wide string (UTF-16/32).
  112. ///
  113. /// \param s Input string
  114. /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER
  115. ///
  116. inline std::wstring widen(const std::string& s)
  117. {
  118. return widen(s.c_str(), s.size());
  119. }
  120. } // namespace nowide
  121. } // namespace boost
  122. #endif