[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 14:42:53 PDT 2023


nickdesaulniers updated this revision to Diff 525341.
nickdesaulniers marked an inline comment as done.
nickdesaulniers added a comment.

- remove special case for empty needle; it's already handled


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.525341.patch
Type: text/x-patch
Size: 1826 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20230524/6eae5d3a/attachment.bin>


More information about the libcxx-commits mailing list