blob: 22d3628b30e9290d12155f9871d58b029942820b [file] [log] [blame]
license.botf003cfe2008-08-24 09:55:55 +09001// 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.
initial.commit3f4a7322008-07-27 06:49:38 +09004
5#ifndef BASE_STRING_TOKENIZER_H__
6#define BASE_STRING_TOKENIZER_H__
7
8#include <string>
9
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.
14//
15//
16// EXAMPLE 1:
17//
18// StringTokenizer t("this is a test", " ");
19// while (t.GetNext()) {
20// printf("%s\n", t.token().c_str());
21// }
22//
23// Output:
24//
25// this
26// is
27// a
28// test
29//
30//
31// EXAMPLE 2:
32//
33// StringTokenizer t("no-cache=\"foo, bar\", private", ", ");
34// t.set_quote_chars("\"");
35// while (t.GetNext()) {
36// printf("%s\n", t.token().c_str());
37// }
38//
39// Output:
40//
41// no-cache="foo, bar"
42// private
43//
44//
45// EXAMPLE 3:
46//
47// bool next_is_option = false, next_is_value = false;
48// std::string input = "text/html; charset=UTF-8; foo=bar";
49// StringTokenizer t(input, "; =");
50// t.set_options(StringTokenizer::RETURN_DELIMS);
51// while (t.GetNext()) {
52// if (t.token_is_delim()) {
53// switch (*t.token_begin()) {
54// case ';':
55// next_is_option = true;
56// break;
57// case '=':
58// next_is_value = true;
59// break;
60// }
61// } else {
62// const char* label;
63// if (next_is_option) {
64// label = "option-name";
65// next_is_option = false;
66// } else if (next_is_value) {
67// label = "option-value";
68// next_is_value = false;
69// } else {
70// label = "mime-type";
71// }
72// printf("%s: %s\n", label, t.token().c_str());
73// }
74// }
75//
76//
77template <class str>
78class StringTokenizerT {
79 public:
80 typedef typename str::const_iterator const_iterator;
81 typedef typename str::value_type char_type;
82
83 // Options that may be pass to set_options()
84 enum {
85 // Specifies the delimiters should be returned as tokens
86 RETURN_DELIMS = 1 << 0,
87 };
88
89 StringTokenizerT(const str& string,
90 const str& delims) {
91 Init(string.begin(), string.end(), delims);
92 }
93
94 StringTokenizerT(const_iterator string_begin,
95 const_iterator string_end,
96 const str& delims) {
97 Init(string_begin, string_end, delims);
98 }
99
100 // Set the options for this tokenizer. By default, this is 0.
101 void set_options(int options) { options_ = options; }
102
103 // Set the characters to regard as quotes. By default, this is empty. When
104 // a quote char is encountered, the tokenizer will switch into a mode where
105 // it ignores delimiters that it finds. It switches out of this mode once it
106 // finds another instance of the quote char. If a backslash is encountered
107 // within a quoted string, then the next character is skipped.
108 void set_quote_chars(const std::string& quotes) { quotes_ = quotes; }
109
110 // Call this method to advance the tokenizer to the next delimiter. This
111 // returns false if the tokenizer is complete. This method must be called
112 // before calling any of the token* methods.
113 bool GetNext() {
114 AdvanceState state;
115 token_is_delim_ = false;
116 for (;;) {
117 token_begin_ = token_end_;
118 if (token_end_ == end_)
119 return false;
120 ++token_end_;
121 if (AdvanceOne(&state, *token_begin_))
122 break;
123 if (options_ & RETURN_DELIMS) {
124 token_is_delim_ = true;
125 return true;
126 }
127 // else skip over delim
128 }
129 while (token_end_ != end_ && AdvanceOne(&state, *token_end_))
130 ++token_end_;
131 return true;
132 }
133
134 // Returns true if token is a delimiter. When the tokenizer is constructed
135 // with the RETURN_DELIMS option, this method can be used to check if the
136 // returned token is actually a delimiter.
137 bool token_is_delim() const { return token_is_delim_; }
138
139 // If GetNext() returned true, then these methods may be used to read the
140 // value of the token.
141 const_iterator token_begin() const { return token_begin_; }
142 const_iterator token_end() const { return token_end_; }
143 str token() const { return str(token_begin_, token_end_); }
144
145 private:
146 void Init(const_iterator string_begin,
147 const_iterator string_end,
148 const str& delims) {
149 token_end_ = string_begin;
150 end_ = string_end;
151 delims_ = delims;
152 options_ = 0;
153 }
154
155 bool IsDelim(char_type c) const {
156 return delims_.find(c) != str::npos;
157 }
158
159 bool IsQuote(char_type c) const {
160 return quotes_.find(c) != str::npos;
161 }
162
163 struct AdvanceState {
164 bool in_quote;
165 bool in_escape;
166 char_type quote_char;
167 AdvanceState() : in_quote(false), in_escape(false) {}
168 };
169
170 // Returns true if a delimiter was not hit.
171 bool AdvanceOne(AdvanceState* state, char_type c) {
172 if (state->in_quote) {
173 if (state->in_escape) {
174 state->in_escape = false;
175 } else if (c == '\\') {
176 state->in_escape = true;
177 } else if (c == state->quote_char) {
178 state->in_quote = false;
179 }
180 } else {
181 if (IsDelim(c))
182 return false;
183 state->in_quote = IsQuote(state->quote_char = c);
184 }
185 return true;
186 }
187
188 const_iterator token_begin_;
189 const_iterator token_end_;
190 const_iterator end_;
191 str delims_;
192 str quotes_;
193 int options_;
194 bool token_is_delim_;
195};
196
197typedef StringTokenizerT<std::string> StringTokenizer;
198typedef StringTokenizerT<std::wstring> WStringTokenizer;
199
200#endif // BASE_STRING_TOKENIZER_H__
license.botf003cfe2008-08-24 09:55:55 +0900201