Skip to content

Commit

Permalink
feat: text ouptput should fallback to the portable mode when UTF-8 is…
Browse files Browse the repository at this point in the history
… not used for `text_encoding`
  • Loading branch information
mpusz committed Nov 26, 2024
1 parent 3b14b58 commit febdaec
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
12 changes: 10 additions & 2 deletions src/core/include/mp-units/bits/text_tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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
Expand Down
15 changes: 2 additions & 13 deletions src/core/include/mp-units/framework/symbol_text.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down

0 comments on commit febdaec

Please sign in to comment.