libstdc++: implement P3044R2 - sub-string_view from string (string_view part)

libstdc++-v3/ChangeLog:

	* include/bits/version.def: Add string_subview FTM.
	* include/bits/version.h: Regenerate.
	* include/std/string_view: Add subview.
	* testsuite/21_strings/basic_string_view/operations/subview/char.cc: New test.
	* testsuite/21_strings/basic_string_view/operations/subview/wchar_t.cc: New test.
This commit is contained in:
Yuao Ma 2025-12-02 23:32:36 +08:00
parent 45f3501bda
commit 475933164f
5 changed files with 129 additions and 1 deletions

View File

@ -1945,6 +1945,14 @@ ftms = {
};
};
ftms = {
name = string_subview;
values = {
v = 202506;
cxxmin = 26;
};
};
ftms = {
name = to_underlying;
values = {

View File

@ -2170,6 +2170,16 @@
#endif /* !defined(__cpp_lib_string_resize_and_overwrite) */
#undef __glibcxx_want_string_resize_and_overwrite
#if !defined(__cpp_lib_string_subview)
# if (__cplusplus > 202302L)
# define __glibcxx_string_subview 202506L
# if defined(__glibcxx_want_all) || defined(__glibcxx_want_string_subview)
# define __cpp_lib_string_subview 202506L
# endif
# endif
#endif /* !defined(__cpp_lib_string_subview) */
#undef __glibcxx_want_string_subview
#if !defined(__cpp_lib_to_underlying)
# if (__cplusplus >= 202100L)
# define __glibcxx_to_underlying 202102L

View File

@ -40,9 +40,10 @@
#define __glibcxx_want_constexpr_char_traits
#define __glibcxx_want_constexpr_string_view
#define __glibcxx_want_freestanding_string_view
#define __glibcxx_want_string_view
#define __glibcxx_want_starts_ends_with
#define __glibcxx_want_string_contains
#define __glibcxx_want_string_subview
#define __glibcxx_want_string_view
#include <bits/version.h>
#if __cplusplus >= 201703L
@ -342,6 +343,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
return basic_string_view{_M_str + __pos, __rlen};
}
#ifdef __glibcxx_string_subview // >= C++26
[[nodiscard]]
constexpr basic_string_view
subview(size_type __pos = 0, size_type __n = npos) const
{ return substr(__pos, __n); }
#endif
[[nodiscard]]
constexpr int
compare(basic_string_view __str) const noexcept

View File

@ -0,0 +1,51 @@
// { dg-do run { target c++26 } }
#include <string_view>
#include <testsuite_hooks.h>
#if __STDC_HOSTED__
#include <stdexcept>
#endif
void test01() {
typedef std::string_view::size_type csize_type;
typedef std::string_view::const_reference cref;
typedef std::string_view::reference ref;
csize_type csz01;
const char str_lit01[] = "rockaway, pacifica";
const std::string_view str01(str_lit01);
std::string_view str02;
csz01 = str01.size();
str02 = str01.subview(0, 1);
VERIFY(str02 == "r");
str02 = str01.subview(10);
VERIFY(str02 == "pacifica");
#if __STDC_HOSTED__
try {
str02 = str01.subview(csz01 + 1);
VERIFY(false);
} catch (std::out_of_range &fail) {
VERIFY(true);
} catch (...) {
VERIFY(false);
}
try {
str02 = str01.subview(csz01);
VERIFY(str02.size() == 0);
VERIFY(str02.begin() == str01.end());
VERIFY(true);
} catch (...) {
VERIFY(false);
}
#endif // HOSTED
}
int main() {
test01();
return 0;
}

View File

@ -0,0 +1,51 @@
// { dg-do run { target c++26 } }
#include <string_view>
#include <testsuite_hooks.h>
#if __STDC_HOSTED__
#include <stdexcept>
#endif
void test01() {
typedef std::wstring_view::size_type csize_type;
typedef std::wstring_view::const_reference cref;
typedef std::wstring_view::reference ref;
csize_type csz01;
const wchar_t str_lit01[] = L"rockaway, pacifica";
const std::wstring_view str01(str_lit01);
std::wstring_view str02;
csz01 = str01.size();
str02 = str01.subview(0, 1);
VERIFY(str02 == L"r");
str02 = str01.subview(10);
VERIFY(str02 == L"pacifica");
#if __STDC_HOSTED__
try {
str02 = str01.subview(csz01 + 1);
VERIFY(false);
} catch (std::out_of_range &fail) {
VERIFY(true);
} catch (...) {
VERIFY(false);
}
try {
str02 = str01.subview(csz01);
VERIFY(str02.size() == 0);
VERIFY(str02.begin() == str01.end());
VERIFY(true);
} catch (...) {
VERIFY(false);
}
#endif // HOSTED
}
int main() {
test01();
return 0;
}