Skip to content

Instantly share code, notes, and snippets.

@takamin
Last active August 29, 2015 14:05
Show Gist options
  • Save takamin/b241802651ac8b0a2a1d to your computer and use it in GitHub Desktop.
Save takamin/b241802651ac8b0a2a1d to your computer and use it in GitHub Desktop.
split string by char
#include <string>
template<class StorageType>
void split(
char c, const std::string& s,
StorageType& result)
{
size_t pos, offset = 0;
while (true) {
pos = s.find(c, offset);
if (pos == std::string::npos) {
result.push_back(
s.substr(offset, pos));
break;
}
else {
result.push_back(
s.substr(offset, pos - offset));
offset = pos + 1;
}
}
}
#include <iostream>
#include <typeinfo>
#include <assert.h>
#include "string_split.h"
#include <vector>
#include <list>
#include <deque>
using namespace std;
template <class StorageType>
void test_split(
StorageType& split_result)
{
split_result.clear();
::split(',', "", split_result);
assert(split_result.size() == 1);
assert(split_result.at(0) == "");
split_result.clear();
::split(',', "A", split_result);
assert(split_result.size() == 1);
assert(split_result.at(0) == "A");
split_result.clear();
::split(',', "AB", split_result);
assert(split_result.size() == 1);
assert(split_result.at(0) == "AB");
split_result.clear();
::split(',', ",", split_result);
assert(split_result.size() == 2);
assert(split_result.at(0) == "");
assert(split_result.at(1) == "");
split_result.clear();
::split(',', "A,", split_result);
assert(split_result.size() == 2);
assert(split_result.at(0) == "A");
assert(split_result.at(1) == "");
split_result.clear();
::split(',', ",A", split_result);
assert(split_result.size() == 2);
assert(split_result.at(0) == "");
assert(split_result.at(1) == "A");
split_result.clear();
::split(',', ",A,", split_result);
assert(split_result.size() == 3);
assert(split_result.at(0) == "");
assert(split_result.at(1) == "A");
assert(split_result.at(2) == "");
split_result.clear();
::split(',', ",,", split_result);
assert(split_result.size() == 3);
assert(split_result.at(0) == "");
assert(split_result.at(1) == "");
assert(split_result.at(2) == "");
split_result.clear();
::split(',', "ABC,BCD", split_result);
assert(split_result.size() == 2);
assert(split_result.at(0) == "ABC");
assert(split_result.at(1) == "BCD");
split_result.clear();
::split(',', ",ABC,BCD", split_result);
assert(split_result.size() == 3);
assert(split_result.at(0) == "");
assert(split_result.at(1) == "ABC");
assert(split_result.at(2) == "BCD");
split_result.clear();
::split(',', "ABC,BCD,", split_result);
assert(split_result.size() == 3);
assert(split_result.at(0) == "ABC");
assert(split_result.at(1) == "BCD");
assert(split_result.at(2) == "");
split_result.clear();
::split(',', "ABC,,BCD,", split_result);
assert(split_result.size() == 4);
assert(split_result.at(0) == "ABC");
assert(split_result.at(1) == "");
assert(split_result.at(2) == "BCD");
assert(split_result.at(3) == "");
split_result.clear();
::split(',', ",ABC,BCD,", split_result);
assert(split_result.size() == 4);
assert(split_result.at(0) == "");
assert(split_result.at(1) == "ABC");
assert(split_result.at(2) == "BCD");
assert(split_result.at(3) == "");
std::cerr << "test for "
<< typeid(split_result).name()
<< " pass." << std::endl;
}
int main(int argc, char*argv[]) {
vector<string> vector_split_result;
test_split(vector_split_result);
deque<string> deque_split_result;
test_split(deque_split_result);
std::cerr << "all test pass." << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment