[libcxx-commits] [PATCH] D112017: [libcxx][format] Fix how we handle char traits in formatter<string> and formatter<string_view>

Daniel McIntosh via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Mon Oct 18 10:19:43 PDT 2021


DanielMcIntosh-IBM created this revision.
DanielMcIntosh-IBM added reviewers: ldionne, Mordante, vitaut, curdeius, miscco.
DanielMcIntosh-IBM requested review of this revision.
Herald added a project: libc++.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.

Right now we drop the char_traits template argument, which presumes that
string<_CharT, _Traits> and string<_CharT> are interchangeable.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D112017

Files:
  libcxx/include/__format/formatter.h
  libcxx/include/__format/formatter_string.h


Index: libcxx/include/__format/formatter_string.h
===================================================================
--- libcxx/include/__format/formatter_string.h
+++ libcxx/include/__format/formatter_string.h
@@ -38,11 +38,10 @@
 
 namespace __format_spec {
 
-template <__formatter::__char_type _CharT>
+template <__formatter::__char_type _CharT, class _Traits = char_traits<_CharT> >
 class _LIBCPP_TEMPLATE_VIS __formatter_string : public __parser_string<_CharT> {
 public:
-  _LIBCPP_HIDE_FROM_ABI auto format(basic_string_view<_CharT> __str,
-                                    auto& __ctx) -> decltype(__ctx.out()) {
+  _LIBCPP_HIDE_FROM_ABI auto format(basic_string_view<_CharT, _Traits> __str, auto& __ctx) -> decltype(__ctx.out()) {
 
     _LIBCPP_ASSERT(this->__alignment != _Flags::_Alignment::__default,
                    "The parser should not use these defaults");
@@ -125,25 +124,23 @@
 
 // Formatter std::string.
 template <class _CharT, class _Traits, class _Allocator>
-struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT
-    formatter<basic_string<_CharT, _Traits, _Allocator>, _CharT>
-    : public __format_spec::__formatter_string<_CharT> {
-  using _Base = __format_spec::__formatter_string<_CharT>;
+struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<basic_string<_CharT, _Traits, _Allocator>, _CharT>
+    : public __format_spec::__formatter_string<_CharT, _Traits> {
+  using _Base = __format_spec::__formatter_string<_CharT, _Traits>;
 
   _LIBCPP_HIDE_FROM_ABI auto
   format(const basic_string<_CharT, _Traits, _Allocator>& __str, auto& __ctx)
       -> decltype(__ctx.out()) {
-    return _Base::format(_VSTD::basic_string_view<_CharT>(__str), __ctx);
+    return _Base::format(_VSTD::basic_string_view<_CharT, _Traits>(__str), __ctx);
   }
 };
 
 // Formatter std::string_view.
 template <class _CharT, class _Traits>
-struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT
-    formatter<basic_string_view<_CharT, _Traits>, _CharT>
-    : public __format_spec::__formatter_string<_CharT> {};
+struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<basic_string_view<_CharT, _Traits>, _CharT>
+    : public __format_spec::__formatter_string<_CharT, _Traits> {};
 
-#endif // !defined(_LIBCPP_HAS_NO_CONCEPTS)
+#  endif // !defined(_LIBCPP_HAS_NO_CONCEPTS)
 
 #endif //_LIBCPP_STD_VER > 17
 
Index: libcxx/include/__format/formatter.h
===================================================================
--- libcxx/include/__format/formatter.h
+++ libcxx/include/__format/formatter.h
@@ -181,13 +181,11 @@
  * @param __precision The width to truncate the input string to, use @c -1 for
  *                     no limit.
  */
-template <class _CharT, class _Fill>
-_LIBCPP_HIDE_FROM_ABI auto
-__write_unicode(output_iterator<const _CharT&> auto __out_it,
-                basic_string_view<_CharT> __str, ptrdiff_t __width,
-                ptrdiff_t __precision, _Fill __fill,
-                __format_spec::_Flags::_Alignment __alignment)
-    -> decltype(__out_it) {
+template <class _CharT, class _Traits, class _Fill>
+_LIBCPP_HIDE_FROM_ABI auto __write_unicode(output_iterator<const _CharT&> auto __out_it,
+                                           basic_string_view<_CharT, _Traits> __str, ptrdiff_t __width,
+                                           ptrdiff_t __precision, _Fill __fill,
+                                           __format_spec::_Flags::_Alignment __alignment) -> decltype(__out_it) {
 
   // This value changes when there Unicode column width limits the output
   // size.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D112017.380460.patch
Type: text/x-patch
Size: 3571 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20211018/f5714ff4/attachment.bin>


More information about the libcxx-commits mailing list