From eba1aa2fde8f772ed783d6555b577bf972d2fc9a Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Sat, 20 Jul 2024 04:36:32 +0900 Subject: [PATCH] feat: take string type itself when conversion --- include/toml11/get.hpp | 5 +---- include/toml11/parser.hpp | 2 +- include/toml11/utility.hpp | 35 ++++++++++++++++++++--------------- include/toml11/value.hpp | 6 +++--- 4 files changed, 25 insertions(+), 23 deletions(-) diff --git a/include/toml11/get.hpp b/include/toml11/get.hpp index dbf327a..8cdad9c 100644 --- a/include/toml11/get.hpp +++ b/include/toml11/get.hpp @@ -119,10 +119,7 @@ cxx::enable_if_t::value, T> get(const basic_value& v) { - using value_type = typename cxx::remove_cvref_t::value_type; - using traits_type = typename cxx::remove_cvref_t::traits_type; - using allocator_type = typename cxx::remove_cvref_t::allocator_type; - return detail::to_string_of(v.as_string()); + return detail::string_conv>(v.as_string()); } // ============================================================================ diff --git a/include/toml11/parser.hpp b/include/toml11/parser.hpp index 37e4d32..92dfb2f 100644 --- a/include/toml11/parser.hpp +++ b/include/toml11/parser.hpp @@ -1693,7 +1693,7 @@ parse_simple_key(location& loc, const context& ctx) if(const auto bare = syntax::unquoted_key(spec).scan(loc)) { - return ok(to_string_of(bare.as_string())); + return ok(string_conv(bare.as_string())); } else { diff --git a/include/toml11/utility.hpp b/include/toml11/utility.hpp index 66dfc76..b82b3f4 100644 --- a/include/toml11/utility.hpp +++ b/include/toml11/utility.hpp @@ -103,7 +103,7 @@ inline std::string make_string(std::size_t len, char c) template -struct to_string_of_impl +struct string_conv_impl { static_assert(sizeof(Char) == sizeof(char), ""); static_assert(sizeof(Char2) == sizeof(char), ""); @@ -126,7 +126,7 @@ struct to_string_of_impl }; template -struct to_string_of_impl +struct string_conv_impl { static_assert(sizeof(Char) == sizeof(char), ""); @@ -141,22 +141,27 @@ struct to_string_of_impl } }; -template, - typename Alloc = std::allocator, - typename Char2, typename Traits2, typename Alloc2> -std::basic_string -to_string_of(std::basic_string s) +template +cxx::enable_if_t::value, S> +string_conv(std::basic_string s) { - return to_string_of_impl::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::invoke(std::move(s)); } -template, - typename Alloc = std::allocator, - typename Char2, typename Traits2, typename Alloc2, std::size_t N> -std::basic_string to_string_of(const char (&s)[N]) +template +cxx::enable_if_t::value, S> +string_conv(const char (&s)[N]) { - return to_string_of_impl::template invoke(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; + using A2 = std::allocator; + + return string_conv_impl::template invoke(s); } } // namespace detail diff --git a/include/toml11/value.hpp b/include/toml11/value.hpp index 5d70671..22c3535 100644 --- a/include/toml11/value.hpp +++ b/include/toml11/value.hpp @@ -680,7 +680,7 @@ class basic_value basic_value(const T& x, string_format_info fmt, std::vector com, region_type reg) : type_(value_t::string), - string_(string_storage(detail::to_string_of(x), std::move(fmt))), + string_(string_storage(detail::string_conv(x), std::move(fmt))), region_(std::move(reg)), comments_(std::move(com)) {} templatecleanup(); this->type_ = value_t::string; this->region_ = region_type{}; - assigner(this->string_, string_storage(detail::to_string_of(x), std::move(fmt))); + assigner(this->string_, string_storage(detail::string_conv(x), std::move(fmt))); return *this; } @@ -2131,7 +2131,7 @@ template error_info make_not_found_error(const basic_value& v, const std::string& fname, const typename basic_value::key_type& key) { const auto loc = v.location(); - const std::string title = fname + ": key \"" + to_string_of(key) + "\" not found"; + const std::string title = fname + ": key \"" + string_conv(key) + "\" not found"; std::vector> locs; if( ! loc.is_ok())