[llvm-branch-commits] [clang] 7c8b9c1 - [clang-format] [NFC] Restructure getLineCommentIndentPrefix

Björn Schäpers via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Jan 25 12:08:16 PST 2021


Author: Björn Schäpers
Date: 2021-01-25T21:02:40+01:00
New Revision: 7c8b9c102f38a4d29ac56ef7bd1da003f03900d0

URL: https://github.com/llvm/llvm-project/commit/7c8b9c102f38a4d29ac56ef7bd1da003f03900d0
DIFF: https://github.com/llvm/llvm-project/commit/7c8b9c102f38a4d29ac56ef7bd1da003f03900d0.diff

LOG: [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

Added: 
    

Modified: 
    clang/lib/Format/BreakableToken.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/BreakableToken.cpp b/clang/lib/Format/BreakableToken.cpp
index d5167c01dc23..0ec00e039f33 100644
--- a/clang/lib/Format/BreakableToken.cpp
+++ b/clang/lib/Format/BreakableToken.cpp
@@ -42,24 +42,26 @@ static bool IsBlank(char C) {
 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


        


More information about the llvm-branch-commits mailing list