[libcxx-commits] [libcxx] [libc++] Simplify the implementation of string::{append, assign, assign_range} (PR #162254)
Louis Dionne via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Oct 8 08:27:55 PDT 2025
================
@@ -1413,24 +1413,16 @@ public:
_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(const value_type* _LIBCPP_DIAGNOSE_NULLPTR __s);
_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(size_type __n, value_type __c);
- template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> = 0>
- _LIBCPP_HIDE_FROM_ABI _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
- append(_InputIterator __first, _InputIterator __last) {
- const basic_string __temp(__first, __last, __alloc_);
- append(__temp.data(), __temp.size());
- return *this;
- }
-
- template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>
+ template <class _InputIterator>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
- append(_ForwardIterator __first, _ForwardIterator __last) {
+ append(_InputIterator __first, _InputIterator __last) {
size_type __sz = size();
size_type __cap = capacity();
size_type __n = static_cast<size_type>(std::distance(__first, __last));
if (__n == 0)
return *this;
- if (__string_is_trivial_iterator_v<_ForwardIterator> && !__addr_in_range(*__first)) {
+ if (__string_is_trivial_iterator_v<_InputIterator> && !__addr_in_range(*__first)) {
if (__cap - __sz < __n)
__grow_by_without_replace(__cap, __sz + __n - __cap, __sz, __sz, 0);
__annotate_increase(__n);
----------------
ldionne wrote:
I guess this could be something like
```
string __tmp;
__tmp.reserve(__sz + __n);
__tmp.assign(begin(), end());
__tmp.insert(__tmp.end(), __first, __last);
swap(*this, __tmp);
```
And then we don't have to care about `__addr_in_range(*__first)` anymore. That's basically the algorithm that we do in `vector` as well.
This can be done in a follow-up patch.
https://github.com/llvm/llvm-project/pull/162254
More information about the libcxx-commits
mailing list