1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef BASE_STRING_TOKENIZER_H_
6 #define BASE_STRING_TOKENIZER_H_
10 // StringTokenizerT is a simple string tokenizer class. It works like an
11 // iterator that with each step (see the Advance method) updates members that
12 // refer to the next token in the input string. The user may optionally
13 // configure the tokenizer to return delimiters.
15 // Warning: be careful not to pass a C string into the 2-arg constructor:
16 // StringTokenizer t("this is a test", " "); // WRONG
17 // This will create a temporary std::string, save the begin() and end()
18 // iterators, and then the string will be freed before we actually start
20 // Instead, use a std::string or use the 3 arg constructor of CStringTokenizer.
25 // char input[] = "this is a test";
26 // CStringTokenizer t(input, input + strlen(input), " ");
27 // while (t.GetNext()) {
28 // printf("%s\n", t.token().c_str());
41 // std::string input = "no-cache=\"foo, bar\", private";
42 // StringTokenizer t(input, ", ");
43 // t.set_quote_chars("\"");
44 // while (t.GetNext()) {
45 // printf("%s\n", t.token().c_str());
50 // no-cache="foo, bar"
56 // bool next_is_option = false, next_is_value = false;
57 // std::string input = "text/html; charset=UTF-8; foo=bar";
58 // StringTokenizer t(input, "; =");
59 // t.set_options(StringTokenizer::RETURN_DELIMS);
60 // while (t.GetNext()) {
61 // if (t.token_is_delim()) {
62 // switch (*t.token_begin()) {
64 // next_is_option = true;
67 // next_is_value = true;
72 // if (next_is_option) {
73 // label = "option-name";
74 // next_is_option = false;
75 // } else if (next_is_value) {
76 // label = "option-value";
77 // next_is_value = false;
79 // label = "mime-type";
81 // printf("%s: %s\n", label, t.token().c_str());
86 template <class str
, class const_iterator
>
87 class StringTokenizerT
{
89 typedef typename
str::value_type char_type
;
91 // Options that may be pass to set_options()
93 // Specifies the delimiters should be returned as tokens
94 RETURN_DELIMS
= 1 << 0,
97 // The string object must live longer than the tokenizer. (In particular this
98 // should not be constructed with a temporary.)
99 StringTokenizerT(const str
& string
,
101 Init(string
.begin(), string
.end(), delims
);
104 StringTokenizerT(const_iterator string_begin
,
105 const_iterator string_end
,
107 Init(string_begin
, string_end
, delims
);
110 // Set the options for this tokenizer. By default, this is 0.
111 void set_options(int options
) { options_
= options
; }
113 // Set the characters to regard as quotes. By default, this is empty. When
114 // a quote char is encountered, the tokenizer will switch into a mode where
115 // it ignores delimiters that it finds. It switches out of this mode once it
116 // finds another instance of the quote char. If a backslash is encountered
117 // within a quoted string, then the next character is skipped.
118 void set_quote_chars(const str
& quotes
) { quotes_
= quotes
; }
120 // Call this method to advance the tokenizer to the next delimiter. This
121 // returns false if the tokenizer is complete. This method must be called
122 // before calling any of the token* methods.
125 token_is_delim_
= false;
127 token_begin_
= token_end_
;
128 if (token_end_
== end_
)
131 if (AdvanceOne(&state
, *token_begin_
))
133 if (options_
& RETURN_DELIMS
) {
134 token_is_delim_
= true;
137 // else skip over delim
139 while (token_end_
!= end_
&& AdvanceOne(&state
, *token_end_
))
144 // Start iterating through tokens from the beginning of the string.
146 token_end_
= start_pos_
;
149 // Returns true if token is a delimiter. When the tokenizer is constructed
150 // with the RETURN_DELIMS option, this method can be used to check if the
151 // returned token is actually a delimiter.
152 bool token_is_delim() const { return token_is_delim_
; }
154 // If GetNext() returned true, then these methods may be used to read the
155 // value of the token.
156 const_iterator
token_begin() const { return token_begin_
; }
157 const_iterator
token_end() const { return token_end_
; }
158 str
token() const { return str(token_begin_
, token_end_
); }
161 void Init(const_iterator string_begin
,
162 const_iterator string_end
,
164 start_pos_
= string_begin
;
165 token_end_
= string_begin
;
171 bool IsDelim(char_type c
) const {
172 return delims_
.find(c
) != str::npos
;
175 bool IsQuote(char_type c
) const {
176 return quotes_
.find(c
) != str::npos
;
179 struct AdvanceState
{
182 char_type quote_char
;
183 AdvanceState() : in_quote(false), in_escape(false) {}
186 // Returns true if a delimiter was not hit.
187 bool AdvanceOne(AdvanceState
* state
, char_type c
) {
188 if (state
->in_quote
) {
189 if (state
->in_escape
) {
190 state
->in_escape
= false;
191 } else if (c
== '\\') {
192 state
->in_escape
= true;
193 } else if (c
== state
->quote_char
) {
194 state
->in_quote
= false;
199 state
->in_quote
= IsQuote(state
->quote_char
= c
);
204 const_iterator start_pos_
;
205 const_iterator token_begin_
;
206 const_iterator token_end_
;
211 bool token_is_delim_
;
214 typedef StringTokenizerT
<std::string
, std::string::const_iterator
>
216 typedef StringTokenizerT
<std::wstring
, std::wstring::const_iterator
>
218 typedef StringTokenizerT
<std::string
, const char*> CStringTokenizer
;
220 #endif // BASE_STRING_TOKENIZER_H_