Last active
August 29, 2015 14:05
-
-
Save takamin/b241802651ac8b0a2a1d to your computer and use it in GitHub Desktop.
split string by char
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> | |
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; | |
} | |
} | |
} |
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 <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