]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Convert cachemgr parameter parser to std::regex
authorAmos Jeffries <squid3@treenet.co.nz>
Mon, 27 Jul 2015 20:25:57 +0000 (13:25 -0700)
committerAmos Jeffries <squid3@treenet.co.nz>
Mon, 27 Jul 2015 20:25:57 +0000 (13:25 -0700)
src/mgr/QueryParams.cc

index 96bca8ae0955c29aa8277394c69d15ffd994c3ce..c08b878a7a55f96c1360ebefc33a17d06a36f698 100644 (file)
@@ -15,6 +15,8 @@
 #include "mgr/QueryParams.h"
 #include "mgr/StringParam.h"
 
+#include <regex>
+
 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<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