[clang-doc] Sort index elements case insensitive

Implement logic to compare the references of the index case insensitive.

Differential revision: https://reviews.llvm.org/D66299

llvm-svn: 369068
This commit is contained in:
Diego Astiazaran 2019-08-15 23:32:12 +00:00
parent 4be5d53a33
commit b7bb9fb28f
3 changed files with 43 additions and 2 deletions

View File

@ -245,6 +245,26 @@ llvm::SmallString<16> Info::extractName() const {
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)

View File

@ -292,7 +292,8 @@ struct SymbolInfo : public Info {
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 EnumInfo : public SymbolInfo {
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;

View File

@ -70,5 +70,24 @@ TEST(GeneratorTest, emitIndex) {
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