feat: take string type itself when conversion

This commit is contained in:
ToruNiina
2024-07-20 04:36:32 +09:00
parent 5e05b75473
commit eba1aa2fde
4 changed files with 25 additions and 23 deletions

View File

@@ -119,10 +119,7 @@ cxx::enable_if_t<cxx::conjunction<
>::value, T> >::value, T>
get(const basic_value<TC>& v) get(const basic_value<TC>& v)
{ {
using value_type = typename cxx::remove_cvref_t<T>::value_type; return detail::string_conv<cxx::remove_cvref_t<T>>(v.as_string());
using traits_type = typename cxx::remove_cvref_t<T>::traits_type;
using allocator_type = typename cxx::remove_cvref_t<T>::allocator_type;
return detail::to_string_of<value_type, traits_type, allocator_type>(v.as_string());
} }
// ============================================================================ // ============================================================================

View File

@@ -1693,7 +1693,7 @@ parse_simple_key(location& loc, const context<TC>& ctx)
if(const auto bare = syntax::unquoted_key(spec).scan(loc)) if(const auto bare = syntax::unquoted_key(spec).scan(loc))
{ {
return ok(to_string_of<typename key_type::value_type>(bare.as_string())); return ok(string_conv<key_type>(bare.as_string()));
} }
else else
{ {

View File

@@ -103,7 +103,7 @@ inline std::string make_string(std::size_t len, char c)
template<typename Char, typename Traits, typename Alloc, template<typename Char, typename Traits, typename Alloc,
typename Char2, typename Traits2, typename Alloc2> typename Char2, typename Traits2, typename Alloc2>
struct to_string_of_impl struct string_conv_impl
{ {
static_assert(sizeof(Char) == sizeof(char), ""); static_assert(sizeof(Char) == sizeof(char), "");
static_assert(sizeof(Char2) == sizeof(char), ""); static_assert(sizeof(Char2) == sizeof(char), "");
@@ -126,7 +126,7 @@ struct to_string_of_impl
}; };
template<typename Char, typename Traits, typename Alloc> template<typename Char, typename Traits, typename Alloc>
struct to_string_of_impl<Char, Traits, Alloc, Char, Traits, Alloc> struct string_conv_impl<Char, Traits, Alloc, Char, Traits, Alloc>
{ {
static_assert(sizeof(Char) == sizeof(char), ""); static_assert(sizeof(Char) == sizeof(char), "");
@@ -141,22 +141,27 @@ struct to_string_of_impl<Char, Traits, Alloc, Char, Traits, Alloc>
} }
}; };
template<typename Char, template<typename S, typename Char2, typename Traits2, typename Alloc2>
typename Traits = std::char_traits<Char>, cxx::enable_if_t<is_std_basic_string<S>::value, S>
typename Alloc = std::allocator<Char>, string_conv(std::basic_string<Char2, Traits2, Alloc2> s)
typename Char2, typename Traits2, typename Alloc2>
std::basic_string<Char, Traits, Alloc>
to_string_of(std::basic_string<Char2, Traits2, Alloc2> s)
{ {
return to_string_of_impl<Char, Traits, Alloc, Char2, Traits2, Alloc2>::invoke(std::move(s)); using C = typename S::value_type;
using T = typename S::traits_type;
using A = typename S::allocator_type;
return string_conv_impl<C, T, A, Char2, Traits2, Alloc2>::invoke(std::move(s));
} }
template<typename Char, template<typename S, std::size_t N>
typename Traits = std::char_traits<Char>, cxx::enable_if_t<is_std_basic_string<S>::value, S>
typename Alloc = std::allocator<Char>, string_conv(const char (&s)[N])
typename Char2, typename Traits2, typename Alloc2, std::size_t N>
std::basic_string<Char, Traits, Alloc> to_string_of(const char (&s)[N])
{ {
return to_string_of_impl<Char, Traits, Alloc, Char2, Traits2, Alloc2>::template invoke<N>(s); using C = typename S::value_type;
using T = typename S::traits_type;
using A = typename S::allocator_type;
using C2 = char;
using T2 = std::char_traits<C2>;
using A2 = std::allocator<C2>;
return string_conv_impl<C, T, A, C2, T2, A2>::template invoke<N>(s);
} }
} // namespace detail } // namespace detail

View File

@@ -680,7 +680,7 @@ class basic_value
basic_value(const T& x, string_format_info fmt, basic_value(const T& x, string_format_info fmt,
std::vector<std::string> com, region_type reg) std::vector<std::string> com, region_type reg)
: type_(value_t::string), : type_(value_t::string),
string_(string_storage(detail::to_string_of<char_type>(x), std::move(fmt))), string_(string_storage(detail::string_conv<string_type>(x), std::move(fmt))),
region_(std::move(reg)), comments_(std::move(com)) region_(std::move(reg)), comments_(std::move(com))
{} {}
template<typename T, cxx::enable_if_t<cxx::conjunction< template<typename T, cxx::enable_if_t<cxx::conjunction<
@@ -697,7 +697,7 @@ class basic_value
this->cleanup(); this->cleanup();
this->type_ = value_t::string; this->type_ = value_t::string;
this->region_ = region_type{}; this->region_ = region_type{};
assigner(this->string_, string_storage(detail::to_string_of<char_type>(x), std::move(fmt))); assigner(this->string_, string_storage(detail::string_conv<string_type>(x), std::move(fmt)));
return *this; return *this;
} }
@@ -2131,7 +2131,7 @@ template<typename TC>
error_info make_not_found_error(const basic_value<TC>& v, const std::string& fname, const typename basic_value<TC>::key_type& key) error_info make_not_found_error(const basic_value<TC>& v, const std::string& fname, const typename basic_value<TC>::key_type& key)
{ {
const auto loc = v.location(); const auto loc = v.location();
const std::string title = fname + ": key \"" + to_string_of<char>(key) + "\" not found"; const std::string title = fname + ": key \"" + string_conv<std::string>(key) + "\" not found";
std::vector<std::pair<source_location, std::string>> locs; std::vector<std::pair<source_location, std::string>> locs;
if( ! loc.is_ok()) if( ! loc.is_ok())