diff options
author | Jonathan Wakely <jwakely@redhat.com> | 2021-07-13 12:21:27 +0100 |
---|---|---|
committer | Jonathan Wakely <jwakely@redhat.com> | 2021-07-13 15:21:26 +0100 |
commit | 4d3eaeb4f505b0838c673ee28e7dba8687fc8272 (patch) | |
tree | fb472dc6f1dd115546653ea64eb1e7663840a317 | |
parent | f75560398af6f1f696c820016f437af4e8b4265c (diff) | |
download | gcc-4d3eaeb4f505b0838c673ee28e7dba8687fc8272.zip gcc-4d3eaeb4f505b0838c673ee28e7dba8687fc8272.tar.gz gcc-4d3eaeb4f505b0838c673ee28e7dba8687fc8272.tar.bz2 |
libstdc++: Simplify basic_string_view::ends_with [PR 101361]
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.
-rw-r--r-- | libstdc++-v3/include/std/string_view | 6 |
1 files changed, 4 insertions, 2 deletions
diff --git a/libstdc++-v3/include/std/string_view b/libstdc++-v3/include/std/string_view index cfdcf28..4ea72c6 100644 --- a/libstdc++-v3/include/std/string_view +++ b/libstdc++-v3/include/std/string_view @@ -361,8 +361,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 |