-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringTokenizer.h
More file actions
75 lines (66 loc) · 1.95 KB
/
StringTokenizer.h
File metadata and controls
75 lines (66 loc) · 1.95 KB
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
#ifndef STRINGTOKENIZER_H
#define STRINGTOKENIZER_H
#include <string>
int getNumber(int low, int high, std::string message);
/** The string_tokenizer class splits a string into a sequence of subtrings,
called tokens, separated by delimeters.
*/
class String_Tokenizer
{
public:
/** Construct a String_Tokenizer
@param source The string to be split into tokens
@param delim The string containing the delimeters. If
this parameter is omitted, a space character is assumed.
*/
String_Tokenizer(std::string source, std::string delim = " ") :
the_source(source), the_delim(delim), start(0), end(0) {
find_next();
}
/** Determine if there are more tokens
@return true if there are more tokens
*/
bool String_Tokenizer::has_more_tokens() {
return start != std::string::npos;
}
/** Retrieve the next token
@return the next token. If there are no more
tokens, an empty string is returned
*/
std::string String_Tokenizer::next_token() {
// Make sure there is a next token
if (!has_more_tokens())
return "";
// Save the next token in return_value
/*<snippet id="3" omit="false">*/
std::string token = the_source.substr(start, end - start);
/*</snippet>*/
// Find the following token
find_next();
// Return the next token
return token;
}
private:
/** Position start and end so that start is the index of the start
of the next token and end is the end.
*/
void String_Tokenizer::find_next() {
// Find the first character that is not a delimeter
/*<snippet id="1" omit="false">*/
start = the_source.find_first_not_of(the_delim, end);
/*</snippet>*/
// Find the next delimeter
/*<snippet id="2" omit="false">*/
end = the_source.find_first_of(the_delim, start);
/*</snippet>*/
}
/** The string to be split into tokens */
std::string the_source;
/** The string of delimeters */
std::string the_delim;
/** The index of the start of the next token */
size_t start;
/** The index of the end of the next token*/
size_t end;
};
#endif