Last active
April 17, 2020 19:56
-
-
Save thejohnfreeman/d1e298e0d860a1bf539e6b0178ae05d9 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
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