[libcxx-commits] [libcxx] 75ecd1f - [libcxx][format] Fix how we handle char traits in formatter<string> and formatter<string_view>

Daniel McIntosh via libcxx-commits libcxx-commits at lists.llvm.org
Thu Nov 11 11:49:46 PST 2021


Author: Daniel McIntosh
Date: 2021-11-11T14:49:40-05:00
New Revision: 75ecd1f38c6feab159034dc3bbf30aaaf72bbac2

URL: https://github.com/llvm/llvm-project/commit/75ecd1f38c6feab159034dc3bbf30aaaf72bbac2
DIFF: https://github.com/llvm/llvm-project/commit/75ecd1f38c6feab159034dc3bbf30aaaf72bbac2.diff

LOG: [libcxx][format] Fix how we handle char traits in formatter<string> and formatter<string_view>

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

Reviewed By: Mordante, #libc, Quuxplusone

Differential Revision: https://reviews.llvm.org/D112017

Added: 
    

Modified: 
    libcxx/include/__format/formatter_string.h

Removed: 
    


################################################################################
diff  --git a/libcxx/include/__format/formatter_string.h b/libcxx/include/__format/formatter_string.h
index 45d1147e445da..2be36a1ba9473 100644
--- a/libcxx/include/__format/formatter_string.h
+++ b/libcxx/include/__format/formatter_string.h
@@ -119,7 +119,7 @@ struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT
 
   _LIBCPP_HIDE_FROM_ABI auto format(const _CharT __str[_Size], auto& __ctx)
       -> decltype(__ctx.out()) {
-    return _Base::format(_VSTD::basic_string_view<_CharT>(__str, _Size), __ctx);
+    return _Base::format(basic_string_view<_CharT>(__str, _Size), __ctx);
   }
 };
 
@@ -133,15 +133,24 @@ struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT
   _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);
+    // drop _Traits and _Allocator
+    return _Base::format(basic_string_view<_CharT>(__str.data(), __str.size()), __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> {
+  using _Base = __format_spec::__formatter_string<_CharT>;
+
+  _LIBCPP_HIDE_FROM_ABI auto
+  format(basic_string_view<_CharT, _Traits> __str, auto& __ctx)
+      -> decltype(__ctx.out()) {
+    // drop _Traits
+    return _Base::format(basic_string_view<_CharT>(__str.data(), __str.size()), __ctx);
+  }
+};
 
 #endif // !defined(_LIBCPP_HAS_NO_CONCEPTS)
 


        


More information about the libcxx-commits mailing list