#include "mgr/QueryParams.h"
#include "mgr/StringParam.h"
+#include <regex>
+
Mgr::QueryParam::Pointer
Mgr::QueryParams::get(const String& name) const
{
bool
Mgr::QueryParams::ParseParam(const String& paramStr, Param& param)
{
- bool parsed = false;
- regmatch_t pmatch[3];
- regex_t intExpr;
- regcomp(&intExpr, "^([a-z][a-z0-9_]*)=([0-9]+((,[0-9]+))*)$", REG_EXTENDED | REG_ICASE);
- regex_t stringExpr;
- regcomp(&stringExpr, "^([a-z][a-z0-9_]*)=([^&= ]+)$", REG_EXTENDED | REG_ICASE);
- if (regexec(&intExpr, paramStr.termedBuf(), 3, pmatch, 0) == 0) {
- param.first = paramStr.substr(pmatch[1].rm_so, pmatch[1].rm_eo);
+ std::regex intExpr("^([a-z][a-z0-9_]*)=([0-9]+((,[0-9]+))*)$", std::regex::extended | std::regex::icase);
+ std::regex stringExpr("^([a-z][a-z0-9_]*)=([^&= ]+)$", std::regex::extended | std::regex::icase);
+ std::smatch pmatch;
+
+ std::string temp(paramStr.termedBuf());
+ if (std::regex_match(temp, pmatch, intExpr)) {
+
+ auto itr = pmatch.begin();
+ ++itr; // move to [1] - first actual sub-match
+
+ // match [1] is the key name
+ param.first = itr->str().c_str();
+ ++itr;
+
+ // match [2] and later are a series of N,N,N,N,N values
std::vector<int> array;
- int n = pmatch[2].rm_so;
- for (int i = n; i < pmatch[2].rm_eo; ++i) {
- if (paramStr[i] == ',') {
- array.push_back(atoi(paramStr.substr(n, i).termedBuf()));
- n = i + 1;
- }
+ while (itr != pmatch.end()) {
+ if (itr->str().c_str()[0] == ',')
+ array.push_back(atoi(itr->str().c_str()+1));
+ else
+ array.push_back(atoi(itr->str().c_str()));
+ ++itr;
}
- if (n < pmatch[2].rm_eo)
- array.push_back(atoi(paramStr.substr(n, pmatch[2].rm_eo).termedBuf()));
param.second = new IntParam(array);
- parsed = true;
- } else if (regexec(&stringExpr, paramStr.termedBuf(), 3, pmatch, 0) == 0) {
- param.first = paramStr.substr(pmatch[1].rm_so, pmatch[1].rm_eo);
- param.second = new StringParam(paramStr.substr(pmatch[2].rm_so, pmatch[2].rm_eo));
- parsed = true;
+ return true;
}
- regfree(&stringExpr);
- regfree(&intExpr);
- return parsed;
+
+ if (std::regex_match(temp, pmatch, stringExpr)) {
+ param.first = pmatch[1].str().c_str();
+ param.second = new StringParam(pmatch[2].str().c_str());
+ return true;
+ }
+
+ return false;
}
bool