summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--include/unicode.h29
-rw-r--r--src/test-unicode.cpp26
2 files changed, 29 insertions, 26 deletions
diff --git a/include/unicode.h b/include/unicode.h
index be91d77..4e4c7eb 100644
--- a/include/unicode.h
+++ b/include/unicode.h
@@ -576,7 +576,7 @@ namespace unicode {
typename iso_back_insert_iterator::string_type& s;
};
- // Facet for convert() and ISO-8859-*
+ // Encoding for convert() and ISO-8859-*
template<typename InputIt, typename OutputIt>
struct ISO_8859
{
@@ -599,7 +599,7 @@ namespace unicode {
}
};
- // Facet for convert() and UTF-*
+ // Encoding for convert() and UTF-*
template<typename InputIt, typename OutputIt>
struct UTF
{
@@ -622,7 +622,7 @@ namespace unicode {
}
};
- // Facet for convert()
+ // Encoding for convert()
typedef ISO_8859<iso_iterator<>, iso_back_insert_iterator<>> ISO_8859_1;
typedef ISO_8859<iso_iterator<iso_8859_15_map>, iso_back_insert_iterator<iso_8859_15_map_reverse>> ISO_8859_15;
@@ -731,7 +731,7 @@ namespace unicode {
}; // class ArchitectureOptimizer
- // From and To are facets
+ // From and To are Encodings
template<typename From, typename To, std::enable_if_t<std::is_empty<From>::value, bool> = true>
typename To::string_type convert_optimized(const typename From::string_type& s)
{
@@ -777,7 +777,7 @@ namespace unicode {
return result;
}
- // From and To are facets
+ // From and To are Encodings
template<typename From, typename To, std::enable_if_t<std::is_empty<From>::value, bool> = true>
typename To::string_type convert(const typename From::string_type& s)
{
@@ -799,7 +799,7 @@ namespace unicode {
}
}
- // Helper to get correct Facet from char type, e.g. Encoding<typename decltype(s)::value_type>::Facet
+ // Helper to get correct Encoding from char type, e.g. Encoding<typename decltype(s)::value_type>::type or Encoding_t<typename decltype(s)::value_type>
template<typename T>
struct Encoding
{
@@ -808,21 +808,24 @@ namespace unicode {
template<>
struct Encoding<utf8_t>
{
- typedef UTF_8 Facet;
+ typedef UTF_8 type;
};
template<>
struct Encoding<char16_t>
{
- typedef UTF_16 Facet;
+ typedef UTF_16 type;
};
template<>
struct Encoding<char32_t>
{
- typedef UTF_32 Facet;
+ typedef UTF_32 type;
};
+ template<typename T>
+ using Encoding_t = typename Encoding<T>::type;
+
// From and To are from: utf8_t (i.e. char or char8_t (C++20)), char16_t and char32_t, char, wchar_t, uint8_t, uint16_t, uint32_t
template<typename From, typename To,
typename FromContainer=std::basic_string<From>,
@@ -884,11 +887,11 @@ namespace unicode {
return true;
}
- // Facet version
- template<typename Facet, std::enable_if_t<std::is_empty<Facet>::value, bool> = true>
- bool is_valid_utf(const typename Facet::string_type& s)
+ // Encoding version
+ template<typename Encoding, std::enable_if_t<std::is_empty<Encoding>::value, bool> = true>
+ bool is_valid_utf(const typename Encoding::string_type& s)
{
- return validate_utf<typename Facet::value_type>(s);
+ return validate_utf<typename Encoding::value_type>(s);
}
} // namespace unicode
diff --git a/src/test-unicode.cpp b/src/test-unicode.cpp
index 99e164b..70ec453 100644
--- a/src/test-unicode.cpp
+++ b/src/test-unicode.cpp
@@ -281,8 +281,8 @@ void test_utf_to_utf(std::tuple<Ts...>& t)
BOOST_CHECK_MESSAGE(std::get<j>(t) == result, "Container: From " << typeid(From).name() << "(" << i << ", " << std::get<i>(t) << ") to " << typeid(To).name() << "(" << j << ", " << std::get<j>(t) << "), got " << result);
// test facet interface
- result = unicode::convert<typename unicode::Encoding<typename From::value_type>::Facet, typename unicode::Encoding<typename To::value_type>::Facet>(std::get<i>(t));
- BOOST_CHECK_MESSAGE(std::get<j>(t) == result, "Facet: From " << typeid(From).name() << "(" << i << ", " << std::get<i>(t) << ") to " << typeid(To).name() << "(" << j << ", " << std::get<j>(t) << "), got " << result);
+ result = unicode::convert<typename unicode::Encoding_t<typename From::value_type>, typename unicode::Encoding_t<typename To::value_type>>(std::get<i>(t));
+ BOOST_CHECK_MESSAGE(std::get<j>(t) == result, "Encoding: From " << typeid(From).name() << "(" << i << ", " << std::get<i>(t) << ") to " << typeid(To).name() << "(" << j << ", " << std::get<j>(t) << "), got " << result);
// test actual results by comparing with boost::locale::conv results
BOOST_CHECK_EQUAL(result, (boost::locale::conv::utf_to_utf<typename To::value_type, typename From::value_type>(std::get<i>(t))));
@@ -317,9 +317,9 @@ void test_is_valid_utf(std::tuple<Ts...>& t)
result = unicode::is_valid_utf<T>(std::get<i>(t));
BOOST_CHECK_MESSAGE(result == true, "is_valid_utf w/ " << typeid(T).name() << "(" << i << ", " << std::get<i>(t) << "), got " << result);
- // test via Facet
- result = unicode::is_valid_utf<typename unicode::Encoding<typename T::value_type>::Facet>(std::get<i>(t));
- BOOST_CHECK_MESSAGE(result == true, "is_valid_utf w/ " << typeid(typename unicode::Encoding<typename T::value_type>::Facet).name() << "(" << i << ", " << std::get<i>(t) << "), got " << result);
+ // test via Encoding
+ result = unicode::is_valid_utf<typename unicode::Encoding_t<typename T::value_type>>(std::get<i>(t));
+ BOOST_CHECK_MESSAGE(result == true, "is_valid_utf w/ " << typeid(typename unicode::Encoding_t<typename T::value_type>).name() << "(" << i << ", " << std::get<i>(t) << "), got " << result);
// iterate over other combinations
if constexpr (i + 1 < std::tuple_size<typename std::remove_reference<decltype(t)>::type>::value)
@@ -350,7 +350,7 @@ void test_utf_to_utf_failure(std::basic_string<From>& s)
// via container
try {
- (void) unicode::convert<typename unicode::Encoding<From>::Facet::string_type, typename unicode::Encoding<To>::Facet::string_type>(s);
+ (void) unicode::convert<typename unicode::Encoding_t<From>::string_type, typename unicode::Encoding_t<To>::string_type>(s);
BOOST_ERROR("Container type: Expected exception at index: " << index << ", " << typeid(From).name() << " -> " << typeid(To).name());
} catch (const std::invalid_argument&) {
// OK: this is an expected exception for convert() on bad input
@@ -360,8 +360,8 @@ void test_utf_to_utf_failure(std::basic_string<From>& s)
// via facet
try {
- (void) unicode::convert<typename unicode::Encoding<From>::Facet,typename unicode::Encoding<To>::Facet>(s);
- BOOST_ERROR("Facet: Expected exception at index: " << index << ", " << typeid(From).name() << " -> " << typeid(To).name());
+ (void) unicode::convert<typename unicode::Encoding_t<From>,typename unicode::Encoding_t<To>>(s);
+ BOOST_ERROR("Encoding: Expected exception at index: " << index << ", " << typeid(From).name() << " -> " << typeid(To).name());
} catch (const std::invalid_argument&) {
// OK: this is an expected exception for convert() on bad input
} catch (const std::exception& ex) {
@@ -393,7 +393,7 @@ void test_is_valid_utf_failure(std::basic_string<T>& s)
BOOST_CHECK_MESSAGE(unicode::is_valid_utf<typename std::basic_string<T>>(s) == false, "Expected bad UTF at index: " << index << ", " << typeid(T).name());
- BOOST_CHECK_MESSAGE(unicode::is_valid_utf<typename unicode::Encoding<T>::Facet>(s) == false, "Expected bad UTF at index: " << index << ", " << typeid(typename unicode::Encoding<T>::Facet).name());
+ BOOST_CHECK_MESSAGE(unicode::is_valid_utf<typename unicode::Encoding_t<T>>(s) == false, "Expected bad UTF at index: " << index << ", " << typeid(typename unicode::Encoding_t<T>).name());
// iterate over remaining types
if constexpr (index + 1 < std::tuple_size<Collection>::value)
@@ -506,7 +506,7 @@ void test_random_invalid(random_context& rc, size_t length)
// facet interface
try {
- To result{unicode::convert<typename unicode::Encoding<typename From::value_type>::Facet,typename unicode::Encoding<typename To::value_type>::Facet>(r)};
+ To result{unicode::convert<typename unicode::Encoding_t<typename From::value_type>,typename unicode::Encoding_t<typename To::value_type>>(r)};
if (r.empty()) {
BOOST_CHECK(result.empty());
@@ -573,12 +573,12 @@ void test_random_valid(random_context& rc, size_t length, const std::string& des
// Fill From data list
std::vector<From> list;
std::transform(u32list.begin(), u32list.end(), std::back_inserter(list), [](const std::u32string& s){
- return unicode::convert<unicode::UTF_32, typename unicode::Encoding<typename From::value_type>::Facet>(s);
+ return unicode::convert<unicode::UTF_32, typename unicode::Encoding_t<typename From::value_type>>(s);
});
for (int i = 0; i < list.size(); i++) {
BOOST_CHECK(list[i].size() >= u32list[i].size());
- To result{unicode::convert<typename unicode::Encoding<typename From::value_type>::Facet,typename unicode::Encoding<typename To::value_type>::Facet>(list[i])};
+ To result{unicode::convert<typename unicode::Encoding_t<typename From::value_type>,typename unicode::Encoding_t<typename To::value_type>>(list[i])};
BOOST_CHECK(result.size() >= u32list[i].size());
auto boost_result{boost::locale::conv::utf_to_utf<typename To::value_type, typename From::value_type>(list[i])};
BOOST_CHECK_EQUAL(result, boost_result);
@@ -590,7 +590,7 @@ void test_random_valid(random_context& rc, size_t length, const std::string& des
" from UTF-"s + std::to_string(sizeof(typename From::value_type) * 8) +
" to UTF-"s + std::to_string(sizeof(typename To::value_type) * 8));
for (const auto& i: list)
- To result{unicode::convert<typename unicode::Encoding<typename From::value_type>::Facet,typename unicode::Encoding<typename To::value_type>::Facet>(i)};
+ To result{unicode::convert<typename unicode::Encoding_t<typename From::value_type>,typename unicode::Encoding_t<typename To::value_type>>(i)};
}
{