[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 9 09:54:55 PDT 2021


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

Rebased and adjusted to changes in `__put`.


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
@@ -549,14 +549,24 @@
 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;
 }
 
 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;
 }
 
@@ -691,17 +701,25 @@
 
 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;
 }
 
 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;
 }
 
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.378459.patch
Type: text/x-patch
Size: 2757 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20211009/f3b3a440/attachment.bin>


More information about the libcxx-commits mailing list