From 51d196f962af681bc5fbbc7015169e6ab8188eb2 Mon Sep 17 00:00:00 2001 From: achabense <60953653+achabense@users.noreply.github.com> Date: Fri, 23 Jun 2023 19:10:59 +0800 Subject: [PATCH 1/8] - redundant concept arg --- stl/inc/format | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stl/inc/format b/stl/inc/format index 1ed63d4ec7d..8c58c116547 100644 --- a/stl/inc/format +++ b/stl/inc/format @@ -181,7 +181,7 @@ concept _Parse_spec_callbacks = _Parse_align_callbacks<_Ty, _CharT> && _Parse_precision_callbacks<_Ty, _CharT> && _Width_adapter_callbacks<_Ty, _CharT> && _Precision_adapter_callbacks<_Ty, _CharT> - && requires(_Ty _At, basic_string_view<_CharT> _Sv, _Fmt_align _Aln, _Fmt_sign _Sgn) { + && requires(_Ty _At, _Fmt_sign _Sgn) { { _At._On_sign(_Sgn) } -> same_as; { _At._On_hash() } -> same_as; { _At._On_zero() } -> same_as; From 493d77267115220f299b716191b5d92da2c40c7a Mon Sep 17 00:00:00 2001 From: achabense <60953653+achabense@users.noreply.github.com> Date: Fri, 23 Jun 2023 19:36:16 +0800 Subject: [PATCH 2/8] inline `_Estimate_required_capacity` --- stl/inc/format | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/stl/inc/format b/stl/inc/format index 8c58c116547..7857d51492d 100644 --- a/stl/inc/format +++ b/stl/inc/format @@ -2089,19 +2089,21 @@ public: } _NODISCARD size_t _Estimate_required_capacity() const noexcept { - using _CharT = typename _Context::char_type; - size_t _Result = 0; - const auto _Visitor = [&_Result](const _ArgTy _Arg) noexcept { - if constexpr (is_same_v<_ArgTy, basic_string_view<_CharT>>) { - _Result += _Arg.size(); - } else if constexpr (is_same_v<_ArgTy, const _CharT*>) { + using _CharT = typename _Context::char_type; + size_t _Result = 0; + for (size_t _Idx = 0; _Idx < _Num_args; ++_Idx) { + const auto _Packed_index = _Index_array[_Idx]; + const auto _Arg_type = _Packed_index._Type(); + if (_Arg_type == _Basic_format_arg_type::_String_type) { + const auto _Arg_storage = + reinterpret_cast(_Index_array + _Num_args) + _Packed_index._Index; + const auto _View = _Get_value_from_memory>(_Arg_storage); + _Result += _View.size(); + } else if (_Arg_type == _Basic_format_arg_type::_CString_type) { _Result += 32; // estimate for length of null-terminated strings } else { _Result += 8; // estimate for length of all other arguments } - }; - for (size_t _Idx = 0; _Idx < _Num_args; ++_Idx) { - _STD visit_format_arg(_Visitor, get(_Idx)); } return _Result; } From ebd85d0279012cf6c7f5b15b723b680ba75cbd88 Mon Sep 17 00:00:00 2001 From: achabense <60953653+achabense@users.noreply.github.com> Date: Fri, 23 Jun 2023 19:52:12 +0800 Subject: [PATCH 3/8] nit --- stl/inc/format | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/stl/inc/format b/stl/inc/format index 7857d51492d..bcc57b3c33e 100644 --- a/stl/inc/format +++ b/stl/inc/format @@ -2089,15 +2089,16 @@ public: } _NODISCARD size_t _Estimate_required_capacity() const noexcept { - using _CharT = typename _Context::char_type; + using _CharType = typename _Context::char_type; size_t _Result = 0; + for (size_t _Idx = 0; _Idx < _Num_args; ++_Idx) { const auto _Packed_index = _Index_array[_Idx]; const auto _Arg_type = _Packed_index._Type(); if (_Arg_type == _Basic_format_arg_type::_String_type) { const auto _Arg_storage = reinterpret_cast(_Index_array + _Num_args) + _Packed_index._Index; - const auto _View = _Get_value_from_memory>(_Arg_storage); + const auto _View = _Get_value_from_memory>(_Arg_storage); _Result += _View.size(); } else if (_Arg_type == _Basic_format_arg_type::_CString_type) { _Result += 32; // estimate for length of null-terminated strings From 7ea866d46b58b5b3aca62aaf32d8e633d088f07a Mon Sep 17 00:00:00 2001 From: achabense <60953653+achabense@users.noreply.github.com> Date: Fri, 23 Jun 2023 20:32:39 +0800 Subject: [PATCH 4/8] refine _Buffer_to_uppercase logic --- stl/inc/format | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/stl/inc/format b/stl/inc/format index bcc57b3c33e..f7113fc3f54 100644 --- a/stl/inc/format +++ b/stl/inc/format @@ -2592,9 +2592,12 @@ _NODISCARD _OutputIt _Write_sign(_OutputIt _Out, const _Fmt_sign _Sgn, const boo return _Out; } -inline void _Buffer_to_uppercase(char* _First, const char* _Last) { +// We don't need to care about locale here. +inline void _Buffer_to_uppercase(char* _First, const char* _Last) noexcept { for (; _First != _Last; ++_First) { - *_First = static_cast(_CSTD toupper(*_First)); + if (*_First >= 'a' && *_First <= 'z') { + *_First -= 'a' - 'A'; + } } } @@ -2726,18 +2729,13 @@ _NODISCARD _OutputIt _Write_integral( } int _Base = 10; - bool _To_upper = false; switch (_Specs._Type) { case 'B': - _To_upper = true; - [[fallthrough]]; case 'b': _Base = 2; break; case 'X': - _To_upper = true; - [[fallthrough]]; case 'x': _Base = 16; break; @@ -2763,7 +2761,7 @@ _NODISCARD _OutputIt _Write_integral( _Buffer_start += 1; } - if (_To_upper) { + if (_Specs._Type == 'X') { _Buffer_to_uppercase(_Buffer_start, _End); } @@ -2968,9 +2966,10 @@ _NODISCARD _OutputIt _Fmt_write( } } + _STL_INTERNAL_CHECK(_Exponent == 'e' || _Exponent == 'p'); if (_To_upper) { _Buffer_to_uppercase(_Buffer_start, _Result.ptr); - _Exponent = static_cast(_CSTD toupper(_Exponent)); + _Exponent -= 'a' - 'A'; } const auto _Is_finite = (_STD isfinite)(_Value); From 06d531d785fe9547ec8a686e0bd3042d8ce19d53 Mon Sep 17 00:00:00 2001 From: achabense <60953653+achabense@users.noreply.github.com> Date: Fri, 23 Jun 2023 20:39:09 +0800 Subject: [PATCH 5/8] nit --- stl/inc/format | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stl/inc/format b/stl/inc/format index f7113fc3f54..0eb06a8045f 100644 --- a/stl/inc/format +++ b/stl/inc/format @@ -2592,7 +2592,7 @@ _NODISCARD _OutputIt _Write_sign(_OutputIt _Out, const _Fmt_sign _Sgn, const boo return _Out; } -// We don't need to care about locale here. +// We don't need to use `toupper` here. inline void _Buffer_to_uppercase(char* _First, const char* _Last) noexcept { for (; _First != _Last; ++_First) { if (*_First >= 'a' && *_First <= 'z') { From 5ac1efdb24d176d2a065652e79d87e6196100788 Mon Sep 17 00:00:00 2001 From: achabense <60953653+achabense@users.noreply.github.com> Date: Fri, 23 Jun 2023 20:58:39 +0800 Subject: [PATCH 6/8] nit --- stl/inc/format | 1 - 1 file changed, 1 deletion(-) diff --git a/stl/inc/format b/stl/inc/format index 0eb06a8045f..622cae61714 100644 --- a/stl/inc/format +++ b/stl/inc/format @@ -2592,7 +2592,6 @@ _NODISCARD _OutputIt _Write_sign(_OutputIt _Out, const _Fmt_sign _Sgn, const boo return _Out; } -// We don't need to use `toupper` here. inline void _Buffer_to_uppercase(char* _First, const char* _Last) noexcept { for (; _First != _Last; ++_First) { if (*_First >= 'a' && *_First <= 'z') { From 8d9f3e68760f6ff47e9af72484f7f24de1dc5775 Mon Sep 17 00:00:00 2001 From: achabense <60953653+achabense@users.noreply.github.com> Date: Fri, 23 Jun 2023 20:32:39 +0800 Subject: [PATCH 7/8] refine _Buffer_to_uppercase logic --- stl/inc/format | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/stl/inc/format b/stl/inc/format index bcc57b3c33e..622cae61714 100644 --- a/stl/inc/format +++ b/stl/inc/format @@ -2592,9 +2592,11 @@ _NODISCARD _OutputIt _Write_sign(_OutputIt _Out, const _Fmt_sign _Sgn, const boo return _Out; } -inline void _Buffer_to_uppercase(char* _First, const char* _Last) { +inline void _Buffer_to_uppercase(char* _First, const char* _Last) noexcept { for (; _First != _Last; ++_First) { - *_First = static_cast(_CSTD toupper(*_First)); + if (*_First >= 'a' && *_First <= 'z') { + *_First -= 'a' - 'A'; + } } } @@ -2726,18 +2728,13 @@ _NODISCARD _OutputIt _Write_integral( } int _Base = 10; - bool _To_upper = false; switch (_Specs._Type) { case 'B': - _To_upper = true; - [[fallthrough]]; case 'b': _Base = 2; break; case 'X': - _To_upper = true; - [[fallthrough]]; case 'x': _Base = 16; break; @@ -2763,7 +2760,7 @@ _NODISCARD _OutputIt _Write_integral( _Buffer_start += 1; } - if (_To_upper) { + if (_Specs._Type == 'X') { _Buffer_to_uppercase(_Buffer_start, _End); } @@ -2968,9 +2965,10 @@ _NODISCARD _OutputIt _Fmt_write( } } + _STL_INTERNAL_CHECK(_Exponent == 'e' || _Exponent == 'p'); if (_To_upper) { _Buffer_to_uppercase(_Buffer_start, _Result.ptr); - _Exponent = static_cast(_CSTD toupper(_Exponent)); + _Exponent -= 'a' - 'A'; } const auto _Is_finite = (_STD isfinite)(_Value); From 69bbd50f050a5aea20a07112faecb174a284576c Mon Sep 17 00:00:00 2001 From: achabense <60953653+achabense@users.noreply.github.com> Date: Fri, 23 Jun 2023 22:02:21 +0800 Subject: [PATCH 8/8] formatting --- stl/inc/format | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stl/inc/format b/stl/inc/format index 622cae61714..fdcf65e7908 100644 --- a/stl/inc/format +++ b/stl/inc/format @@ -2090,7 +2090,7 @@ public: _NODISCARD size_t _Estimate_required_capacity() const noexcept { using _CharType = typename _Context::char_type; - size_t _Result = 0; + size_t _Result = 0; for (size_t _Idx = 0; _Idx < _Num_args; ++_Idx) { const auto _Packed_index = _Index_array[_Idx]; @@ -2727,7 +2727,7 @@ _NODISCARD _OutputIt _Write_integral( _Specs._Sgn = _Fmt_sign::_Minus; } - int _Base = 10; + int _Base = 10; switch (_Specs._Type) { case 'B':