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

Mark de Wever via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Sun Sep 26 08:08:07 PDT 2021


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

Directly use the `string`'s insert operation instead of using a
`back_insert_iterator` wrapper. With the other changes in this series the
`back_insert_iterator` will merely be a wrapper to select the `string`s
insert operation.

Depends on D110499 <https://reviews.llvm.org/D110499>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D110500

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


Index: libcxx/include/format
===================================================================
--- libcxx/include/format
+++ libcxx/include/format
@@ -550,14 +550,26 @@
 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>{&__format::__put<_Buffer, char>,
+                                        &__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>{&__format::__put<_Buffer, wchar_t>,
+                                           &__buffer},
+      __fmt, __args);
+  __buffer.out();
   return __res;
 }
 
@@ -696,17 +708,27 @@
 
 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>{&__format::__put<_Buffer, char>,
+                                        &__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>{&__format::__put<_Buffer, wchar_t>,
+                                           &__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
@@ -148,6 +148,9 @@
       back_insert_iterator<_Container> __it)
       : __container_(__container_extractor<_Container>{__it}.get()) {}
 
+  _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.375111.patch
Type: text/x-patch
Size: 3020 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20210926/e35c972c/attachment.bin>


More information about the libcxx-commits mailing list