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

Nick Desaulniers via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Wed May 24 12:24:24 PDT 2023


nickdesaulniers updated this revision to Diff 525296.
nickdesaulniers added a comment.

- replace front() with *begin(), length() with size()


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,18 @@
 
 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.empty())
+    return true;
+  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,18 @@
 
 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.empty())
+    return true;
+  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.525296.patch
Type: text/x-patch
Size: 1908 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20230524/21343672/attachment.bin>


More information about the libcxx-commits mailing list