I want to be able to solve problems like this: Getting std :: ifstream to handle LF, CR, and CRLF? where an istream
needs to be tokenized by a complex delimiter; such that the only way to tokenize the istream
is to:
- Read it in the
istream
a character at a time - Collect the characters
- When a delimiter is hit return the collection as a token
Regexes are very good at tokenizing strings with complex delimiters:
string foo{ "A
B
C
" };
vector<string> bar;
// This puts {"A", "B", "C"} into bar
transform(sregex_iterator(foo.cbegin(), foo.cend(), regex("(.*)(?:
?|
)")), sregex_iterator(), back_inserter(bar), [](const smatch& i){ return i[1].str(); });
But I can't use a regex_iterator
on a istream
:( My solution has been to slurp the istream
and then run the regex_iterator
over it, but the slurping step seems superfluous.
Is there an unholy combination of istream_iterator
and regex_iterator
out there somewhere, or if I want it do I have to write it myself?