int main() { std::vector<std::string> v = { "oranguatan", "orange" }; assert(findLongestCommonPrefix(v) == "orang"); v.push_back("orifice"); assert(findLongestCommonPrefix(v) == "or"); v.push_back("onerous"); assert(findLongestCommonPrefix(v) == "o"); v.push_back("a"); assert(findLongestCommonPrefix(v) == ""); return 0; }
std::string ConsoleCommandManager::onComplete(ConsoleCommandRequest & request) { string name = request.command(); // If exactly one command fits the name, ask the command to // fill in the rest if (commands.find(name) != commands.end()) { return name + " " + commands[name]->onComplete(request); } // If there are additional tokens, autocomplete would destroy them // so return if (request.tokens().hasMoreTokens()) { return name + " " + request.tokens().remainingString(); } // Find all commands that has name as a prefix vector<string> matches = findCommandsByPrefix(name); // No commands return the string as we received it if (matches.size() == 0) { return name; } // Print the commands that matches the prefix request.out() << "\n"; for (unsigned int i = 0; i < matches.size(); ++i) { request.out() << matches[i] << "\n"; } // Return the longest common command prefix return findLongestCommonPrefix(name, matches); }