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