Skip to content

Commit

Permalink
[Demangle] avoid more std::string_view::substr
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nickdesaulniers committed May 25, 2023
1 parent ecc70b4 commit ee740b7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
12 changes: 8 additions & 4 deletions libcxxabi/src/demangle/StringViewExtras.h
Expand Up @@ -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
Expand Down
12 changes: 8 additions & 4 deletions llvm/include/llvm/Demangle/StringViewExtras.h
Expand Up @@ -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
Expand Down

0 comments on commit ee740b7

Please sign in to comment.