[libcxx-commits] [libcxx] [libc++] Simplify the implementation of string::{append, assign, assign_range} (PR #162254)
Nikolas Klauser via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Oct 7 06:47:00 PDT 2025
https://github.com/philnik777 updated https://github.com/llvm/llvm-project/pull/162254
>From f0148e8c1932b839d7e0b0e81b7403c8392c7a12 Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Tue, 7 Oct 2025 11:38:00 +0200
Subject: [PATCH] [libc++] Simplify the implementation of
string::{append,assign,assign_range}
---
libcxx/include/string | 39 ++++++++++-----------------------------
1 file changed, 10 insertions(+), 29 deletions(-)
diff --git a/libcxx/include/string b/libcxx/include/string
index 363f27a51648c..adcb564fa106f 100644
--- a/libcxx/include/string
+++ b/libcxx/include/string
@@ -639,6 +639,7 @@ basic_string<char32_t> operator""s( const char32_t *str, size_t len );
# include <__type_traits/is_trivially_copyable.h>
# include <__type_traits/is_trivially_relocatable.h>
# include <__type_traits/remove_cvref.h>
+# include <__utility/declval.h>
# include <__utility/default_three_way_comparator.h>
# include <__utility/forward.h>
# include <__utility/is_pointer_in_range.h>
@@ -699,13 +700,9 @@ __concatenate_strings(const _Allocator& __alloc,
__type_identity_t<basic_string_view<_CharT, _Traits> > __str2);
template <class _Iter>
-inline const bool __string_is_trivial_iterator_v = false;
-
-template <class _Tp>
-inline const bool __string_is_trivial_iterator_v<_Tp*> = is_arithmetic<_Tp>::value;
-
-template <class _Iter>
-inline const bool __string_is_trivial_iterator_v<__wrap_iter<_Iter> > = __string_is_trivial_iterator_v<_Iter>;
+inline const bool __string_is_trivial_iterator_v =
+ __libcpp_is_contiguous_iterator<_Iter>::value &&
+ is_arithmetic<__remove_cvref_t<decltype(*std::declval<_Iter&>())> >::value;
template <class _CharT, class _Traits, class _Tp>
inline const bool __can_be_converted_to_string_view_v =
@@ -1413,24 +1410,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);
@@ -1540,17 +1529,10 @@ public:
_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(const value_type* _LIBCPP_DIAGNOSE_NULLPTR __s);
_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(size_type __n, value_type __c);
- template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> = 0>
+ template <class _InputIterator>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
assign(_InputIterator __first, _InputIterator __last) {
- __assign_with_sentinel(__first, __last);
- return *this;
- }
-
- template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>
- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
- assign(_ForwardIterator __first, _ForwardIterator __last) {
- if (__string_is_trivial_iterator_v<_ForwardIterator>) {
+ if _LIBCPP_CONSTEXPR (__string_is_trivial_iterator_v<_InputIterator>) {
size_type __n = static_cast<size_type>(std::distance(__first, __last));
__assign_trivial(__first, __last, __n);
} else {
@@ -1563,8 +1545,7 @@ public:
# if _LIBCPP_STD_VER >= 23
template <_ContainerCompatibleRange<_CharT> _Range>
_LIBCPP_HIDE_FROM_ABI constexpr basic_string& assign_range(_Range&& __range) {
- if constexpr (__string_is_trivial_iterator_v<ranges::iterator_t<_Range>> &&
- (ranges::forward_range<_Range> || ranges::sized_range<_Range>)) {
+ if constexpr (__string_is_trivial_iterator_v<ranges::iterator_t<_Range>>) {
size_type __n = static_cast<size_type>(ranges::distance(__range));
__assign_trivial(ranges::begin(__range), ranges::end(__range), __n);
More information about the libcxx-commits
mailing list