[PATCH] D66299: [clang-doc] Sort index elements case insensitive

Diego Astiazarán via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 15 16:31:25 PDT 2019


This revision was automatically updated to reflect the committed changes.
Closed by commit rL369068: [clang-doc] Sort index elements case insensitive (authored by DiegoAstiazaran, committed by ).
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Changed prior to commit:
  https://reviews.llvm.org/D66299?vs=215407&id=215500#toc

Repository:
  rL LLVM

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D66299/new/

https://reviews.llvm.org/D66299

Files:
  clang-tools-extra/trunk/clang-doc/Representation.cpp
  clang-tools-extra/trunk/clang-doc/Representation.h
  clang-tools-extra/trunk/unittests/clang-doc/GeneratorTest.cpp


Index: clang-tools-extra/trunk/clang-doc/Representation.h
===================================================================
--- clang-tools-extra/trunk/clang-doc/Representation.h
+++ clang-tools-extra/trunk/clang-doc/Representation.h
@@ -292,7 +292,8 @@
   SymbolInfo(InfoType IT) : Info(IT) {}
   SymbolInfo(InfoType IT, SymbolID USR) : Info(IT, USR) {}
   SymbolInfo(InfoType IT, SymbolID USR, StringRef Name) : Info(IT, USR, Name) {}
-  SymbolInfo(InfoType IT, SymbolID USR, StringRef Name, StringRef Path) : Info(IT, USR, Name, Path) {}
+  SymbolInfo(InfoType IT, SymbolID USR, StringRef Name, StringRef Path)
+      : Info(IT, USR, Name, Path) {}
 
   void merge(SymbolInfo &&I);
 
@@ -368,13 +369,14 @@
 
 struct Index : public Reference {
   Index() = default;
+  Index(StringRef Name) : Reference(Name) {}
   Index(StringRef Name, StringRef JumpToSection)
       : Reference(Name), JumpToSection(JumpToSection) {}
   Index(SymbolID USR, StringRef Name, InfoType IT, StringRef Path)
       : Reference(USR, Name, IT, Path) {}
   // This is used to look for a USR in a vector of Indexes using std::find
   bool operator==(const SymbolID &Other) const { return USR == Other; }
-  bool operator<(const Index &Other) const { return Name < Other.Name; }
+  bool operator<(const Index &Other) const;
 
   llvm::Optional<SmallString<16>> JumpToSection;
   std::vector<Index> Children;
Index: clang-tools-extra/trunk/clang-doc/Representation.cpp
===================================================================
--- clang-tools-extra/trunk/clang-doc/Representation.cpp
+++ clang-tools-extra/trunk/clang-doc/Representation.cpp
@@ -245,6 +245,26 @@
   return llvm::SmallString<16>("");
 }
 
+// Order is based on the Name attribute: case insensitive order
+bool Index::operator<(const Index &Other) const {
+  // Loop through each character of both strings
+  for (unsigned I = 0; I < Name.size() && I < Other.Name.size(); ++I) {
+    // Compare them after converting both to lower case
+    int D = tolower(Name[I]) - tolower(Other.Name[I]);
+    if (D == 0)
+      continue;
+    return D < 0;
+  }
+  // If both strings have the size it means they would be equal if changed to
+  // lower case. In here, lower case will be smaller than upper case
+  // Example: string < stRing = true
+  // This is the opposite of how operator < handles strings
+  if (Name.size() == Other.Name.size())
+    return Name > Other.Name;
+  // If they are not the same size; the shorter string is smaller
+  return Name.size() < Other.Name.size();
+}
+
 void Index::sort() {
   std::sort(Children.begin(), Children.end());
   for (auto &C : Children)
Index: clang-tools-extra/trunk/unittests/clang-doc/GeneratorTest.cpp
===================================================================
--- clang-tools-extra/trunk/unittests/clang-doc/GeneratorTest.cpp
+++ clang-tools-extra/trunk/unittests/clang-doc/GeneratorTest.cpp
@@ -70,5 +70,24 @@
   CheckIndex(ExpectedIdx, Idx);
 }
 
+TEST(GeneratorTest, sortIndex) {
+  Index Idx;
+  Idx.Children.emplace_back("b");
+  Idx.Children.emplace_back("aA");
+  Idx.Children.emplace_back("aa");
+  Idx.Children.emplace_back("A");
+  Idx.Children.emplace_back("a");
+  Idx.sort();
+
+  Index ExpectedIdx;
+  ExpectedIdx.Children.emplace_back("a");
+  ExpectedIdx.Children.emplace_back("A");
+  ExpectedIdx.Children.emplace_back("aa");
+  ExpectedIdx.Children.emplace_back("aA");
+  ExpectedIdx.Children.emplace_back("b");
+
+  CheckIndex(ExpectedIdx, Idx);
+}
+
 } // namespace doc
 } // namespace clang


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D66299.215500.patch
Type: text/x-patch
Size: 3524 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190815/b24a3bca/attachment.bin>


More information about the cfe-commits mailing list