Skip to content

Commit 7c8b9c1

Browse files
[clang-format] [NFC] Restructure getLineCommentIndentPrefix
When sorting the known prefixes after length the if in the loop will hit at most once, so we can return from there. Also replace the inner loop with an algorithm, that makes it more readable. Differential Revision: https://reviews.llvm.org/D95081
1 parent 6cb2887 commit 7c8b9c1

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

clang/lib/Format/BreakableToken.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,26 @@ static bool IsBlank(char C) {
4242
static StringRef getLineCommentIndentPrefix(StringRef Comment,
4343
const FormatStyle &Style) {
4444
static constexpr StringRef KnownCStylePrefixes[] = {"///<", "//!<", "///",
45-
"//", "//!", "//:"};
46-
static constexpr StringRef KnownTextProtoPrefixes[] = {"//", "#", "##", "###",
47-
"####"};
45+
"//!", "//:", "//"};
46+
static constexpr StringRef KnownTextProtoPrefixes[] = {"####", "###", "##",
47+
"//", "#"};
4848
ArrayRef<StringRef> KnownPrefixes(KnownCStylePrefixes);
4949
if (Style.Language == FormatStyle::LK_TextProto)
5050
KnownPrefixes = KnownTextProtoPrefixes;
5151

52-
StringRef LongestPrefix;
52+
assert(std::is_sorted(KnownPrefixes.begin(), KnownPrefixes.end(),
53+
[](StringRef Lhs, StringRef Rhs) noexcept {
54+
return Lhs.size() > Rhs.size();
55+
}));
56+
5357
for (StringRef KnownPrefix : KnownPrefixes) {
5458
if (Comment.startswith(KnownPrefix)) {
55-
size_t PrefixLength = KnownPrefix.size();
56-
while (PrefixLength < Comment.size() && Comment[PrefixLength] == ' ')
57-
++PrefixLength;
58-
if (PrefixLength > LongestPrefix.size())
59-
LongestPrefix = Comment.substr(0, PrefixLength);
59+
const auto PrefixLength =
60+
Comment.find_first_not_of(' ', KnownPrefix.size());
61+
return Comment.substr(0, PrefixLength);
6062
}
6163
}
62-
return LongestPrefix;
64+
return {};
6365
}
6466

6567
static BreakableToken::Split

0 commit comments

Comments
 (0)