1 /*
2 * Copyright 2011, Ben Langmead <langmea@cs.jhu.edu>
3 *
4 * This file is part of Bowtie 2.
5 *
6 * Bowtie 2 is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Bowtie 2 is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Bowtie 2. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #ifndef TOKENIZE_H_
21 #define TOKENIZE_H_
22
23 #include <string>
24 #include <sstream>
25 #include <limits>
26
27 using namespace std;
28
29 /**
30 * Split string s according to given delimiters. Mostly borrowed
31 * from C++ Programming HOWTO 7.3.
32 */
33 template<typename T>
34 static inline void tokenize(
35 const string& s,
36 const string& delims,
37 T& ss,
38 size_t max = std::numeric_limits<size_t>::max())
39 {
40 //string::size_type lastPos = s.find_first_not_of(delims, 0);
41 string::size_type lastPos = 0;
42 string::size_type pos = s.find_first_of(delims, lastPos);
43 while (string::npos != pos || string::npos != lastPos) {
44 ss.push_back(s.substr(lastPos, pos - lastPos));
45 lastPos = s.find_first_not_of(delims, pos);
46 pos = s.find_first_of(delims, lastPos);
47 if(ss.size() == (max - 1)) {
48 pos = string::npos;
49 }
50 }
51 }
52
53 template<typename T>
tokenize(const std::string & s,char delim,T & ss)54 static inline void tokenize(const std::string& s, char delim, T& ss) {
55 std::string token;
56 std::istringstream iss(s);
57 while(getline(iss, token, delim)) {
58 ss.push_back(token);
59 }
60 }
61
62 #endif /*TOKENIZE_H_*/
63