Skip to content

Commit

Permalink
feat: take string type itself when conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
ToruNiina committed Jul 19, 2024
1 parent 5e05b75 commit eba1aa2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
5 changes: 1 addition & 4 deletions include/toml11/get.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,7 @@ cxx::enable_if_t<cxx::conjunction<
>::value, T>
get(const basic_value<TC>& v)
{
using value_type = typename cxx::remove_cvref_t<T>::value_type;
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());
return detail::string_conv<cxx::remove_cvref_t<T>>(v.as_string());
}

// ============================================================================
Expand Down
2 changes: 1 addition & 1 deletion include/toml11/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1693,7 +1693,7 @@ parse_simple_key(location& loc, const context<TC>& ctx)

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
{
Expand Down
35 changes: 20 additions & 15 deletions include/toml11/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ inline std::string make_string(std::size_t len, char c)

template<typename Char, typename Traits, typename Alloc,
typename Char2, typename Traits2, typename Alloc2>
struct to_string_of_impl
struct string_conv_impl
{
static_assert(sizeof(Char) == sizeof(char), "");
static_assert(sizeof(Char2) == sizeof(char), "");
Expand All @@ -126,7 +126,7 @@ struct to_string_of_impl
};

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), "");

Expand All @@ -141,22 +141,27 @@ struct to_string_of_impl<Char, Traits, Alloc, Char, Traits, Alloc>
}
};

template<typename Char,
typename Traits = std::char_traits<Char>,
typename Alloc = std::allocator<Char>,
typename Char2, typename Traits2, typename Alloc2>
std::basic_string<Char, Traits, Alloc>
to_string_of(std::basic_string<Char2, Traits2, Alloc2> s)
template<typename S, typename Char2, typename Traits2, typename Alloc2>
cxx::enable_if_t<is_std_basic_string<S>::value, S>
string_conv(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,
typename Traits = std::char_traits<Char>,
typename Alloc = std::allocator<Char>,
typename Char2, typename Traits2, typename Alloc2, std::size_t N>
std::basic_string<Char, Traits, Alloc> to_string_of(const char (&s)[N])
template<typename S, std::size_t N>
cxx::enable_if_t<is_std_basic_string<S>::value, S>
string_conv(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
Expand Down
6 changes: 3 additions & 3 deletions include/toml11/value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ class basic_value
basic_value(const T& x, string_format_info fmt,
std::vector<std::string> com, region_type reg)
: 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))
{}
template<typename T, cxx::enable_if_t<cxx::conjunction<
Expand All @@ -697,7 +697,7 @@ class basic_value
this->cleanup();
this->type_ = value_t::string;
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;
}

Expand Down Expand Up @@ -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)
{
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;
if( ! loc.is_ok())
Expand Down

0 comments on commit eba1aa2

Please sign in to comment.