From: Yuao Ma Date: Tue, 2 Dec 2025 15:32:36 +0000 (+0800) Subject: libstdc++: implement P3044R2 - sub-string_view from string (string_view part) X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=475933164f6008bae468fdf709062b76a073727c;p=thirdparty%2Fgcc.git 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. --- diff --git a/libstdc++-v3/include/bits/version.def b/libstdc++-v3/include/bits/version.def index 1fde9eef9d36..d20e08519cac 100644 --- a/libstdc++-v3/include/bits/version.def +++ b/libstdc++-v3/include/bits/version.def @@ -1945,6 +1945,14 @@ ftms = { }; }; +ftms = { + name = string_subview; + values = { + v = 202506; + cxxmin = 26; + }; +}; + ftms = { name = to_underlying; values = { diff --git a/libstdc++-v3/include/bits/version.h b/libstdc++-v3/include/bits/version.h index 2ebc48b234b9..c75368d44c29 100644 --- a/libstdc++-v3/include/bits/version.h +++ b/libstdc++-v3/include/bits/version.h @@ -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 diff --git a/libstdc++-v3/include/std/string_view b/libstdc++-v3/include/std/string_view index 842f6ad89af1..b226544fa6f2 100644 --- a/libstdc++-v3/include/std/string_view +++ b/libstdc++-v3/include/std/string_view @@ -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 #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 diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/operations/subview/char.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/operations/subview/char.cc new file mode 100644 index 000000000000..d6b66e6f732d --- /dev/null +++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/operations/subview/char.cc @@ -0,0 +1,51 @@ +// { dg-do run { target c++26 } } + +#include +#include + +#if __STDC_HOSTED__ +#include +#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; +} diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/operations/subview/wchar_t.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/operations/subview/wchar_t.cc new file mode 100644 index 000000000000..86b50959b5c5 --- /dev/null +++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/operations/subview/wchar_t.cc @@ -0,0 +1,51 @@ +// { dg-do run { target c++26 } } + +#include +#include + +#if __STDC_HOSTED__ +#include +#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; +}