Commit patch to not break on spaces.
[bowtie.git] / tokenize.h
1 /*
2  * tokenize.h
3  *
4  *  Created on: Jul 21, 2009
5  *      Author: Ben Langmead
6  */
7
8 #ifndef TOKENIZE_H_
9 #define TOKENIZE_H_
10
11 #include <string>
12 #include <sstream>
13 #include <vector>
14
15 using namespace std;
16
17 /**
18  * Split string s according to given delimiters.  Mostly borrowed
19  * from C++ Programming HOWTO 7.3.
20  */
21 static inline void tokenize(const string& s,
22                             const string& delims,
23                             vector<string>& ss,
24                             size_t max = 9999)
25 {
26         string::size_type lastPos = s.find_first_not_of(delims, 0);
27         string::size_type pos = s.find_first_of(delims, lastPos);
28         while (string::npos != pos || string::npos != lastPos) {
29                 ss.push_back(s.substr(lastPos, pos - lastPos));
30                 lastPos = s.find_first_not_of(delims, pos);
31                 pos = s.find_first_of(delims, lastPos);
32                 if(ss.size() == (max - 1)) {
33                         pos = string::npos;
34                 }
35         }
36 }
37
38 static inline void tokenize(
39         const std::string& s,
40     char delim,
41     std::vector<std::string>& ss)
42 {
43         std::string token;
44         std::istringstream iss(s);
45         while(getline(iss, token, delim)) {
46                 ss.push_back(token);
47         }
48 }
49
50 #endif /*TOKENIZE_H_*/