From f30a9375bab6b99901f4478e0264ceb8e86eccba Mon Sep 17 00:00:00 2001 From: Alex Guteniev Date: Tue, 25 Jul 2023 14:42:47 +0300 Subject: [PATCH 1/3] Towards #3858 : resize_and_overwrite --- stl/inc/bitset | 12 ++++++------ stl/inc/xstring | 17 ++++++++++++++--- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/stl/inc/bitset b/stl/inc/bitset index b91f8a6eb38..2c438e7dc21 100644 --- a/stl/inc/bitset +++ b/stl/inc/bitset @@ -347,13 +347,13 @@ public: _NODISCARD _CONSTEXPR23 basic_string<_Elem, _Tr, _Alloc> to_string( const _Elem _Elem0 = static_cast<_Elem>('0'), const _Elem _Elem1 = static_cast<_Elem>('1')) const { // convert bitset to string - basic_string<_Elem, _Tr, _Alloc> _Str(_Bits, _Elem0); - for (size_t _Pos = 0; _Pos < _Bits; ++_Pos) { - if (_Subscript(_Bits - 1 - _Pos)) { - _Str[_Pos] = _Elem1; + basic_string<_Elem, _Tr, _Alloc> _Str; + _Str._Resize_and_overwrite(_Bits, [this, _Elem0, _Elem1](_Elem* _Buf, size_t _Len) { + for (size_t _Pos = 0; _Pos < _Len; ++_Pos) { + _Buf[_Pos] = _Subscript(_Len - 1 - _Pos) ? _Elem1 : _Elem0; } - } - + return _Len; + }); return _Str; } diff --git a/stl/inc/xstring b/stl/inc/xstring index 455e6ae8f12..1ab31f10c1e 100644 --- a/stl/inc/xstring +++ b/stl/inc/xstring @@ -4158,11 +4158,16 @@ public: } } -#if _HAS_CXX23 #pragma warning(push) #pragma warning(disable : 4018) // '<=': signed/unsigned mismatch (we compare to 0 before _New_size below, so it's safe) template - constexpr void resize_and_overwrite(_CRT_GUARDOVERFLOW const size_type _New_size, _Operation _Op) { + constexpr void +#if _HAS_CXX23 + resize_and_overwrite +#else // ^^^ _HAS_CXX23 / !_HAS_CXX23 vvv + _Resize_and_overwrite +#endif // ^^^ !_HAS_CXX23 ^^^ + (_CRT_GUARDOVERFLOW const size_type _New_size, _Operation _Op) { if (_Mypair._Myval2._Myres < _New_size) { _Reallocate_grow_by(_New_size - _Mypair._Myval2._Mysize, [](_Elem* const _New_ptr, const _Elem* const _Old_ptr, const size_type _Old_size) { @@ -4180,7 +4185,13 @@ public: _Eos(static_cast(_Result_size)); } #pragma warning(pop) -#endif // _HAS_CXX23 + +#if _HAS_CXX23 + template + constexpr void _Resize_and_overwrite(_CRT_GUARDOVERFLOW const size_type _New_size, _Operation _Op) { + return resize_and_overwrite(_New_size, _Op); + } +#endif // !_HAS_CXX23 _NODISCARD _CONSTEXPR20 size_type capacity() const noexcept { return _Mypair._Myval2._Myres; From 2696c9dfdf07c49cad2883c69bff24ebdd197370 Mon Sep 17 00:00:00 2001 From: Alex Guteniev Date: Tue, 25 Jul 2023 15:15:30 +0300 Subject: [PATCH 2/3] formating --- stl/inc/xstring | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stl/inc/xstring b/stl/inc/xstring index 1ab31f10c1e..53fe7e645b9 100644 --- a/stl/inc/xstring +++ b/stl/inc/xstring @@ -4161,7 +4161,7 @@ public: #pragma warning(push) #pragma warning(disable : 4018) // '<=': signed/unsigned mismatch (we compare to 0 before _New_size below, so it's safe) template - constexpr void + constexpr void #if _HAS_CXX23 resize_and_overwrite #else // ^^^ _HAS_CXX23 / !_HAS_CXX23 vvv @@ -4191,7 +4191,7 @@ public: constexpr void _Resize_and_overwrite(_CRT_GUARDOVERFLOW const size_type _New_size, _Operation _Op) { return resize_and_overwrite(_New_size, _Op); } -#endif // !_HAS_CXX23 +#endif // _HAS_CXX23 _NODISCARD _CONSTEXPR20 size_type capacity() const noexcept { return _Mypair._Myval2._Myres; From fbe38a04dd9720c4aed422b9d6a3dd5e0dd0e27e Mon Sep 17 00:00:00 2001 From: Alex Guteniev Date: Wed, 26 Jul 2023 17:38:34 +0300 Subject: [PATCH 3/3] no void return --- stl/inc/xstring | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stl/inc/xstring b/stl/inc/xstring index 53fe7e645b9..adb18c1f429 100644 --- a/stl/inc/xstring +++ b/stl/inc/xstring @@ -4189,7 +4189,7 @@ public: #if _HAS_CXX23 template constexpr void _Resize_and_overwrite(_CRT_GUARDOVERFLOW const size_type _New_size, _Operation _Op) { - return resize_and_overwrite(_New_size, _Op); + resize_and_overwrite(_New_size, _Op); } #endif // _HAS_CXX23