-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cleanups and enhancements for basic_string
#3984
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
acd0821
merge `_Memcpy_val_from`
achabense f7e8135
simplify construction from initializer_list
achabense 6ed3396
improve exception guarantee
achabense 3cda46d
improve usage of `_Calculate_growth`
achabense 14b7558
noexcept enhancements
achabense a0716eb
nitpick
achabense File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -2673,8 +2673,7 @@ private: | |||
return; | ||||
} | ||||
|
||||
_My_data._Myres = _Small_string_capacity; | ||||
size_type _New_capacity = _Calculate_growth(_Count); | ||||
size_type _New_capacity = _Calculate_growth(_Count, _Small_string_capacity, max_size()); | ||||
const pointer _New_ptr = _Allocate_for_capacity(_Al, _New_capacity); // throws | ||||
_Construct_in_place(_My_data._Bx._Ptr, _New_ptr); | ||||
|
||||
|
@@ -2731,7 +2730,7 @@ private: | |||
} | ||||
|
||||
_Elem* const _Old_ptr = _My_data._Myptr(); | ||||
size_type _New_capacity = _Calculate_growth(_My_data._Mysize); | ||||
size_type _New_capacity = _Calculate_growth(_My_data._Mysize + 1); | ||||
const pointer _New_ptr = _Allocate_for_capacity(_Al, _New_capacity); // throws | ||||
|
||||
_Traits::copy(_Unfancy(_New_ptr), _Old_ptr, _My_data._Mysize); | ||||
|
@@ -3010,15 +3009,6 @@ public: | |||
} | ||||
|
||||
private: | ||||
void _Memcpy_val_from(const basic_string& _Right) noexcept { | ||||
_STL_INTERNAL_CHECK(_Can_memcpy_val); | ||||
const auto _My_data_mem = | ||||
reinterpret_cast<unsigned char*>(_STD addressof(_Mypair._Myval2)) + _Memcpy_val_offset; | ||||
const auto _Right_data_mem = | ||||
reinterpret_cast<const unsigned char*>(_STD addressof(_Right._Mypair._Myval2)) + _Memcpy_val_offset; | ||||
_CSTD memcpy(_My_data_mem, _Right_data_mem, _Memcpy_val_size); | ||||
} | ||||
|
||||
_CONSTEXPR20 void _Take_contents(basic_string& _Right) noexcept { | ||||
// assign by stealing _Right's buffer | ||||
// pre: this != &_Right | ||||
|
@@ -3043,7 +3033,11 @@ private: | |||
} | ||||
#endif // _ITERATOR_DEBUG_LEVEL != 0 | ||||
|
||||
_Memcpy_val_from(_Right); | ||||
const auto _My_data_mem = | ||||
reinterpret_cast<unsigned char*>(_STD addressof(_Mypair._Myval2)) + _Memcpy_val_offset; | ||||
const auto _Right_data_mem = | ||||
reinterpret_cast<const unsigned char*>(_STD addressof(_Right._Mypair._Myval2)) + _Memcpy_val_offset; | ||||
_CSTD memcpy(_My_data_mem, _Right_data_mem, _Memcpy_val_size); | ||||
_Right._Tidy_init(); | ||||
return; | ||||
} | ||||
|
@@ -3075,12 +3069,12 @@ private: | |||
const auto _Right_ptr = _Right_data._Myptr(); | ||||
auto& _Al = _Getal(); | ||||
if (_Allocators_equal(_Al, _Right._Getal()) && _Result_size > _Small_string_capacity) { | ||||
_Mypair._Myval2._Alloc_proxy(_GET_PROXY_ALLOCATOR(_Alty, _Al)); | ||||
|
||||
if (_Roff != 0) { | ||||
_Traits::move(_Right_ptr, _Right_ptr + _Roff, _Result_size); | ||||
} | ||||
_Right._Eos(_Result_size); | ||||
|
||||
_Mypair._Myval2._Alloc_proxy(_GET_PROXY_ALLOCATOR(_Alty, _Al)); | ||||
_Take_contents(_Right); | ||||
} else { | ||||
_Construct<_Construct_strategy::_From_ptr>(_Right_ptr + _Roff, _Result_size); | ||||
|
@@ -3091,11 +3085,7 @@ private: | |||
public: | ||||
_CONSTEXPR20 basic_string(initializer_list<_Elem> _Ilist, const _Alloc& _Al = allocator_type()) | ||||
: _Mypair(_One_then_variadic_args_t{}, _Al) { | ||||
auto&& _Alproxy = _GET_PROXY_ALLOCATOR(_Alty, _Getal()); | ||||
_Container_proxy_ptr<_Alty> _Proxy(_Alproxy, _Mypair._Myval2); | ||||
_Tidy_init(); | ||||
assign(_Ilist.begin(), _Convert_size<size_type>(_Ilist.size())); | ||||
_Proxy._Release(); | ||||
_Construct<_Construct_strategy::_From_ptr>(_Ilist.begin(), _Convert_size<size_type>(_Ilist.size())); | ||||
} | ||||
|
||||
_CONSTEXPR20 basic_string& operator=(initializer_list<_Elem> _Ilist) { | ||||
|
@@ -4274,9 +4264,6 @@ public: | |||
auto& _My_data = _Mypair._Myval2; | ||||
auto& _Right_data = _Right._Mypair._Myval2; | ||||
|
||||
const bool _My_large = _My_data._Large_mode_engaged(); | ||||
const bool _Right_large = _Right_data._Large_mode_engaged(); | ||||
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line 4272 in 60f1885
Is it encouraged to move this using _STD swap into the if block where it is used? I'm not sure as I find using _STD swap is usually defined at the beginning of functions.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Making it as local as possible seems like an improvement. |
||||
#if !defined(_INSERT_STRING_ANNOTATION) | ||||
if constexpr (_Can_memcpy_val) { | ||||
#if _HAS_CXX20 | ||||
|
@@ -4297,6 +4284,9 @@ public: | |||
} | ||||
#endif // !defined(_INSERT_STRING_ANNOTATION) | ||||
|
||||
const bool _My_large = _My_data._Large_mode_engaged(); | ||||
const bool _Right_large = _Right_data._Large_mode_engaged(); | ||||
|
||||
if (_My_large && _Right_large) { // swap buffers, iterators preserved | ||||
swap(_My_data._Bx._Ptr, _Right_data._Bx._Ptr); // intentional ADL | ||||
} else if (_My_large) { // swap large with small | ||||
|
@@ -4801,7 +4791,7 @@ private: | |||
return *this; | ||||
} | ||||
|
||||
_CONSTEXPR20 void _Become_small() { | ||||
_CONSTEXPR20 void _Become_small() noexcept { | ||||
// release any held storage and return to small string mode | ||||
auto& _My_data = _Mypair._Myval2; | ||||
_STL_INTERNAL_CHECK(_My_data._Large_mode_engaged()); | ||||
|
@@ -5021,7 +5011,7 @@ _NODISCARD _CONSTEXPR20 bool operator==( | |||
|
||||
_EXPORT_STD template <class _Elem, class _Traits, class _Alloc> | ||||
_NODISCARD _CONSTEXPR20 bool operator==( | ||||
const basic_string<_Elem, _Traits, _Alloc>& _Left, _In_z_ const _Elem* const _Right) { | ||||
const basic_string<_Elem, _Traits, _Alloc>& _Left, _In_z_ const _Elem* const _Right) noexcept /* strengthened */ { | ||||
return _Left._Equal(_Right); | ||||
} | ||||
|
||||
|
@@ -5034,12 +5024,13 @@ _NODISCARD constexpr _Get_comparison_category_t<_Traits> operator<=>( | |||
|
||||
_EXPORT_STD template <class _Elem, class _Traits, class _Alloc> | ||||
_NODISCARD constexpr _Get_comparison_category_t<_Traits> operator<=>( | ||||
const basic_string<_Elem, _Traits, _Alloc>& _Left, _In_z_ const _Elem* const _Right) { | ||||
const basic_string<_Elem, _Traits, _Alloc>& _Left, _In_z_ const _Elem* const _Right) noexcept /* strengthened */ { | ||||
return static_cast<_Get_comparison_category_t<_Traits>>(_Left.compare(_Right) <=> 0); | ||||
} | ||||
#else // ^^^ _HAS_CXX20 / !_HAS_CXX20 vvv | ||||
template <class _Elem, class _Traits, class _Alloc> | ||||
_NODISCARD bool operator==(_In_z_ const _Elem* const _Left, const basic_string<_Elem, _Traits, _Alloc>& _Right) { | ||||
_NODISCARD bool operator==(_In_z_ const _Elem* const _Left, const basic_string<_Elem, _Traits, _Alloc>& _Right) noexcept | ||||
/* strengthened */ { | ||||
return _Right._Equal(_Left); | ||||
} | ||||
|
||||
|
@@ -5050,12 +5041,14 @@ _NODISCARD bool operator!=( | |||
} | ||||
|
||||
template <class _Elem, class _Traits, class _Alloc> | ||||
_NODISCARD bool operator!=(_In_z_ const _Elem* const _Left, const basic_string<_Elem, _Traits, _Alloc>& _Right) { | ||||
_NODISCARD bool operator!=(_In_z_ const _Elem* const _Left, const basic_string<_Elem, _Traits, _Alloc>& _Right) noexcept | ||||
/* strengthened */ { | ||||
return !(_Left == _Right); | ||||
} | ||||
|
||||
template <class _Elem, class _Traits, class _Alloc> | ||||
_NODISCARD bool operator!=(const basic_string<_Elem, _Traits, _Alloc>& _Left, _In_z_ const _Elem* const _Right) { | ||||
_NODISCARD bool operator!=(const basic_string<_Elem, _Traits, _Alloc>& _Left, _In_z_ const _Elem* const _Right) noexcept | ||||
/* strengthened */ { | ||||
return !(_Left == _Right); | ||||
} | ||||
|
||||
|
@@ -5066,12 +5059,14 @@ _NODISCARD bool operator<( | |||
} | ||||
|
||||
template <class _Elem, class _Traits, class _Alloc> | ||||
_NODISCARD bool operator<(_In_z_ const _Elem* const _Left, const basic_string<_Elem, _Traits, _Alloc>& _Right) { | ||||
_NODISCARD bool operator<(_In_z_ const _Elem* const _Left, const basic_string<_Elem, _Traits, _Alloc>& _Right) noexcept | ||||
/* strengthened */ { | ||||
return _Right.compare(_Left) > 0; | ||||
} | ||||
|
||||
template <class _Elem, class _Traits, class _Alloc> | ||||
_NODISCARD bool operator<(const basic_string<_Elem, _Traits, _Alloc>& _Left, _In_z_ const _Elem* const _Right) { | ||||
_NODISCARD bool operator<(const basic_string<_Elem, _Traits, _Alloc>& _Left, _In_z_ const _Elem* const _Right) noexcept | ||||
/* strengthened */ { | ||||
return _Left.compare(_Right) < 0; | ||||
} | ||||
|
||||
|
@@ -5082,12 +5077,14 @@ _NODISCARD bool operator>( | |||
} | ||||
|
||||
template <class _Elem, class _Traits, class _Alloc> | ||||
_NODISCARD bool operator>(_In_z_ const _Elem* const _Left, const basic_string<_Elem, _Traits, _Alloc>& _Right) { | ||||
_NODISCARD bool operator>(_In_z_ const _Elem* const _Left, const basic_string<_Elem, _Traits, _Alloc>& _Right) noexcept | ||||
/* strengthened */ { | ||||
return _Right < _Left; | ||||
} | ||||
|
||||
template <class _Elem, class _Traits, class _Alloc> | ||||
_NODISCARD bool operator>(const basic_string<_Elem, _Traits, _Alloc>& _Left, _In_z_ const _Elem* const _Right) { | ||||
_NODISCARD bool operator>(const basic_string<_Elem, _Traits, _Alloc>& _Left, _In_z_ const _Elem* const _Right) noexcept | ||||
/* strengthened */ { | ||||
return _Right < _Left; | ||||
} | ||||
|
||||
|
@@ -5098,12 +5095,14 @@ _NODISCARD bool operator<=( | |||
} | ||||
|
||||
template <class _Elem, class _Traits, class _Alloc> | ||||
_NODISCARD bool operator<=(_In_z_ const _Elem* const _Left, const basic_string<_Elem, _Traits, _Alloc>& _Right) { | ||||
_NODISCARD bool operator<=(_In_z_ const _Elem* const _Left, const basic_string<_Elem, _Traits, _Alloc>& _Right) noexcept | ||||
/* strengthened */ { | ||||
return !(_Right < _Left); | ||||
} | ||||
|
||||
template <class _Elem, class _Traits, class _Alloc> | ||||
_NODISCARD bool operator<=(const basic_string<_Elem, _Traits, _Alloc>& _Left, _In_z_ const _Elem* const _Right) { | ||||
_NODISCARD bool operator<=(const basic_string<_Elem, _Traits, _Alloc>& _Left, _In_z_ const _Elem* const _Right) noexcept | ||||
/* strengthened */ { | ||||
return !(_Right < _Left); | ||||
} | ||||
|
||||
|
@@ -5114,12 +5113,14 @@ _NODISCARD bool operator>=( | |||
} | ||||
|
||||
template <class _Elem, class _Traits, class _Alloc> | ||||
_NODISCARD bool operator>=(_In_z_ const _Elem* const _Left, const basic_string<_Elem, _Traits, _Alloc>& _Right) { | ||||
_NODISCARD bool operator>=(_In_z_ const _Elem* const _Left, const basic_string<_Elem, _Traits, _Alloc>& _Right) noexcept | ||||
/* strengthened */ { | ||||
return !(_Left < _Right); | ||||
} | ||||
|
||||
template <class _Elem, class _Traits, class _Alloc> | ||||
_NODISCARD bool operator>=(const basic_string<_Elem, _Traits, _Alloc>& _Left, _In_z_ const _Elem* const _Right) { | ||||
_NODISCARD bool operator>=(const basic_string<_Elem, _Traits, _Alloc>& _Left, _In_z_ const _Elem* const _Right) noexcept | ||||
/* strengthened */ { | ||||
return !(_Left < _Right); | ||||
} | ||||
#endif // ^^^ !_HAS_CXX20 ^^^ | ||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not practically behavior-changing (won't affect resulting value), but I think it will be clearer to
+1
, as this is the only place that use_Mysize
as input, and_Calculate_growth
accept the first argument as_Requested
, and this will be in line withpush_back
(using_Reallocate_grow_by(1, ...)
which does_Calculate_growth(_My_data._Mysize + 1,...)
.I'm neutral about
+1
change, as it might slightly affect codegen; a comment about why to use_My_data._Mysize
(just to dox1.5
evaluation) might be better.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
+ 1
makes sense here.