feat: text ouptput should fallback to the portable mode when UTF-8 is not used for text_encoding

This commit is contained in:
Mateusz Pusz
2024-11-26 10:59:02 +01:00
parent 3b14b588e9
commit febdaec85e
2 changed files with 12 additions and 15 deletions

View File

@@ -33,8 +33,11 @@
import std;
#else
#include <cstdint>
#endif
#endif
#if __cpp_lib_text_encoding
#include <text_encoding>
#endif // __cpp_lib_text_encoding
#endif // MP_UNITS_IMPORT_STD
#endif // MP_UNITS_IN_MODULE_INTERFACE
namespace mp_units::detail {
@@ -102,6 +105,11 @@ constexpr Out copy(const symbol_text<N, M>& txt, character_set char_set, Out out
if constexpr (is_same_v<CharT, char8_t>)
return ::mp_units::detail::copy(txt.utf8().begin(), txt.utf8().end(), out);
else if constexpr (is_same_v<CharT, char>) {
#if __cpp_lib_text_encoding
if (std::text_encoding::literal().mib() != std::text_encoding::id::UTF8)
// fallback to portable mode
return ::mp_units::detail::copy(txt.portable().begin(), txt.portable().end(), out);
#endif
for (const char8_t ch : txt.utf8()) *out++ = static_cast<char>(ch);
return out;
} else

View File

@@ -38,19 +38,8 @@ import std;
#include <compare> // IWYU pragma: export
#include <cstddef>
#include <cstdint>
#endif
#endif
#if __cpp_lib_text_encoding
#ifndef MP_UNITS_IN_MODULE_INTERFACE
#ifdef MP_UNITS_IMPORT_STD
import std;
#else
#include <text_encoding>
#endif
#endif
static_assert(std::text_encoding::literal().mib() == std::text_encoding::id::UTF8);
#endif
#endif // MP_UNITS_IMPORT_STD
#endif // MP_UNITS_IN_MODULE_INTERFACE
namespace mp_units {