[PATCH] D95081: [clang-format] [NFC] Restructure getLineCommentIndentPrefix

Björn Schäpers via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 20 12:52:03 PST 2021


HazardyKnusperkeks created this revision.
HazardyKnusperkeks added reviewers: MyDeveloperDay, curdeius.
HazardyKnusperkeks added a project: clang-format.
HazardyKnusperkeks requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

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.


Repository:
  rG LLVM Github Monorepo

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
@@ -41,25 +41,23 @@
 
 static StringRef getLineCommentIndentPrefix(StringRef Comment,
                                             const FormatStyle &Style) {
+  // Keep these prefixes sorted after length.
   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;
   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.317978.patch
Type: text/x-patch
Size: 1695 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210120/4a421cdb/attachment-0001.bin>


More information about the cfe-commits mailing list