From: Amos Jeffries Date: Mon, 27 Jul 2015 20:25:57 +0000 (-0700) Subject: Convert cachemgr parameter parser to std::regex X-Git-Tag: M-staged-PR71~362^2~6 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e7faa22e4808eec791f3bda4e88a9b20290769a5;p=thirdparty%2Fsquid.git Convert cachemgr parameter parser to std::regex --- diff --git a/src/mgr/QueryParams.cc b/src/mgr/QueryParams.cc index 96bca8ae09..c08b878a7a 100644 --- a/src/mgr/QueryParams.cc +++ b/src/mgr/QueryParams.cc @@ -15,6 +15,8 @@ #include "mgr/QueryParams.h" #include "mgr/StringParam.h" +#include + Mgr::QueryParam::Pointer Mgr::QueryParams::get(const String& name) const { @@ -68,34 +70,40 @@ Mgr::QueryParams::find(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 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