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> JumpToSection; std::vector 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