[libcxx-commits] [PATCH] D151260: [Demangle] avoid more std::string_view::substr
Nick Desaulniers via Phabricator via libcxx-commits
libcxx-commits at lists.llvm.org
Tue May 23 14:39:26 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/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,18 @@
DEMANGLE_NAMESPACE_BEGIN
-inline bool starts_with(std::string_view self, char C) {
+inline bool starts_with(std::string_view self, char C) noexcept {
return !self.empty() && self.front() == 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.length() > haystack.length())
+ return false;
+ haystack.remove_suffix(haystack.length() - needle.length());
+ 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) {
+inline bool starts_with(std::string_view self, char C) noexcept {
return !self.empty() && self.front() == 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.length() > haystack.length())
+ return false;
+ haystack.remove_suffix(haystack.length() - needle.length());
+ return haystack == needle;
}
DEMANGLE_NAMESPACE_END
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D151260.524891.patch
Type: text/x-patch
Size: 1830 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20230523/504bdbcf/attachment.bin>
More information about the libcxx-commits
mailing list