| 148 | }; |
| 149 | |
| 150 | void AutoComplete::SetList(const char *list) { |
| 151 | if (autoSort == SC_ORDER_PRESORTED) { |
| 152 | lb->SetList(list, separator, typesep); |
| 153 | sortMatrix.clear(); |
| 154 | for (int i = 0; i < lb->Length(); ++i) |
| 155 | sortMatrix.push_back(i); |
| 156 | return; |
| 157 | } |
| 158 | |
| 159 | Sorter IndexSort(this, list); |
| 160 | sortMatrix.clear(); |
| 161 | for (int i = 0; i < (int)IndexSort.indices.size() / 2; ++i) |
| 162 | sortMatrix.push_back(i); |
| 163 | std::sort(sortMatrix.begin(), sortMatrix.end(), IndexSort); |
| 164 | if (autoSort == SC_ORDER_CUSTOM || sortMatrix.size() < 2) { |
| 165 | lb->SetList(list, separator, typesep); |
| 166 | PLATFORM_ASSERT(lb->Length() == static_cast<int>(sortMatrix.size())); |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | std::string sortedList; |
| 171 | char item[maxItemLen]; |
| 172 | for (size_t i = 0; i < sortMatrix.size(); ++i) { |
| 173 | int wordLen = IndexSort.indices[sortMatrix[i] * 2 + 2] - IndexSort.indices[sortMatrix[i] * 2]; |
| 174 | strncpy(item, list + IndexSort.indices[sortMatrix[i] * 2], wordLen); |
| 175 | if ((i+1) == sortMatrix.size()) { |
| 176 | // Last item so remove separator if present |
| 177 | if ((wordLen > 0) && (item[wordLen-1] == separator)) |
| 178 | wordLen--; |
| 179 | } else { |
| 180 | // Item before last needs a separator |
| 181 | if ((wordLen == 0) || (item[wordLen-1] != separator)) { |
| 182 | item[wordLen] = separator; |
| 183 | wordLen++; |
| 184 | } |
| 185 | } |
| 186 | item[wordLen] = '\0'; |
| 187 | sortedList += item; |
| 188 | } |
| 189 | for (int i = 0; i < (int)sortMatrix.size(); ++i) |
| 190 | sortMatrix[i] = i; |
| 191 | lb->SetList(sortedList.c_str(), separator, typesep); |
| 192 | } |
| 193 | |
| 194 | int AutoComplete::GetSelection() const { |
| 195 | return lb->GetSelection(); |
no test coverage detected