Skip to content
Snippets Groups Projects
Commit 69c11d6e authored by Brent Pedersen's avatar Brent Pedersen
Browse files

add TokenizeColumns function

parent 56af1979
No related branches found
No related tags found
No related merge requests found
...@@ -46,5 +46,40 @@ void Tokenize(const string &str, vector<int> &elems, char delimiter = '\t') ...@@ -46,5 +46,40 @@ void Tokenize(const string &str, vector<int> &elems, char delimiter = '\t')
} }
} }
// tokenize a column string into a list of integers.
inline
void TokenizeColumns(const string &str, vector<int> &elems)
{
// http://stackoverflow.com/questions/236129/how-to-split-a-string-in-c/236803#236803
// NOTE: this approach intentionally allows consecutive delimiters
vector<string> col_sets;
Tokenize(str, col_sets, ',');
for( size_t i = 0; i < col_sets.size(); i++ ) {
string col_set = col_sets[i];
if( string::npos == col_set.find("-") ){
elems.push_back(atoi(col_set.c_str()));
}
else {
vector<string> ends;
Tokenize(col_set, ends, '-');
int start = atoi(ends[0].c_str());
int end = atoi(ends[1].c_str());
if(start <= end){
for(int i = start; i <= end; i++){
elems.push_back(i);
}
}
else {
for(int i = start; i >= end; i--){
elems.push_back(i);
}
}
}
}
}
#endif /* LINEFILEUTILITIES_H */ #endif /* LINEFILEUTILITIES_H */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment