Skip to content

Commit 804b46b

Browse files
H-G-HristovZingamphilnik777
authored
[libc++][string] P3044R2: sub-string_view from string (#147095)
Implements [P3044R2](https://wg21.link/P3044R2) Note: `substr.pass.cpp` is refactored to accommodate the test of `basic_string_view`'s `subview` which is an alias of `substr` without changing the test cases. Closes #148140 # References - cplusplus/draft#7975 - https://wg21.link/string.substr - https://wg21.link/string.view.ops --------- Co-authored-by: Hristo Hristov <[email protected]> Co-authored-by: Nikolas Klauser <[email protected]>
1 parent add906f commit 804b46b

File tree

14 files changed

+361
-1
lines changed

14 files changed

+361
-1
lines changed

libcxx/docs/FeatureTestMacroTable.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,8 @@ Status
506506
---------------------------------------------------------- -----------------
507507
``__cpp_lib_sstream_from_string_view`` ``202306L``
508508
---------------------------------------------------------- -----------------
509+
``__cpp_lib_string_subview`` ``202506L``
510+
---------------------------------------------------------- -----------------
509511
``__cpp_lib_string_view`` ``202403L``
510512
---------------------------------------------------------- -----------------
511513
``__cpp_lib_submdspan`` *unimplemented*

libcxx/docs/ReleaseNotes/22.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Implemented Papers
4040

4141
- P2321R2: ``zip`` (`Github <https://llvm.org/PR105169>`__) (The paper is partially implemented. ``zip_transform_view``
4242
is implemented in this release)
43+
- P3044R2: sub-``string_view`` from ``string`` (`Github <https://llvm.org/PR148140>`__)
4344
- P3168R2: Give ``std::optional`` Range Support (`Github <https://llvm.org/PR105430>`__)
4445

4546
Improvements and New Features

libcxx/docs/Status/Cxx2cPapers.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@
129129
"`P3179R9 <https://wg21.link/P3179R9>`__","Parallel Range Algorithms","2025-06 (Sofia)","","","`#148137 <https://github.com/llvm/llvm-project/issues/148137>`__",""
130130
"`P3709R2 <https://wg21.link/P3709R2>`__","Reconsider parallel ``ranges::rotate_copy`` and ``ranges::reverse_copy``","2025-06 (Sofia)","","","`#148138 <https://github.com/llvm/llvm-project/issues/148138>`__",""
131131
"`P3641R0 <https://wg21.link/P3641R0>`__","Rename ``std::observable`` to ``std::observable_checkpoint``, and add a feature-test macro","2025-06 (Sofia)","","","`#148139 <https://github.com/llvm/llvm-project/issues/148139>`__",""
132-
"`P3044R2 <https://wg21.link/P3044R2>`__","sub-``string_view`` from ``string``","2025-06 (Sofia)","","","`#148140 <https://github.com/llvm/llvm-project/issues/148140>`__",""
132+
"`P3044R2 <https://wg21.link/P3044R2>`__","sub-``string_view`` from ``string``","2025-06 (Sofia)","|Complete|","22","`#148140 <https://github.com/llvm/llvm-project/issues/148140>`__",""
133133
"`P2876R3 <https://wg21.link/P2876R3>`__","Proposal to extend ``std::simd`` with more constructors and accessors","2025-06 (Sofia)","","","`#148143 <https://github.com/llvm/llvm-project/issues/148143>`__",""
134134
"`P3480R6 <https://wg21.link/P3480R6>`__","``std::simd`` is a range","2025-06 (Sofia)","","","`#148144 <https://github.com/llvm/llvm-project/issues/148144>`__",""
135135
"`P2664R11 <https://wg21.link/P2664R11>`__","Extend ``std::simd`` with permutation API","2025-06 (Sofia)","","","`#148145 <https://github.com/llvm/llvm-project/issues/148145>`__",""

libcxx/include/string

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,8 @@ public:
280280
basic_string substr(size_type pos = 0, size_type n = npos) const; // constexpr in C++20, removed in C++23
281281
basic_string substr(size_type pos = 0, size_type n = npos) const&; // since C++23
282282
constexpr basic_string substr(size_type pos = 0, size_type n = npos) &&; // since C++23
283+
constexpr basic_string_view<charT, traits> subview(size_type pos = 0,
284+
size_type n = npos) const; // since C++26
283285
void swap(basic_string& str)
284286
noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
285287
allocator_traits<allocator_type>::is_always_equal::value); // C++17, constexpr since C++20
@@ -1758,6 +1760,11 @@ public:
17581760
return basic_string(std::move(*this), __pos, __n);
17591761
}
17601762
# endif
1763+
# if _LIBCPP_STD_VER >= 26
1764+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr __self_view subview(size_type __pos = 0, size_type __n = npos) const {
1765+
return __self_view(*this).subview(__pos, __n);
1766+
}
1767+
# endif
17611768

17621769
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void swap(basic_string& __str)
17631770
# if _LIBCPP_STD_VER >= 14

libcxx/include/string_view

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ namespace std {
130130
size_type copy(charT* s, size_type n, size_type pos = 0) const; // constexpr in C++20
131131
132132
constexpr basic_string_view substr(size_type pos = 0, size_type n = npos) const;
133+
constexpr basic_string_view subview(size_type pos = 0,
134+
size_type n = npos) const; // freestanding-deleted, since C++26
133135
constexpr int compare(basic_string_view s) const noexcept;
134136
constexpr int compare(size_type pos1, size_type n1, basic_string_view s) const;
135137
constexpr int compare(size_type pos1, size_type n1,
@@ -465,6 +467,13 @@ public:
465467
: basic_string_view(__assume_valid(), data() + __pos, std::min(__n, size() - __pos));
466468
}
467469

470+
# if _LIBCPP_STD_VER >= 26
471+
[[nodiscard]]
472+
_LIBCPP_HIDE_FROM_ABI constexpr basic_string_view subview(size_type __pos = 0, size_type __n = npos) const {
473+
return substr(__pos, __n);
474+
}
475+
# endif
476+
468477
_LIBCPP_CONSTEXPR_SINCE_CXX14 int compare(basic_string_view __sv) const _NOEXCEPT {
469478
size_type __rlen = std::min(size(), __sv.size());
470479
int __retval = _Traits::compare(data(), __sv.data(), __rlen);

libcxx/include/version

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ __cpp_lib_starts_ends_with 201711L <string> <string
245245
__cpp_lib_stdatomic_h 202011L <stdatomic.h>
246246
__cpp_lib_string_contains 202011L <string> <string_view>
247247
__cpp_lib_string_resize_and_overwrite 202110L <string>
248+
__cpp_lib_string_subview 202506L <string> <string_view>
248249
__cpp_lib_string_udls 201304L <string>
249250
__cpp_lib_string_view 202403L <string> <string_view>
250251
201803L // C++20
@@ -599,6 +600,7 @@ __cpp_lib_void_t 201411L <type_traits>
599600
# define __cpp_lib_span_at 202311L
600601
# define __cpp_lib_span_initializer_list 202311L
601602
# define __cpp_lib_sstream_from_string_view 202306L
603+
# define __cpp_lib_string_subview 202506L
602604
# undef __cpp_lib_string_view
603605
# define __cpp_lib_string_view 202403L
604606
// # define __cpp_lib_submdspan 202306L

libcxx/test/libcxx/diagnostics/string.nodiscard.verify.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@
1212

1313
#include <string>
1414

15+
#include "test_macros.h"
16+
1517
void test() {
1618
std::string string;
1719
string.empty(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
20+
#if TEST_STD_VER >= 26
21+
string.subview(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
22+
#endif
1823
}

libcxx/test/libcxx/diagnostics/string_view.nodiscard.verify.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@
1212

1313
#include <string_view>
1414

15+
#include "test_macros.h"
16+
1517
void test() {
1618
std::string_view string_view;
1719
string_view.empty(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
20+
#if TEST_STD_VER >= 26
21+
string_view.subview(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
22+
#endif
1823
}

libcxx/test/std/language.support/support.limits/support.limits.general/string.version.compile.pass.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@
6060
# error "__cpp_lib_string_resize_and_overwrite should not be defined before c++23"
6161
# endif
6262

63+
# ifdef __cpp_lib_string_subview
64+
# error "__cpp_lib_string_subview should not be defined before c++26"
65+
# endif
66+
6367
# ifdef __cpp_lib_string_udls
6468
# error "__cpp_lib_string_udls should not be defined before c++14"
6569
# endif
@@ -114,6 +118,10 @@
114118
# error "__cpp_lib_string_resize_and_overwrite should not be defined before c++23"
115119
# endif
116120

121+
# ifdef __cpp_lib_string_subview
122+
# error "__cpp_lib_string_subview should not be defined before c++26"
123+
# endif
124+
117125
# ifndef __cpp_lib_string_udls
118126
# error "__cpp_lib_string_udls should be defined in c++14"
119127
# endif
@@ -177,6 +185,10 @@
177185
# error "__cpp_lib_string_resize_and_overwrite should not be defined before c++23"
178186
# endif
179187

188+
# ifdef __cpp_lib_string_subview
189+
# error "__cpp_lib_string_subview should not be defined before c++26"
190+
# endif
191+
180192
# ifndef __cpp_lib_string_udls
181193
# error "__cpp_lib_string_udls should be defined in c++17"
182194
# endif
@@ -261,6 +273,10 @@
261273
# error "__cpp_lib_string_resize_and_overwrite should not be defined before c++23"
262274
# endif
263275

276+
# ifdef __cpp_lib_string_subview
277+
# error "__cpp_lib_string_subview should not be defined before c++26"
278+
# endif
279+
264280
# ifndef __cpp_lib_string_udls
265281
# error "__cpp_lib_string_udls should be defined in c++20"
266282
# endif
@@ -354,6 +370,10 @@
354370
# error "__cpp_lib_string_resize_and_overwrite should have the value 202110L in c++23"
355371
# endif
356372

373+
# ifdef __cpp_lib_string_subview
374+
# error "__cpp_lib_string_subview should not be defined before c++26"
375+
# endif
376+
357377
# ifndef __cpp_lib_string_udls
358378
# error "__cpp_lib_string_udls should be defined in c++23"
359379
# endif
@@ -456,6 +476,13 @@
456476
# error "__cpp_lib_string_resize_and_overwrite should have the value 202110L in c++26"
457477
# endif
458478

479+
# ifndef __cpp_lib_string_subview
480+
# error "__cpp_lib_string_subview should be defined in c++26"
481+
# endif
482+
# if __cpp_lib_string_subview != 202506L
483+
# error "__cpp_lib_string_subview should have the value 202506L in c++26"
484+
# endif
485+
459486
# ifndef __cpp_lib_string_udls
460487
# error "__cpp_lib_string_udls should be defined in c++26"
461488
# endif

libcxx/test/std/language.support/support.limits/support.limits.general/string_view.version.compile.pass.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040
# error "__cpp_lib_string_contains should not be defined before c++23"
4141
# endif
4242

43+
# ifdef __cpp_lib_string_subview
44+
# error "__cpp_lib_string_subview should not be defined before c++26"
45+
# endif
46+
4347
# ifdef __cpp_lib_string_view
4448
# error "__cpp_lib_string_view should not be defined before c++17"
4549
# endif
@@ -66,6 +70,10 @@
6670
# error "__cpp_lib_string_contains should not be defined before c++23"
6771
# endif
6872

73+
# ifdef __cpp_lib_string_subview
74+
# error "__cpp_lib_string_subview should not be defined before c++26"
75+
# endif
76+
6977
# ifdef __cpp_lib_string_view
7078
# error "__cpp_lib_string_view should not be defined before c++17"
7179
# endif
@@ -92,6 +100,10 @@
92100
# error "__cpp_lib_string_contains should not be defined before c++23"
93101
# endif
94102

103+
# ifdef __cpp_lib_string_subview
104+
# error "__cpp_lib_string_subview should not be defined before c++26"
105+
# endif
106+
95107
# ifndef __cpp_lib_string_view
96108
# error "__cpp_lib_string_view should be defined in c++17"
97109
# endif
@@ -136,6 +148,10 @@
136148
# error "__cpp_lib_string_contains should not be defined before c++23"
137149
# endif
138150

151+
# ifdef __cpp_lib_string_subview
152+
# error "__cpp_lib_string_subview should not be defined before c++26"
153+
# endif
154+
139155
# ifndef __cpp_lib_string_view
140156
# error "__cpp_lib_string_view should be defined in c++20"
141157
# endif
@@ -183,6 +199,10 @@
183199
# error "__cpp_lib_string_contains should have the value 202011L in c++23"
184200
# endif
185201

202+
# ifdef __cpp_lib_string_subview
203+
# error "__cpp_lib_string_subview should not be defined before c++26"
204+
# endif
205+
186206
# ifndef __cpp_lib_string_view
187207
# error "__cpp_lib_string_view should be defined in c++23"
188208
# endif
@@ -239,6 +259,13 @@
239259
# error "__cpp_lib_string_contains should have the value 202011L in c++26"
240260
# endif
241261

262+
# ifndef __cpp_lib_string_subview
263+
# error "__cpp_lib_string_subview should be defined in c++26"
264+
# endif
265+
# if __cpp_lib_string_subview != 202506L
266+
# error "__cpp_lib_string_subview should have the value 202506L in c++26"
267+
# endif
268+
242269
# ifndef __cpp_lib_string_view
243270
# error "__cpp_lib_string_view should be defined in c++26"
244271
# endif

0 commit comments

Comments
 (0)