[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 08:23:22 PDT 2019
DiegoAstiazaran created this revision.
DiegoAstiazaran added reviewers: juliehockett, jakehehrlich.
DiegoAstiazaran added a project: clang-tools-extra.
Herald added a subscriber: arphaman.
Implement logic to compare the references of the index case insensitive.
https://reviews.llvm.org/D66299
Files:
clang-tools-extra/clang-doc/Representation.cpp
clang-tools-extra/clang-doc/Representation.h
clang-tools-extra/unittests/clang-doc/GeneratorTest.cpp
Index: clang-tools-extra/unittests/clang-doc/GeneratorTest.cpp
===================================================================
--- clang-tools-extra/unittests/clang-doc/GeneratorTest.cpp
+++ clang-tools-extra/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
Index: clang-tools-extra/clang-doc/Representation.h
===================================================================
--- clang-tools-extra/clang-doc/Representation.h
+++ clang-tools-extra/clang-doc/Representation.h
@@ -291,7 +291,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);
@@ -364,13 +365,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/clang-doc/Representation.cpp
===================================================================
--- clang-tools-extra/clang-doc/Representation.cpp
+++ clang-tools-extra/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)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D66299.215407.patch
Type: text/x-patch
Size: 3470 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190815/ce86a00a/attachment-0001.bin>
More information about the cfe-commits
mailing list