[libcxx-commits] [PATCH] D151260: [Demangle] avoid more std::string_view::substr

Nick Desaulniers via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Thu May 25 14:35:29 PDT 2023


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGee740b714ba0: [Demangle] avoid more std::string_view::substr (authored by nickdesaulniers).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D151260/new/

https://reviews.llvm.org/D151260

Files:
  libcxxabi/src/demangle/StringViewExtras.h
  llvm/include/llvm/Demangle/StringViewExtras.h


Index: llvm/include/llvm/Demangle/StringViewExtras.h
===================================================================
--- llvm/include/llvm/Demangle/StringViewExtras.h
+++ llvm/include/llvm/Demangle/StringViewExtras.h
@@ -21,12 +21,16 @@
 
 DEMANGLE_NAMESPACE_BEGIN
 
-inline bool starts_with(std::string_view self, char C) {
-  return !self.empty() && self.front() == C;
+inline bool starts_with(std::string_view self, char C) noexcept {
+  return !self.empty() && *self.begin() == C;
 }
 
-inline bool starts_with(std::string_view haystack, std::string_view needle) {
-  return haystack.substr(0, needle.size()) == needle;
+inline bool starts_with(std::string_view haystack,
+                        std::string_view needle) noexcept {
+  if (needle.size() > haystack.size())
+    return false;
+  haystack.remove_suffix(haystack.size() - needle.size());
+  return haystack == needle;
 }
 
 DEMANGLE_NAMESPACE_END
Index: libcxxabi/src/demangle/StringViewExtras.h
===================================================================
--- libcxxabi/src/demangle/StringViewExtras.h
+++ libcxxabi/src/demangle/StringViewExtras.h
@@ -21,12 +21,16 @@
 
 DEMANGLE_NAMESPACE_BEGIN
 
-inline bool starts_with(std::string_view self, char C) {
-  return !self.empty() && self.front() == C;
+inline bool starts_with(std::string_view self, char C) noexcept {
+  return !self.empty() && *self.begin() == C;
 }
 
-inline bool starts_with(std::string_view haystack, std::string_view needle) {
-  return haystack.substr(0, needle.size()) == needle;
+inline bool starts_with(std::string_view haystack,
+                        std::string_view needle) noexcept {
+  if (needle.size() > haystack.size())
+    return false;
+  haystack.remove_suffix(haystack.size() - needle.size());
+  return haystack == needle;
 }
 
 DEMANGLE_NAMESPACE_END


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D151260.525816.patch
Type: text/x-patch
Size: 1826 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20230525/680aaa93/attachment.bin>


More information about the libcxx-commits mailing list