]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: implement P3044R2 - sub-string_view from string (string_view part)
authorYuao Ma <c8ef@outlook.com>
Tue, 2 Dec 2025 15:32:36 +0000 (23:32 +0800)
committerYuao Ma <c8ef@outlook.com>
Wed, 3 Dec 2025 15:55:06 +0000 (23:55 +0800)
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.

libstdc++-v3/include/bits/version.def
libstdc++-v3/include/bits/version.h
libstdc++-v3/include/std/string_view
libstdc++-v3/testsuite/21_strings/basic_string_view/operations/subview/char.cc [new file with mode: 0644]
libstdc++-v3/testsuite/21_strings/basic_string_view/operations/subview/wchar_t.cc [new file with mode: 0644]

index 1fde9eef9d3629f3df6f6cce6086c5e7656d0d56..d20e08519cac5926461db631fd9c7bb27b7fffb1 100644 (file)
@@ -1945,6 +1945,14 @@ ftms = {
   };
 };
 
+ftms = {
+  name = string_subview;
+  values = {
+    v = 202506;
+    cxxmin = 26;
+  };
+};
+
 ftms = {
   name = to_underlying;
   values = {
index 2ebc48b234b9ba480b3e6d310b0d7137f40380a4..c75368d44c2934431e2f429a80a417a4bf3b2647 100644 (file)
 #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
index 842f6ad89af14807ed4564461de3699b5d39a9a7..b226544fa6f2bff5d4c2575dad2150cc92127975 100644 (file)
 #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
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 (file)
index 0000000..d6b66e6
--- /dev/null
@@ -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;
+}
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 (file)
index 0000000..86b5095
--- /dev/null
@@ -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;
+}