-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringTokenizer.h
84 lines (70 loc) · 1.74 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
* StringTokenizer.h
*
* Created on: Jul 7, 2010
* Author: d3p708
*/
#ifndef STRINGTOKENIZER_H_
#define STRINGTOKENIZER_H_
#include <cstring>
#include <string>
#include "parse.h"
namespace svmpack
{
class StringTokenizer
{
#define _DEFAULT_DELIMITER_ " \t\r\n\v"
public:
StringTokenizer ( string str_in, char *delimiter_in = 0x0 ) :
str ( str_in ), delimiter ( delimiter_in ), first ( 0 ), last ( 0 ) {
if ( delimiter == 0x0 ) delimiter = _DEFAULT_DELIMITER_;
tokenize();
};
~StringTokenizer() {};
bool hasMoreTokens() const throw() {
return ( first != string::npos );
};
size_t countTokens() throw() {
size_t f = first;
size_t l = last;
size_t ntoken = 0;
while ( first != string::npos ) {
++ntoken;
tokenize();
}
first = f;
last = l;
return ntoken;
};
template <class T> inline T nextElement() throw() {
return parse<T> ( this->nextToken().c_str() );
};
string nextToken() throw() {
size_t f = first;
size_t l = last;
tokenize();
return str.substr ( f, ( l - f ) );
};
void resetString ( string& str_in ) throw() {
str = str_in;
first = 0;
last = 0;
tokenize();
};
void resetDelimiter ( char *delimiter_in ) throw() {
delimiter = delimiter_in;
if ( delimiter == 0 ) {
delimiter = _DEFAULT_DELIMITER_ ;
}
};
private:
string str;
char *delimiter;
size_t first, last;
void tokenize() {
first = str.find_first_not_of ( delimiter, last );
last = str.find_first_of ( delimiter, first );
};
};
}
#endif /* STRINGTOKENIZER_H_ */