samedi 25 avril 2015

Would Rewriting It Using Regex Shorten/Beautify The Code?


The problem is a little challenging because I want to code it using std::regex believing it would be easier to read and faster to write.

But it seems that I can only code it one way (shown below).

Somehow my mind could not see the solution using std::regex.

How would you code it?

Would using std::regex_search do the job?

/*
input: data coming in:
/product/country/123456/city/7890/g.json

input: url parameter format:
/product/country/<id1:[0-9]+>/city/<id2:[0-9]+>/g.json

output:
std::vector<std::string> urlParams

sample output:
urlParams[0] = "123456"
urlParams[1] = "7890"
*/

bool ParseIt(const char *path, const char* urlRoute, std::vector<std::string> *urlParams)
{
   const DWORD BUFSZ = 2000;
   char buf[BUFSZ];
   DWORD dwSize = strlen(urlRoute);
   urlParams.clear();

   int j = 0;
   int i = 0;
   bool good = false;
   for (i = 0; i < dwSize; i++)
   {
       char c1 = path[j++];
       char c2 = urlRoute[i];
       if (c2 == '<')
       {
           good = true;
           while (c2 != '/')
           { 
               i++;
               c2 = urlRoute[i];
           }
           int k = 0;
           memset(buf, 0, BUFSZ);
           while (c1 != '/')
           {
               buf[k++] = c1;
               c1 = path[j++];
           }
           urlParams->push_back(_strdup(buf));
           int b = 1;
       }
       if (c1 != c2)
       {
           return false;
       }
       if (c2 != '<')
       {
           if (c1 == c1)
           {

           }
           else
           {
               return false;
           }
        }

    }

    if (dwSize == i && good)
    {
        return true;
    }

    return false;
}


Aucun commentaire:

Enregistrer un commentaire