/** Erase from a list of wcstring values at specified indexes */ static void erase_values(wcstring_list_t &list, const std::vector<long> &indexes) { // Make a set of indexes. // This both sorts them into ascending order and removes duplicates. const std::set<long> indexes_set(indexes.begin(), indexes.end()); // Now walk the set backwards, so we encounter larger indexes first, and remove elements at the given (1-based) indexes. std::set<long>::const_reverse_iterator iter; for (iter = indexes_set.rbegin(); iter != indexes_set.rend(); iter++) { long val = *iter; if (val > 0 && val <= list.size()) { // One-based indexing! list.erase(list.begin() + val - 1); } } }
/** Merge multiple completions with the same description to the same line */ static void join_completions( wcstring_list_t lst ) { std::map<wcstring, long> desc_table; for( size_t i=0; i<lst.size(); i++ ) { const wchar_t *item = lst.at(i).c_str(); const wchar_t *desc = wcschr( item, COMPLETE_SEP ); long prev_idx; if( !desc ) continue; desc++; prev_idx = desc_table[desc] - 1; if( prev_idx == -1 ) { desc_table[desc] = (long)(i+1); } else { const wchar_t *old = lst.at(i).c_str(); const wchar_t *old_end = wcschr( old, COMPLETE_SEP ); if( old_end ) { wcstring foo; foo.append(old, old_end - old); foo.push_back(COMPLETE_ITEM_SEP); foo.append(item); lst.at(prev_idx) = foo; lst.at(i).clear(); } } } /* Remove empty strings */ lst.erase(remove(lst.begin(), lst.end(), wcstring()), lst.end()); }