diff options
author | Roland Reichwein <mail@reichwein.it> | 2021-02-13 12:47:39 +0100 |
---|---|---|
committer | Roland Reichwein <mail@reichwein.it> | 2021-02-13 12:47:39 +0100 |
commit | 07c77b45ba9f74cfe1bed547bea1eeb705f0582b (patch) | |
tree | dde45e92f704229c3ecc129aa2842686f052db6f /include | |
parent | 24ec1d5ba85503599fd301aa8cd56ee65651ab0b (diff) |
Support G++, support string type as template argument for convert()
Diffstat (limited to 'include')
-rw-r--r-- | include/unicode.h | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/include/unicode.h b/include/unicode.h index d6f8e51..171496e 100644 --- a/include/unicode.h +++ b/include/unicode.h @@ -12,6 +12,7 @@ #include <memory> #include <stdexcept> #include <string> +#include <type_traits> #include <unordered_map> #ifdef __cpp_char8_t @@ -526,7 +527,7 @@ namespace unicode { typedef UTF<utf_iterator<char32_t>, utf_back_insert_iterator<char32_t>> UTF_32; // From and To are facets - template<typename From, typename To> + template<typename From, typename To, std::enable_if_t<std::is_empty<From>::value && std::is_empty<To>::value, bool> = true> std::basic_string<typename To::value_type> convert(const std::basic_string<typename From::value_type>& s) { std::basic_string<typename To::value_type> result; @@ -561,7 +562,9 @@ namespace unicode { }; // From and To are from: utf8_t, char16_t and char32_t - template<typename From, typename To> + template<typename From, typename To, + std::enable_if_t<std::is_trivial<From>::value && std::is_trivial<To>::value, bool> = true + > std::basic_string<To> convert(const std::basic_string<From>& s) { typedef UTF<utf_iterator<From>, utf_back_insert_iterator<To>> UTF_Trait; @@ -573,6 +576,20 @@ namespace unicode { return result; } + template<typename FromContainer, typename ToContainer, + std::enable_if_t<!std::is_empty<FromContainer>::value && !std::is_empty<ToContainer>::value, bool> = true + > + ToContainer convert(const FromContainer& s) + { + typedef UTF<utf_iterator<typename FromContainer::value_type>, utf_back_insert_iterator<typename ToContainer::value_type>> UTF_Trait; + + ToContainer result; + + std::copy(UTF_Trait::begin(s), UTF_Trait::end(s), UTF_Trait::back_inserter(result)); + + return result; + } + // basic type version template<typename T> bool is_valid_utf(const std::basic_string<T>& s) |