[PATCH] D95081: [clang-format] [NFC] Restructure getLineCommentIndentPrefix
Björn Schäpers via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Thu Jan 21 04:11:14 PST 2021
HazardyKnusperkeks updated this revision to Diff 318151.
HazardyKnusperkeks added a comment.
Now with `assert`.
We could do a `static_assert`, but then we would have to write our own `is_sorted`, since the `std` is only `constexpr` in C++20.
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D95081/new/
https://reviews.llvm.org/D95081
Files:
clang/lib/Format/BreakableToken.cpp
Index: clang/lib/Format/BreakableToken.cpp
===================================================================
--- clang/lib/Format/BreakableToken.cpp
+++ clang/lib/Format/BreakableToken.cpp
@@ -42,24 +42,28 @@
static StringRef getLineCommentIndentPrefix(StringRef Comment,
const FormatStyle &Style) {
static constexpr StringRef KnownCStylePrefixes[] = {"///<", "//!<", "///",
- "//", "//!", "//:"};
- static constexpr StringRef KnownTextProtoPrefixes[] = {"//", "#", "##", "###",
- "####"};
+ "//!", "//:", "//"};
+ static constexpr StringRef KnownTextProtoPrefixes[] = {"####", "###", "##",
+ "//", "#"};
+
+
ArrayRef<StringRef> KnownPrefixes(KnownCStylePrefixes);
if (Style.Language == FormatStyle::LK_TextProto)
KnownPrefixes = KnownTextProtoPrefixes;
- StringRef LongestPrefix;
+ assert(std::is_sorted(KnownPrefixes.begin(), KnownPrefixes.end(),
+ [](StringRef Lhs, StringRef Rhs) noexcept {
+ return Lhs.size() > Rhs.size();
+ }));
+
for (StringRef KnownPrefix : KnownPrefixes) {
if (Comment.startswith(KnownPrefix)) {
- size_t PrefixLength = KnownPrefix.size();
- while (PrefixLength < Comment.size() && Comment[PrefixLength] == ' ')
- ++PrefixLength;
- if (PrefixLength > LongestPrefix.size())
- LongestPrefix = Comment.substr(0, PrefixLength);
+ const auto PrefixLength =
+ Comment.find_first_not_of(' ', KnownPrefix.size());
+ return Comment.substr(0, PrefixLength);
}
}
- return LongestPrefix;
+ return {};
}
static BreakableToken::Split
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D95081.318151.patch
Type: text/x-patch
Size: 1879 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210121/edb3d4a2/attachment-0001.bin>
More information about the cfe-commits
mailing list