[libcxx-commits] [libcxx] 0569cc1 - [libc++] Simplify the implementation of __formatter::__fill a bit (#147777)
via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Nov 3 01:41:59 PST 2025
Author: Nikolas Klauser
Date: 2025-11-03T10:41:55+01:00
New Revision: 0569cc141bacfa5450a54e8d65eb32ea13c37d9e
URL: https://github.com/llvm/llvm-project/commit/0569cc141bacfa5450a54e8d65eb32ea13c37d9e
DIFF: https://github.com/llvm/llvm-project/commit/0569cc141bacfa5450a54e8d65eb32ea13c37d9e.diff
LOG: [libc++] Simplify the implementation of __formatter::__fill a bit (#147777)
This replaces some SFINAE with `if constexpr`.
Added:
Modified:
libcxx/include/__format/formatter_output.h
Removed:
################################################################################
diff --git a/libcxx/include/__format/formatter_output.h b/libcxx/include/__format/formatter_output.h
index d53b6cec707d8..63dd7fcacdcc9 100644
--- a/libcxx/include/__format/formatter_output.h
+++ b/libcxx/include/__format/formatter_output.h
@@ -151,45 +151,41 @@ _LIBCPP_HIDE_FROM_ABI _OutIt __fill(_OutIt __out_it, size_t __n, _CharT __value)
}
}
-# if _LIBCPP_HAS_UNICODE
template <__fmt_char_type _CharT, output_iterator<const _CharT&> _OutIt>
- requires(same_as<_CharT, char>)
_LIBCPP_HIDE_FROM_ABI _OutIt __fill(_OutIt __out_it, size_t __n, __format_spec::__code_point<_CharT> __value) {
- std::size_t __bytes = std::countl_one(static_cast<unsigned char>(__value.__data[0]));
- if (__bytes == 0)
- return __formatter::__fill(std::move(__out_it), __n, __value.__data[0]);
-
- for (size_t __i = 0; __i < __n; ++__i)
- __out_it = __formatter::__copy(
- std::addressof(__value.__data[0]), std::addressof(__value.__data[0]) + __bytes, std::move(__out_it));
- return __out_it;
-}
-
+# if _LIBCPP_HAS_UNICODE
+ if constexpr (same_as<_CharT, char>) {
+ std::size_t __bytes = std::countl_one(static_cast<unsigned char>(__value.__data[0]));
+ if (__bytes == 0)
+ return __formatter::__fill(std::move(__out_it), __n, __value.__data[0]);
+
+ for (size_t __i = 0; __i < __n; ++__i)
+ __out_it = __formatter::__copy(
+ std::addressof(__value.__data[0]), std::addressof(__value.__data[0]) + __bytes, std::move(__out_it));
+ return __out_it;
# if _LIBCPP_HAS_WIDE_CHARACTERS
-template <__fmt_char_type _CharT, output_iterator<const _CharT&> _OutIt>
- requires(same_as<_CharT, wchar_t> && sizeof(wchar_t) == 2)
-_LIBCPP_HIDE_FROM_ABI _OutIt __fill(_OutIt __out_it, size_t __n, __format_spec::__code_point<_CharT> __value) {
- if (!__unicode::__is_high_surrogate(__value.__data[0]))
- return __formatter::__fill(std::move(__out_it), __n, __value.__data[0]);
-
- for (size_t __i = 0; __i < __n; ++__i)
- __out_it = __formatter::__copy(
- std::addressof(__value.__data[0]), std::addressof(__value.__data[0]) + 2, std::move(__out_it));
- return __out_it;
-}
-
-template <__fmt_char_type _CharT, output_iterator<const _CharT&> _OutIt>
- requires(same_as<_CharT, wchar_t> && sizeof(wchar_t) == 4)
-_LIBCPP_HIDE_FROM_ABI _OutIt __fill(_OutIt __out_it, size_t __n, __format_spec::__code_point<_CharT> __value) {
- return __formatter::__fill(std::move(__out_it), __n, __value.__data[0]);
-}
+ } else if constexpr (same_as<_CharT, wchar_t>) {
+ if constexpr (sizeof(wchar_t) == 2) {
+ if (!__unicode::__is_high_surrogate(__value.__data[0]))
+ return __formatter::__fill(std::move(__out_it), __n, __value.__data[0]);
+
+ for (size_t __i = 0; __i < __n; ++__i)
+ __out_it = __formatter::__copy(
+ std::addressof(__value.__data[0]), std::addressof(__value.__data[0]) + 2, std::move(__out_it));
+ return __out_it;
+ } else if constexpr (sizeof(wchar_t) == 4) {
+ return __formatter::__fill(std::move(__out_it), __n, __value.__data[0]);
+ } else {
+ static_assert(false, "expected sizeof(wchar_t) to be 2 or 4");
+ }
# endif // _LIBCPP_HAS_WIDE_CHARACTERS
-# else // _LIBCPP_HAS_UNICODE
-template <__fmt_char_type _CharT, output_iterator<const _CharT&> _OutIt>
-_LIBCPP_HIDE_FROM_ABI _OutIt __fill(_OutIt __out_it, size_t __n, __format_spec::__code_point<_CharT> __value) {
+ } else {
+ static_assert(false, "Unexpected CharT");
+ }
+# else // _LIBCPP_HAS_UNICODE
return __formatter::__fill(std::move(__out_it), __n, __value.__data[0]);
+# endif // _LIBCPP_HAS_UNICODE
}
-# endif // _LIBCPP_HAS_UNICODE
/// Writes the input to the output with the required padding.
///
More information about the libcxx-commits
mailing list