Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(40)

Side by Side Diff: base/string_tokenizer.h

Issue 174490: Fix cases that initialized StringTokenizer with a temporary. (Closed)
Patch Set: check for null path Created 11 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | chrome/browser/gtk/options/advanced_contents_gtk.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef BASE_STRING_TOKENIZER_H_ 5 #ifndef BASE_STRING_TOKENIZER_H_
6 #define BASE_STRING_TOKENIZER_H_ 6 #define BASE_STRING_TOKENIZER_H_
7 7
8 #include <string> 8 #include <string>
9 9
10 // StringTokenizerT is a simple string tokenizer class. It works like an 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 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 12 // refer to the next token in the input string. The user may optionally
13 // configure the tokenizer to return delimiters. 13 // configure the tokenizer to return delimiters.
14 // 14 //
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
19 // tokenizing it.
20 // Instead, use a std::string or use the 3 arg constructor of CStringTokenizer.
21 //
15 // 22 //
16 // EXAMPLE 1: 23 // EXAMPLE 1:
17 // 24 //
18 // StringTokenizer t("this is a test", " "); 25 // char input[] = "this is a test";
26 // CStringTokenizer t(input, input + strlen(input), " ");
19 // while (t.GetNext()) { 27 // while (t.GetNext()) {
20 // printf("%s\n", t.token().c_str()); 28 // printf("%s\n", t.token().c_str());
21 // } 29 // }
22 // 30 //
23 // Output: 31 // Output:
24 // 32 //
25 // this 33 // this
26 // is 34 // is
27 // a 35 // a
28 // test 36 // test
29 // 37 //
30 // 38 //
31 // EXAMPLE 2: 39 // EXAMPLE 2:
32 // 40 //
33 // StringTokenizer t("no-cache=\"foo, bar\", private", ", "); 41 // std::string input = "no-cache=\"foo, bar\", private";
42 // StringTokenizer t(input, ", ");
34 // t.set_quote_chars("\""); 43 // t.set_quote_chars("\"");
35 // while (t.GetNext()) { 44 // while (t.GetNext()) {
36 // printf("%s\n", t.token().c_str()); 45 // printf("%s\n", t.token().c_str());
37 // } 46 // }
38 // 47 //
39 // Output: 48 // Output:
40 // 49 //
41 // no-cache="foo, bar" 50 // no-cache="foo, bar"
42 // private 51 // private
43 // 52 //
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 class StringTokenizerT { 87 class StringTokenizerT {
79 public: 88 public:
80 typedef typename str::value_type char_type; 89 typedef typename str::value_type char_type;
81 90
82 // Options that may be pass to set_options() 91 // Options that may be pass to set_options()
83 enum { 92 enum {
84 // Specifies the delimiters should be returned as tokens 93 // Specifies the delimiters should be returned as tokens
85 RETURN_DELIMS = 1 << 0, 94 RETURN_DELIMS = 1 << 0,
86 }; 95 };
87 96
97 // The string object must live longer than the tokenizer. (In particular this
98 // should not be constructed with a temporary.)
88 StringTokenizerT(const str& string, 99 StringTokenizerT(const str& string,
89 const str& delims) { 100 const str& delims) {
90 Init(string.begin(), string.end(), delims); 101 Init(string.begin(), string.end(), delims);
91 } 102 }
92 103
93 StringTokenizerT(const_iterator string_begin, 104 StringTokenizerT(const_iterator string_begin,
94 const_iterator string_end, 105 const_iterator string_end,
95 const str& delims) { 106 const str& delims) {
96 Init(string_begin, string_end, delims); 107 Init(string_begin, string_end, delims);
97 } 108 }
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 bool token_is_delim_; 204 bool token_is_delim_;
194 }; 205 };
195 206
196 typedef StringTokenizerT<std::string, std::string::const_iterator> 207 typedef StringTokenizerT<std::string, std::string::const_iterator>
197 StringTokenizer; 208 StringTokenizer;
198 typedef StringTokenizerT<std::wstring, std::wstring::const_iterator> 209 typedef StringTokenizerT<std::wstring, std::wstring::const_iterator>
199 WStringTokenizer; 210 WStringTokenizer;
200 typedef StringTokenizerT<std::string, const char*> CStringTokenizer; 211 typedef StringTokenizerT<std::string, const char*> CStringTokenizer;
201 212
202 #endif // BASE_STRING_TOKENIZER_H_ 213 #endif // BASE_STRING_TOKENIZER_H_
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/gtk/options/advanced_contents_gtk.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698