From 8ee5394c1099a410f81e7218cee4a1c6ffaecbdd Mon Sep 17 00:00:00 2001 From: Zufu Liu Date: Sun, 15 Dec 2024 09:11:39 +1100 Subject: Feature [feature-requests:#1537]. Extract common code into function. --- src/AutoComplete.cxx | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/AutoComplete.cxx b/src/AutoComplete.cxx index 57a3073f9..3c7c98cee 100644 --- a/src/AutoComplete.cxx +++ b/src/AutoComplete.cxx @@ -163,21 +163,24 @@ struct Sorter { } }; +void FillSortMatrix(std::vector &sortMatrix, int itemCount) { + sortMatrix.clear(); + for (int i = 0; i < itemCount; i++) { + sortMatrix.push_back(i); + } +} + } void AutoComplete::SetList(const char *list) { if (autoSort == Ordering::PreSorted) { lb->SetList(list, separator, typesep); - sortMatrix.clear(); - for (int i = 0; i < lb->Length(); ++i) - sortMatrix.push_back(i); + FillSortMatrix(sortMatrix, lb->Length()); return; } const Sorter IndexSort(this, list); - sortMatrix.clear(); - for (int i = 0; i < static_cast(IndexSort.indices.size()) / 2; ++i) - sortMatrix.push_back(i); + FillSortMatrix(sortMatrix, static_cast(IndexSort.indices.size() / 2)); std::sort(sortMatrix.begin(), sortMatrix.end(), IndexSort); if (autoSort == Ordering::Custom || sortMatrix.size() < 2) { lb->SetList(list, separator, typesep); @@ -188,6 +191,7 @@ void AutoComplete::SetList(const char *list) { std::string sortedList; for (size_t i = 0; i < sortMatrix.size(); ++i) { const unsigned index = sortMatrix[i] * 2; + sortMatrix[i] = static_cast(i); // word length include trailing typesep and separator const int wordLen = IndexSort.indices[index + 2] - IndexSort.indices[index]; const std::string_view item(list + IndexSort.indices[index], wordLen); @@ -204,8 +208,6 @@ void AutoComplete::SetList(const char *list) { } } } - for (int i = 0; i < static_cast(sortMatrix.size()); ++i) - sortMatrix[i] = i; lb->SetList(sortedList.c_str(), separator, typesep); } -- cgit v1.2.3