[PATCH] D151364: [Demangle] avoid more std::string_view::substr

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


nickdesaulniers created this revision.
Herald added a project: All.
nickdesaulniers requested review of this revision.
Herald added projects: libc++abi, LLVM.
Herald added subscribers: llvm-commits, libcxx-commits.
Herald added a reviewer: libc++abi.

In D148959 <https://reviews.llvm.org/D148959>, I removed usage of std::string_view::substr because it may
throw, and libcxxabi cannot use such code.  I missed one instance in
llvm::starts_with.  That is blocking copying the code back upstream in
D148566 <https://reviews.llvm.org/D148566>.

Mark these helpers noexcept (as they are in C++20) as well, to remind
future travelers.

Make these changes upstream, and copy them back downstream using
libcxxabi/src/demangle/cp-to-llvm.sh.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D151364

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: D151364.525295.patch
Type: text/x-patch
Size: 1908 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230524/f6bf8ca1/attachment.bin>


More information about the llvm-commits mailing list