]> gcc.gnu.org Git - gcc.git/commitdiff
libstdc++: Simplify basic_string_view::ends_with [PR 101361]
authorJonathan Wakely <jwakely@redhat.com>
Tue, 13 Jul 2021 11:21:27 +0000 (12:21 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Wed, 14 Jul 2021 16:04:29 +0000 (17:04 +0100)
The use of npos triggers a diagnostic as described in PR c++/101361.
This change replaces the use of npos with the exact length, which is
already known. We can further simplify it by inlining the effects of
compare and substr, avoiding the redundant range checks in the latter.

Signed-off-by: Jonathan Wakely <jwakely@redhat.com>
libstdc++-v3/ChangeLog:

PR c++/101361
* include/std/string_view (ends_with): Use traits_type::compare
directly.

(cherry picked from commit 4d3eaeb4f505b0838c673ee28e7dba8687fc8272)

libstdc++-v3/include/std/string_view

index da23f9bc85510740f59125a4c2c2f603eb127e02..7e8ccbdd9e417d1a817f79d552c85d7b300a172d 100644 (file)
@@ -338,8 +338,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       constexpr bool
       ends_with(basic_string_view __x) const noexcept
       {
-       return this->size() >= __x.size()
-           && this->compare(this->size() - __x.size(), npos, __x) == 0;
+       const auto __len = this->size();
+       const auto __xlen = __x.size();
+       return __len >= __xlen
+         && traits_type::compare(end() - __xlen, __x.data(), __xlen) == 0;
       }
 
       constexpr bool
This page took 0.067271 seconds and 5 git commands to generate.