File indexing completed on 2025-09-15 08:50:44
0001
0002
0003
0004
0005 #ifndef BOOST_PROCESS_V2_DETAIL_UTF8_HPP
0006 #define BOOST_PROCESS_V2_DETAIL_UTF8_HPP
0007
0008 #include <boost/process/v2/detail/config.hpp>
0009 #include <boost/process/v2/detail/throw_error.hpp>
0010
0011 BOOST_PROCESS_V2_BEGIN_NAMESPACE
0012
0013 namespace detail
0014 {
0015
0016 BOOST_PROCESS_V2_DECL std::size_t size_as_utf8(const wchar_t * in, std::size_t size, error_code & ec);
0017 BOOST_PROCESS_V2_DECL std::size_t size_as_wide(const char * in, std::size_t size, error_code & ec);
0018
0019 BOOST_PROCESS_V2_DECL std::size_t convert_to_utf8(const wchar_t * in, std::size_t size,
0020 char * out, std::size_t max_size, error_code & ec);
0021 BOOST_PROCESS_V2_DECL std::size_t convert_to_wide(const char * in, std::size_t size,
0022 wchar_t * out, std::size_t max_size, error_code & ec);
0023
0024 template<typename CharOut, typename Traits = std::char_traits<CharOut>,
0025 typename Allocator = std::allocator<CharOut>, typename CharIn,
0026 typename = typename std::enable_if<std::is_same<CharOut, CharIn>::value>::type>
0027 std::basic_string<CharOut, Traits, Allocator> conv_string(
0028 const CharIn * data, std::size_t size,
0029 const Allocator allocator = Allocator{})
0030 {
0031 return std::basic_string<CharOut, Traits, Allocator>(data, size, allocator);
0032 }
0033
0034
0035 template<typename CharOut, typename Traits = std::char_traits<CharOut>,
0036 typename Allocator = std::allocator<CharOut>,
0037 typename = typename std::enable_if<std::is_same<CharOut, char>::value>::type>
0038 std::basic_string<CharOut, Traits, Allocator> conv_string(
0039 const wchar_t * data, std::size_t size,
0040 const Allocator allocator = Allocator{})
0041 {
0042 error_code ec;
0043 const auto req_size = size_as_utf8(data, size, ec);
0044 if (ec)
0045 detail::throw_error(ec, "size_as_utf8");
0046
0047 std::basic_string<CharOut, Traits, Allocator> res(allocator);
0048 res.resize(req_size);
0049
0050 auto res_size = convert_to_utf8(data, size, &res.front(), req_size, ec);
0051 if (ec)
0052 detail::throw_error(ec, "convert_to_utf8");
0053
0054 res.resize(res_size);
0055 return res;
0056 }
0057
0058 template<typename CharOut, typename Traits = std::char_traits<CharOut>,
0059 typename Allocator = std::allocator<CharOut>,
0060 typename = typename std::enable_if<std::is_same<CharOut, wchar_t>::value>::type>
0061 std::basic_string<CharOut, Traits, Allocator> conv_string(
0062 const char * data, std::size_t size,
0063 const Allocator allocator = Allocator{})
0064 {
0065 error_code ec;
0066 const auto req_size = size_as_wide(data, size, ec);
0067 if (ec)
0068 detail::throw_error(ec, "size_as_wide");
0069
0070 std::basic_string<CharOut, Traits, Allocator> res(allocator);
0071 res.resize(req_size);
0072
0073 auto res_size = convert_to_wide(data, size, &res.front(), req_size, ec);
0074 if (ec)
0075 detail::throw_error(ec, "convert_to_wide");
0076
0077 res.resize(res_size);
0078 return res;
0079 }
0080
0081 }
0082
0083 BOOST_PROCESS_V2_END_NAMESPACE
0084
0085
0086 #endif