Skip to content

Instantly share code, notes, and snippets.

@niklas88
Created February 28, 2018 10:15
Show Gist options
  • Save niklas88/eea4a2e850c937f6a6b66d358f4a60a1 to your computer and use it in GitHub Desktop.
Save niklas88/eea4a2e850c937f6a6b66d358f4a60a1 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <string_view>
#include <vector>
std::vector<std::string_view> split_no_strcpy(std::string_view orig, const char sep) {
std::vector<std::string_view> result;
if (orig.size() > 0) {
size_t from = 0;
size_t sepIndex = orig.find(sep);
while (sepIndex != std::string::npos) {
result.emplace_back(&orig[from], sepIndex - from);
from = sepIndex + 1;
sepIndex = orig.find(sep, from);
}
result.emplace_back(&orig[from], orig.size() - from);
}
return result;
}
int main(int argc, char **argv) {
std::string hello = u8"Hello World what up?";
auto splits = split_no_strcpy(hello, ' ');
for (std::string_view split : splits) {
std::cout << split << std::endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment