utf8.hpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. std::basic_string<CharOut, Traits, Allocator> conv_string(
  22. const CharIn * data, std::size_t size,
  23. const Allocator allocator = Allocator{})
  24. {
  25. return std::basic_string<CharOut, Traits, Allocator>(data, size, allocator);
  26. }
  27. template<typename CharOut, typename Traits = std::char_traits<CharOut>,
  28. typename Allocator = std::allocator<CharOut>,
  29. typename = typename std::enable_if<std::is_same<CharOut, char>::value>::type>
  30. std::basic_string<CharOut, Traits, Allocator> conv_string(
  31. const wchar_t * data, std::size_t size,
  32. const Allocator allocator = Allocator{})
  33. {
  34. error_code ec;
  35. const auto req_size = size_as_utf8(data, size, ec);
  36. if (ec)
  37. detail::throw_error(ec, "size_as_utf8");
  38. std::basic_string<CharOut, Traits, Allocator> res(allocator);
  39. res.resize(req_size);
  40. if (req_size == 0)
  41. return res;
  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. std::basic_string<CharOut, Traits, Allocator> conv_string(
  52. const char * data, std::size_t size,
  53. const Allocator allocator = Allocator{})
  54. {
  55. error_code ec;
  56. const auto req_size = size_as_wide(data, size, ec);
  57. if (ec)
  58. detail::throw_error(ec, "size_as_wide");
  59. std::basic_string<CharOut, Traits, Allocator> res(allocator);
  60. res.resize(req_size);
  61. if (req_size == 0)
  62. return res;
  63. auto res_size = convert_to_wide(data, size, &res.front(), req_size, ec);
  64. if (ec)
  65. detail::throw_error(ec, "convert_to_wide");
  66. res.resize(res_size);
  67. return res;
  68. }
  69. }
  70. BOOST_PROCESS_V2_END_NAMESPACE
  71. #endif //BOOST_PROCESS_V2_DETAIL_UTF8_HPP