-
Notifications
You must be signed in to change notification settings - Fork 0
/
stringtokenizer.h
38 lines (28 loc) · 1.56 KB
/
stringtokenizer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#ifndef STRINGTOKENIZER_H
#define STRINGTOKENIZER_H
#include <string>
#include <vector>
// default delimiter string (space, tab, newline, carriage return, form feed)
const std::string DEFAULT_DELIMITER = " \t\v\n\r\f";
class StringTokenizer
{
public:
StringTokenizer();
StringTokenizer(const std::string& str, const std::string& delimiter=DEFAULT_DELIMITER);
// set string and delimiter
void set(const std::string& str, const std::string& delimiter=DEFAULT_DELIMITER);
void setString(const std::string& str); // set source string only
void setDelimiter(const std::string& delimiter); // set delimiter string only
void addDelimiter(const std::string& add); // add delimiter string only
std::string nextToken(); // return the next token, return "" if it ends
bool hasMoreTokens(); // true if has one more token else false
std::vector<std::string> split(); // return array of tokens from current cursor
private:
void skipDelimiter(); // ignore leading delimiters
bool isDelimiter(char c); // check if the current char is delimiter
std::string buffer; // input string
std::string token; // output string
std::string delimiter; // delimiter string
std::string::const_iterator currPos; // string iterator pointing the current position
};
#endif // STRINGTOKENIZER_H