Created
July 9, 2020 23:02
-
-
Save matt-42/008224d7aa8fdf7e3ea99147ad17df24 to your computer and use it in GitHub Desktop.
C++ split implementation
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
#include <string_view> | |
#include <iostream> | |
#include <cstring> | |
#include <vector> | |
std::vector<std::string> split(const std::string& str, char split_char) { | |
const char* cur = str.c_str(); | |
const char* str_end = str.c_str() + str.size(); | |
std::vector<std::string> chunks; | |
while (cur < str_end) | |
{ | |
const char* start = cur; | |
while (start < str_end and *start == split_char) | |
start++; | |
const char* end = start + 1; | |
while (end < str_end and *end != split_char) | |
end++; | |
cur = end + 1; | |
chunks.push_back(std::string(start, end - start)); | |
} | |
return chunks; | |
} | |
int main() { | |
for (auto c : split("1,2,3,4", ',')) std::cout << c << std::endl; | |
} | |
std::string_view next_chunk(const char*& cur, const char* line_end, char split_char) { | |
const char* start = cur; | |
while (start < line_end and *start == split_char) | |
start++; | |
const char* end = start + 1; | |
while (end < line_end and *end != split_char) | |
end++; | |
cur = end + 1; | |
return std::string_view(start, end - start); | |
} | |
int main() { | |
const char* str = "1,2,3,4"; | |
const char* end = str + strlen(str); | |
const char* it = str; | |
while (it < end) { | |
std::cout << next_chunk(it, end, ',') << std::endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment