utf8.hpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright (c) 2022 Klemens D. Morgenstern
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_PROCESS_V2_DETAIL_UTF8_HPP
  6. #define BOOST_PROCESS_V2_DETAIL_UTF8_HPP
  7. #include <boost/process/v2/detail/config.hpp>
  8. #include <boost/process/v2/detail/throw_error.hpp>
  9. BOOST_PROCESS_V2_BEGIN_NAMESPACE
  10. namespace detail
  11. {
  12. BOOST_PROCESS_V2_DECL std::size_t size_as_utf8(const wchar_t * in, std::size_t size, error_code & ec);
  13. BOOST_PROCESS_V2_DECL std::size_t size_as_wide(const char * in, std::size_t size, error_code & ec);
  14. BOOST_PROCESS_V2_DECL std::size_t convert_to_utf8(const wchar_t * in, std::size_t size,
  15. char * out, std::size_t max_size, error_code & ec);
  16. BOOST_PROCESS_V2_DECL std::size_t convert_to_wide(const char * in, std::size_t size,
  17. wchar_t * out, std::size_t max_size, error_code & ec);
  18. template<typename CharOut, typename Traits = std::char_traits<CharOut>,
  19. typename Allocator = std::allocator<CharOut>, typename CharIn,
  20. typename = typename std::enable_if<std::is_same<CharOut, CharIn>::value>::type>
  21. BOOST_PROCESS_V2_DECL
  22. std::basic_string<CharOut, Traits, Allocator> conv_string(
  23. const CharIn * data, std::size_t size,
  24. const Allocator allocator = Allocator{})
  25. {
  26. return std::basic_string<CharOut, Traits, Allocator>(data, size, allocator);
  27. }
  28. template<typename CharOut, typename Traits = std::char_traits<CharOut>,
  29. typename Allocator = std::allocator<CharOut>,
  30. typename = typename std::enable_if<std::is_same<CharOut, char>::value>::type>
  31. BOOST_PROCESS_V2_DECL
  32. std::basic_string<CharOut, Traits, Allocator> conv_string(
  33. const wchar_t * data, std::size_t size,
  34. const Allocator allocator = Allocator{})
  35. {
  36. error_code ec;
  37. const auto req_size = size_as_utf8(data, size, ec);
  38. if (ec)
  39. detail::throw_error(ec, "size_as_utf8");
  40. std::basic_string<CharOut, Traits, Allocator> res(allocator);
  41. res.resize(req_size);
  42. auto res_size = convert_to_utf8(data, size, &res.front(), req_size, ec);
  43. if (ec)
  44. detail::throw_error(ec, "convert_to_utf8");
  45. res.resize(res_size);
  46. return res;
  47. }
  48. template<typename CharOut, typename Traits = std::char_traits<CharOut>,
  49. typename Allocator = std::allocator<CharOut>,
  50. typename = typename std::enable_if<std::is_same<CharOut, wchar_t>::value>::type>
  51. BOOST_PROCESS_V2_DECL
  52. std::basic_string<CharOut, Traits, Allocator> conv_string(
  53. const char * data, std::size_t size,
  54. const Allocator allocator = Allocator{})
  55. {
  56. error_code ec;
  57. const auto req_size = size_as_wide(data, size, ec);
  58. if (ec)
  59. detail::throw_error(ec, "size_as_wide");
  60. std::basic_string<CharOut, Traits, Allocator> res(allocator);
  61. res.resize(req_size);
  62. auto res_size = convert_to_wide(data, size, &res.front(), req_size, ec);
  63. if (ec)
  64. detail::throw_error(ec, "convert_to_wide");
  65. res.resize(res_size);
  66. return res;
  67. }
  68. }
  69. BOOST_PROCESS_V2_END_NAMESPACE
  70. #endif //BOOST_PROCESS_V2_DETAIL_UTF8_HPP