summaryrefslogtreecommitdiff
path: root/src/command_parse.cpp
diff options
context:
space:
mode:
authorattilamolnar <attilamolnar@hush.com>2013-06-01 22:55:37 +0200
committerattilamolnar <attilamolnar@hush.com>2013-06-01 22:55:37 +0200
commit84b40a76b8093a3bd505e79b06c685853c7726d1 (patch)
treed76cfd54478676975d070301c5a2983f9683c6a7 /src/command_parse.cpp
parentdac63207aac2ed05603aaa7691421b6bbfc5dc35 (diff)
Improve command parser logic when there are more params than Command::max_params
Diffstat (limited to 'src/command_parse.cpp')
-rw-r--r--src/command_parse.cpp35
1 files 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<std::string>::iterator it = --command_p.end();
+ // Iterator to the last parameter that will be kept
+ const std::vector<std::string>::iterator lastkeep = command_p.begin() + (cm->second->max_params - 1);
+ // Iterator to the first excess parameter
+ const std::vector<std::string>::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<std::string>::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());
}
/*