From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 9CA223969813; Tue, 13 Jul 2021 14:21:56 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9CA223969813 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r12-2285] libstdc++: Simplify basic_string_view::ends_with [PR 101361] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: f75560398af6f1f696c820016f437af4e8b4265c X-Git-Newrev: 4d3eaeb4f505b0838c673ee28e7dba8687fc8272 Message-Id: <20210713142156.9CA223969813@sourceware.org> Date: Tue, 13 Jul 2021 14:21:56 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 13 Jul 2021 14:21:56 -0000 https://gcc.gnu.org/g:4d3eaeb4f505b0838c673ee28e7dba8687fc8272 commit r12-2285-g4d3eaeb4f505b0838c673ee28e7dba8687fc8272 Author: Jonathan Wakely Date: Tue Jul 13 12:21:27 2021 +0100 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 libstdc++-v3/ChangeLog: PR c++/101361 * include/std/string_view (ends_with): Use traits_type::compare directly. Diff: --- libstdc++-v3/include/std/string_view | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/libstdc++-v3/include/std/string_view b/libstdc++-v3/include/std/string_view index cfdcf28f026..4ea72c6cef2 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