Parse string to create a list of element
- by Nick
I have a string like this:
"\r color=\"red\" name=\"Jon\" \t\n depth=\"8.26\" "
And I want to parse this string and create a std::list of this object:
class data
{
std::string name;
std::string value;
};
Where for example:
name = color
value = red
What is the fastest way? I can use boost.
EDIT:
This is what i've tried:
vector<string> tokens;
split(tokens, str, is_any_of(" \t\f\v\n\r"));
if(tokens.size() > 1)
{
list<data> attr;
for_each(tokens.begin(), tokens.end(), [&attr](const string& token)
{
if(token.empty() || !contains(token, "="))
return;
vector<string> tokens;
split(tokens, token, is_any_of("="));
erase_all(tokens[1], "\"");
attr.push_back(data(tokens[0], tokens[1]));
}
);
}
But it does not work if there are spaces inside " ": like color="red 1".