Skip to content

Instantly share code, notes, and snippets.

@aman-tiwari
Created May 26, 2016 00:01
Show Gist options
  • Save aman-tiwari/2dcb4b582c681ed4889b422d6ccbbebb to your computer and use it in GitHub Desktop.
Save aman-tiwari/2dcb4b582c681ed4889b422d6ccbbebb to your computer and use it in GitHub Desktop.
// 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