Skip to content

Commit ee740b7

Browse files
[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
1 parent ecc70b4 commit ee740b7

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

libcxxabi/src/demangle/StringViewExtras.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,16 @@
2121

2222
DEMANGLE_NAMESPACE_BEGIN
2323

24-
inline bool starts_with(std::string_view self, char C) {
25-
return !self.empty() && self.front() == C;
24+
inline bool starts_with(std::string_view self, char C) noexcept {
25+
return !self.empty() && *self.begin() == C;
2626
}
2727

28-
inline bool starts_with(std::string_view haystack, std::string_view needle) {
29-
return haystack.substr(0, needle.size()) == needle;
28+
inline bool starts_with(std::string_view haystack,
29+
std::string_view needle) noexcept {
30+
if (needle.size() > haystack.size())
31+
return false;
32+
haystack.remove_suffix(haystack.size() - needle.size());
33+
return haystack == needle;
3034
}
3135

3236
DEMANGLE_NAMESPACE_END

llvm/include/llvm/Demangle/StringViewExtras.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,16 @@
2121

2222
DEMANGLE_NAMESPACE_BEGIN
2323

24-
inline bool starts_with(std::string_view self, char C) {
25-
return !self.empty() && self.front() == C;
24+
inline bool starts_with(std::string_view self, char C) noexcept {
25+
return !self.empty() && *self.begin() == C;
2626
}
2727

28-
inline bool starts_with(std::string_view haystack, std::string_view needle) {
29-
return haystack.substr(0, needle.size()) == needle;
28+
inline bool starts_with(std::string_view haystack,
29+
std::string_view needle) noexcept {
30+
if (needle.size() > haystack.size())
31+
return false;
32+
haystack.remove_suffix(haystack.size() - needle.size());
33+
return haystack == needle;
3034
}
3135

3236
DEMANGLE_NAMESPACE_END

0 commit comments

Comments
 (0)