Skip to content

Instantly share code, notes, and snippets.

@thejohnfreeman
Last active April 17, 2020 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thejohnfreeman/d1e298e0d860a1bf539e6b0178ae05d9 to your computer and use it in GitHub Desktop.
Save thejohnfreeman/d1e298e0d860a1bf539e6b0178ae05d9 to your computer and use it in GitHub Desktop.
split(FwdIt first, FwdIt last, Char delim)
{
Result result;
using string = typename Result::value_type;
FwdIt iter = first;
string word;
bool need_space = false;
while (iter != last)
{
Char c = *iter++;
if (is_lws(c)) {
if (need_space) {
word.append(1, ' ');
need_space = false;
}
continue;
}
if (c == delim)
goto end_word;
need_space = true;
if (c == '"')
{
// quoted-string
while (iter != last)
{
c = *iter++;
if (c == '"')
{
break;
}
if (c == '\\')
{
// quoted-pair
if (iter == last)
break;
c = *iter++;
}
word.append(1, c);
}
continue;
}
word.append(1, c);
continue;
end_word:
// If need_space == false, then we have added a space expecting to add
// more non-whitespace characters.
if (!word.empty() && !need_space)
word.pop_back();
if (!word.empty()) {
result.emplace_back(std::move(word));
word.clear();
}
need_space = false;
}
if (!word.empty() && !need_space)
word.pop_back();
if (!word.empty())
result.emplace_back(std::move(word));
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment