Created
May 26, 2016 00:01
-
-
Save aman-tiwari/2dcb4b582c681ed4889b422d6ccbbebb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Parses a url query (key=val&key_2=val_2&key_3=val_3) into a map | |
std::map<std::string, std::string> query_to_map(const std::string query) { | |
std::map<std::string, std::string> res; | |
std::ostringstream curr_key; | |
std::ostringstream curr_val; | |
bool in_key = true; | |
for(const auto& s : query) { | |
if(s == '=') { | |
in_key = false; | |
} else if (s == '&') { | |
in_key = true; | |
res[curr_key.str()] = curr_val.str(); | |
curr_key.str(""); | |
curr_val.str(""); | |
curr_key.clear(); | |
curr_val.clear(); | |
} else { | |
if(in_key) { | |
curr_key << s; | |
} else { | |
curr_val << s; | |
} | |
} | |
} | |
res[curr_key.str()] = curr_val.str(); | |
return res; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment