public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: [committed] libstdc++: Simplify basic_string_view::ends_with [PR 101361]
Date: Tue, 13 Jul 2021 15:22:06 +0100	[thread overview]
Message-ID: <YO2hjiVC0vWEisWO@redhat.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 496 bytes --]

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.

Tested powerpc64le-linux. Committed to trunk.


[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 1368 bytes --]

commit 4d3eaeb4f505b0838c673ee28e7dba8687fc8272
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Tue Jul 13 12:21:27 2021

    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.

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

                 reply	other threads:[~2021-07-13 14:22 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YO2hjiVC0vWEisWO@redhat.com \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).