[PATCH] libstdc++: c++2b, implement WG21 P1679R3
Jonathan Wakely
jwakely@redhat.com
Thu Jan 14 17:06:44 GMT 2021
On 13/01/21 01:21 +0000, Paul Fee via Libstdc++ wrote:
>Add contains member function to basic_string_view and basic_string.
>
>The new method is enabled for -std=gnu++20, gnu++2b and c++2b. This allows
>users to access the method as a GNU extension to C++20. The conditional
>test may be reduced to "__cplusplus > 202011L" once GCC has a c++2b switch.
Thanks for the patch.
A few comments below.
>diff --git a/libstdc++-v3/include/bits/basic_string.h
>b/libstdc++-v3/include/bits/basic_string.h
>index e272d332934..a569ecd8c08 100644
>--- a/libstdc++-v3/include/bits/basic_string.h
>+++ b/libstdc++-v3/include/bits/basic_string.h
>@@ -3073,6 +3073,21 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
> { return __sv_type(this->data(), this->size()).ends_with(__x); }
> #endif // C++20
>
>+#if __cplusplus > 202011L || \
>+ (__cplusplus == 202002L && !defined __STRICT_ANSI__)
Please put the line break before the binary operator i.e.
#if __cplusplus > 202011L \
|| (__cplusplus == 202002L && !defined __STRICT_ANSI__)
This has the advantage that the operator is at a predictable place,
so it's easier to see at a glance whether this is an && or ||
condition.
>+ bool
>+ contains(basic_string_view<_CharT, _Traits> __x) const noexcept
>+ { return __sv_type(this->data(), this->size()).contains(__x); }
>+
>+ bool
>+ contains(_CharT __x) const noexcept
>+ { return __sv_type(this->data(), this->size()).contains(__x); }
>+
>+ bool
>+ contains(const _CharT* __x) const noexcept
>+ { return __sv_type(this->data(), this->size()).contains(__x); }
>+#endif // C++23
>+
> // Allow basic_stringbuf::__xfer_bufptrs to call _M_length:
> template<typename, typename, typename> friend class basic_stringbuf;
> };
>@@ -5998,6 +6013,21 @@ _GLIBCXX_END_NAMESPACE_CXX11
> { return __sv_type(this->data(), this->size()).ends_with(__x); }
> #endif // C++20
>
>+#if __cplusplus > 202011L || \
>+ (__cplusplus == 202002L && !defined __STRICT_ANSI__)
>+ bool
>+ contains(basic_string_view<_CharT, _Traits> __x) const noexcept
>+ { return __sv_type(this->data(), this->size()).contains(__x); }
>+
>+ bool
>+ contains(_CharT __x) const noexcept
>+ { return __sv_type(this->data(), this->size()).contains(__x); }
>+
>+ bool
>+ contains(const _CharT* __x) const noexcept
>+ { return __sv_type(this->data(), this->size()).contains(__x); }
>+#endif // C++23
>+
> # ifdef _GLIBCXX_TM_TS_INTERNAL
> friend void
> ::_txnal_cow_string_C1_for_exceptions(void* that, const char* s,
>diff --git a/libstdc++-v3/include/std/string_view
>b/libstdc++-v3/include/std/string_view
>index e33e1bc4b79..2f47ef6ed12 100644
>--- a/libstdc++-v3/include/std/string_view
>+++ b/libstdc++-v3/include/std/string_view
>@@ -352,6 +352,22 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> { return this->ends_with(basic_string_view(__x)); }
> #endif // C++20
>
>+#if __cplusplus > 202011L || \
>+ (__cplusplus == 202002L && !defined __STRICT_ANSI__)
>+#define __cpp_lib_string_contains 202011L
This macro should also be defined in <version>, and should depend on
the same conditions.
There should also be tests that the macro is defined by <string_view>
and by <version>.
You can add this to the top of any one of the new tests for
string_view::contains:
#ifndef __cpp_lib_string_contains
# error "Feature-test macro for contains missing in <string_view>"
#elif __cpp_lib_string_contains != 202011L
# error "Feature-test macro for contains has wrong value in <string_view>"
#endif
And then add a new version.cc test adjacent to that file, which
includes <version> (and nothing else) and tests the same conditions:
#include <version>
#ifndef __cpp_lib_string_contains
# error "Feature-test macro for contains missing in <version>"
#elif __cpp_lib_string_contains != 202011L
# error "Feature-test macro for contains has wrong value in <version>"
#endif
>+ constexpr bool
>+ contains(basic_string_view __x) const noexcept
>+ { return this->find(__x) != npos; }
>+
>+ constexpr bool
>+ contains(_CharT __x) const noexcept
>+ { return this->find(__x) != npos; }
>+
>+ constexpr bool
>+ contains(const _CharT* __x) const noexcept
>+ { return this->find(__x) != npos; }
>+#endif // C++23
>+
> // [string.view.find], searching
>
> constexpr size_type
>diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/operations/contains/char/1.cc
>b/libstdc++-v3/testsuite/21_strings/basic_string/operations/contains/char/1.cc
>new file mode 100644
>index 00000000000..5d81dcee0ad
>--- /dev/null
>+++ b/libstdc++-v3/testsuite/21_strings/basic_string/operations/contains/char/1.cc
>@@ -0,0 +1,65 @@
>+// { dg-options "-std=gnu++2b" }
>+// { dg-do run { target c++2b } }
>+
>+// Copyright (C) 2018-2020 Free Software Foundation, Inc.
These dates should be 2021 only.
>diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/operations/contains/wchar_t/1.cc
>b/libstdc++-v3/testsuite/21_strings/basic_string/operations/contains/wchar_t/1.cc
>new file mode 100644
>index 00000000000..21196add4dc
>--- /dev/null
>+++ b/libstdc++-v3/testsuite/21_strings/basic_string/operations/contains/wchar_t/1.cc
>@@ -0,0 +1,65 @@
>+// { dg-options "-std=gnu++2b" }
>+// { dg-do run { target c++2b } }
>+
>+// Copyright (C) 2018-2020 Free Software Foundation, Inc.
>+//
>+// This file is part of the GNU ISO C++ Library. This library is free
>+// software; you can redistribute it and/or modify it under the
>+// terms of the GNU General Public License as published by the
>+// Free Software Foundation; either version 3, or (at your option)
>+// any later version.
>+
>+// This library is distributed in the hope that it will be useful,
>+// but WITHOUT ANY WARRANTY; without even the implied warranty of
>+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
>+// GNU General Public License for more details.
>+
>+// You should have received a copy of the GNU General Public License along
>+// with this library; see the file COPYING3. If not see
>+// <http://www.gnu.org/licenses/>.
>+
>+// basic_string begins_with
Comment not adjusted after copying the begins_with test.
More information about the Libstdc++
mailing list