From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: [committed] libstdc++: Add helper function in <format>
Date: Thu, 22 Dec 2022 23:38:16 +0000 [thread overview]
Message-ID: <20221222233816.772318-1-jwakely@redhat.com> (raw)
Tested x86_64-linux, sparc-solaris2.11, powerpc-aix. Pushed to trunk.
-- >8 --
Add a new __format::__write_padded_as_spec helper to remove duplicated
code in formatter specializations.
libstdc++-v3/ChangeLog:
* include/std/format (__format::__write_padded_as_spec): New
function.
(__format::__formatter_str, __format::__formatter_int::format)
(formatter<const void*, charT>): Use it.
---
libstdc++-v3/include/std/format | 75 +++++++++++++++------------------
1 file changed, 34 insertions(+), 41 deletions(-)
diff --git a/libstdc++-v3/include/std/format b/libstdc++-v3/include/std/format
index 9c928371415..98421e8c123 100644
--- a/libstdc++-v3/include/std/format
+++ b/libstdc++-v3/include/std/format
@@ -597,6 +597,7 @@ namespace __format
return __dest;
}
+ // Write STR to OUT (and do so efficiently if OUT is a _Sink_iter).
template<typename _Out, typename _CharT>
requires output_iterator<_Out, const _CharT&>
inline _Out
@@ -668,6 +669,30 @@ namespace __format
return __out;
}
+ // Write STR to OUT, with alignment and padding as determined by SPEC.
+ // pre: __spec._M_align != _Align_default || __align != _Align_default
+ template<typename _CharT, typename _Out>
+ _Out
+ __write_padded_as_spec(basic_string_view<type_identity_t<_CharT>> __str,
+ size_t __estimated_width,
+ basic_format_context<_Out, _CharT>& __fc,
+ const _Spec<_CharT>& __spec,
+ _Align __align = _Align_left)
+ {
+ size_t __width = __spec._M_get_width(__fc);
+
+ if (__width <= __estimated_width)
+ return __format::__write(__fc.out(), __str);
+
+ const size_t __nfill = __width - __estimated_width;
+
+ if (__spec._M_align)
+ __align = __spec._M_align;
+
+ return __format::__write_padded(__fc.out(), __str, __align, __nfill,
+ __spec._M_fill);
+ }
+
// A lightweight optional<locale>.
struct _Optional_locale
{
@@ -799,7 +824,7 @@ namespace __format
}
template<typename _Out>
- typename basic_format_context<_Out, _CharT>::iterator
+ _Out
format(basic_string_view<_CharT> __s,
basic_format_context<_Out, _CharT>& __fc) const
{
@@ -824,16 +849,8 @@ namespace __format
}
}
- size_t __width = _M_spec._M_get_width(__fc);
-
- if (__width <= __estimated_width)
- return __format::__write(__fc.out(), __s);
-
- const size_t __nfill = __width - __estimated_width;
- _Align __align = _M_spec._M_align ? _M_spec._M_align : _Align_left;
-
- return __format::__write_padded(__fc.out(), __s,
- __align, __nfill, _M_spec._M_fill);
+ return __format::__write_padded_as_spec(__s, __estimated_width,
+ __fc, _M_spec);
}
#if __cpp_lib_format_ranges
@@ -1089,32 +1106,16 @@ namespace __format
__est_width = __s.size();
}
- return _M_format_str(__s, __est_width, __fc);
+ return __format::__write_padded_as_spec(__s, __est_width, __fc,
+ _M_spec);
}
template<typename _Out>
typename basic_format_context<_Out, _CharT>::iterator
_M_format_character(_CharT __c,
basic_format_context<_Out, _CharT>& __fc) const
- { return _M_format_str({&__c, 1u}, 1, __fc); }
-
- template<typename _Out>
- typename basic_format_context<_Out, _CharT>::iterator
- _M_format_str(basic_string_view<_CharT> __str, size_t __est_width,
- basic_format_context<_Out, _CharT>& __fc) const
{
- // TODO: this is identical to last part of __formatter_str::format
- // so refactor to reuse the same code.
-
- size_t __width = _M_spec._M_get_width(__fc);
-
- if (__width <= __est_width)
- return __format::__write(__fc.out(), __str);
-
- size_t __nfill = __width - __est_width;
- _Align __align = _M_spec._M_align ? _M_spec._M_align : _Align_left;
- return __format::__write_padded(__fc.out(), __str,
- __align, __nfill, _M_spec._M_fill);
+ return __format::__write_padded_as_spec({&__c, 1u}, 1, __fc, _M_spec);
}
template<typename _Int>
@@ -2135,20 +2136,12 @@ namespace __format
__str = wstring_view(__p, __n);
}
- size_t __width = _M_spec._M_get_width(__fc);
-
- if (__width <= (size_t)__n)
- return __format::__write(__fc.out(), __str);
-
- size_t __nfill = __width - __n;
- __format::_Align __align
- = _M_spec._M_align ? _M_spec._M_align : __format::_Align_right;
- return __format::__write_padded(__fc.out(), __str,
- __align, __nfill, _M_spec._M_fill);
+ return __format::__write_padded_as_spec(__str, __n, __fc, _M_spec,
+ __format::_Align_right);
}
private:
- __format::_Spec<_CharT> _M_spec{}; // XXX don't need full spec?
+ __format::_Spec<_CharT> _M_spec{};
};
template<__format::__char _CharT>
--
2.38.1
reply other threads:[~2022-12-22 23:38 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20221222233816.772318-1-jwakely@redhat.com \
--to=jwakely@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=libstdc++@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).