From 84b40a76b8093a3bd505e79b06c685853c7726d1 Mon Sep 17 00:00:00 2001 From: attilamolnar Date: Sat, 1 Jun 2013 22:55:37 +0200 Subject: [PATCH] Improve command parser logic when there are more params than Command::max_params --- src/command_parse.cpp | 35 +++++++++++++---------------------- 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/src/command_parse.cpp b/src/command_parse.cpp index 8008f1d4c..721bc2c61 100644 --- a/src/command_parse.cpp +++ b/src/command_parse.cpp @@ -237,6 +237,8 @@ bool CommandParser::ProcessCommand(LocalUser *user, std::string &cmd) } } + // If we were given more parameters than max_params then append the excess parameter(s) + // to command_p[maxparams-1], i.e. to the last param that is still allowed if (cm->second->max_params && command_p.size() > cm->second->max_params) { /* @@ -246,32 +248,21 @@ bool CommandParser::ProcessCommand(LocalUser *user, std::string &cmd) * a * test */ - std::string lparam; - /* - * The '-1' here is a clever trick, we'll go backwards throwing everything into a temporary param - * and then just toss that into the array. - * -- w00t - */ - while (command_p.size() > (cm->second->max_params - 1)) - { - // BE CAREFUL: .end() returns past the end of the vector, hence decrement. - std::vector::iterator it = --command_p.end(); + // Iterator to the last parameter that will be kept + const std::vector::iterator lastkeep = command_p.begin() + (cm->second->max_params - 1); + // Iterator to the first excess parameter + const std::vector::iterator firstexcess = lastkeep + 1; - lparam.insert(0, " " + *(it)); - command_p.erase(it); // remove last element + // Append all excess parameter(s) to the last parameter, seperated by spaces + for (std::vector::const_iterator i = firstexcess; i != command_p.end(); ++i) + { + lastkeep->push_back(' '); + lastkeep->append(*i); } - /* we now have (each iteration): - * ' test' - * ' a test' - * ' is a test' <-- final string - * ...now remove the ' ' at the start... - */ - lparam.erase(lparam.begin()); - - /* param is now 'is a test', which is exactly what we wanted! */ - command_p.push_back(lparam); + // Erase the excess parameter(s) + command_p.erase(firstexcess, command_p.end()); } /* -- 2.39.5