[llvm] ee740b7 - [Demangle] avoid more std::string_view::substr
    Nick Desaulniers via llvm-commits 
    llvm-commits at lists.llvm.org
       
    Thu May 25 14:35:28 PDT 2023
    
    
  
Author: Nick Desaulniers
Date: 2023-05-25T14:35:13-07:00
New Revision: ee740b714ba0de41f48a4fb4717fa9b9a22fedf2
URL: https://github.com/llvm/llvm-project/commit/ee740b714ba0de41f48a4fb4717fa9b9a22fedf2
DIFF: https://github.com/llvm/llvm-project/commit/ee740b714ba0de41f48a4fb4717fa9b9a22fedf2.diff
LOG: [Demangle] avoid more std::string_view::substr
In 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.
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.
Reviewed By: #libc_abi, MaskRay, ldionne
Differential Revision: https://reviews.llvm.org/D151260
Added: 
    
Modified: 
    libcxxabi/src/demangle/StringViewExtras.h
    llvm/include/llvm/Demangle/StringViewExtras.h
Removed: 
    
################################################################################
diff  --git a/libcxxabi/src/demangle/StringViewExtras.h b/libcxxabi/src/demangle/StringViewExtras.h
index d430e67ea3cb..0ad1f8c8a217 100644
--- a/libcxxabi/src/demangle/StringViewExtras.h
+++ b/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
diff  --git a/llvm/include/llvm/Demangle/StringViewExtras.h b/llvm/include/llvm/Demangle/StringViewExtras.h
index e4c2861ee5b6..93940a545e1f 100644
--- a/llvm/include/llvm/Demangle/StringViewExtras.h
+++ b/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
        
    
    
More information about the llvm-commits
mailing list