[libcxx-commits] [PATCH] D110500: [libc++][format][6/6] Improve vformat.

Mark de Wever via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Sat Oct 16 10:08:11 PDT 2021


Mordante updated this revision to Diff 380198.
Mordante added a comment.

Rebased to test with `wchar_t` changes.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D110500/new/

https://reviews.llvm.org/D110500

Files:
  libcxx/include/__format/buffer.h
  libcxx/include/format


Index: libcxx/include/format
===================================================================
--- libcxx/include/format
+++ libcxx/include/format
@@ -560,15 +560,25 @@
 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT string
 vformat(string_view __fmt, format_args __args) {
   string __res;
-  _VSTD::vformat_to(_VSTD::back_inserter(__res), __fmt, __args);
+  using _Buffer = typename __format::__container_buffer<std::string>;
+  _Buffer __buffer{&__res};
+  _VSTD::__vformat_to(
+      __format::__output_iterator<char>{_VSTD::addressof(__buffer)}, __fmt,
+      __args);
+  __buffer.out();
   return __res;
 }
 
 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT wstring
 vformat(wstring_view __fmt, wformat_args __args) {
+  using _Buffer = typename __format::__container_buffer<std::wstring>;
   wstring __res;
-  _VSTD::vformat_to(_VSTD::back_inserter(__res), __fmt, __args);
+  _Buffer __buffer{&__res};
+  _VSTD::__vformat_to(
+      __format::__output_iterator<wchar_t>{_VSTD::addressof(__buffer)}, __fmt,
+      __args);
+  __buffer.out();
   return __res;
 }
 #endif
@@ -714,18 +724,26 @@
 
 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT string
 vformat(locale __loc, string_view __fmt, format_args __args) {
+  using _Buffer = typename __format::__container_buffer<std::string>;
   string __res;
-  _VSTD::vformat_to(_VSTD::back_inserter(__res), _VSTD::move(__loc), __fmt,
-                    __args);
+  _Buffer __buffer{&__res};
+  _VSTD::__vformat_to(
+      __format::__output_iterator<char>{_VSTD::addressof(__buffer)},
+      _VSTD::move(__loc), __fmt, __args);
+  __buffer.out();
   return __res;
 }
 
 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT wstring
 vformat(locale __loc, wstring_view __fmt, wformat_args __args) {
+  using _Buffer = typename __format::__container_buffer<std::wstring>;
   wstring __res;
-  _VSTD::vformat_to(_VSTD::back_inserter(__res), _VSTD::move(__loc), __fmt,
-                    __args);
+  _Buffer __buffer{&__res};
+  _VSTD::__vformat_to(
+      __format::__output_iterator<wchar_t>{_VSTD::addressof(__buffer)},
+      _VSTD::move(__loc), __fmt, __args);
+  __buffer.out();
   return __res;
 }
 #endif
Index: libcxx/include/__format/buffer.h
===================================================================
--- libcxx/include/__format/buffer.h
+++ libcxx/include/__format/buffer.h
@@ -124,6 +124,9 @@
       back_insert_iterator<_Container> __it)
       : __container_(__it.__get_container()) {}
 
+  _LIBCPP_HIDE_FROM_ABI explicit __container_buffer(_Container* __container)
+      : __container_(__container) {}
+
   _LIBCPP_HIDE_FROM_ABI void put(_CharT __c) {
     *__buffer_it_++ = __c;
     // Profiling showed flushing after adding is more efficient than flushing


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D110500.380198.patch
Type: text/x-patch
Size: 2849 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20211016/53fabc5a/attachment-0001.bin>


More information about the libcxx-commits mailing list