summaryrefslogtreecommitdiffstats
path: root/base/string_tokenizer.h
diff options
context:
space:
mode:
authormattm@chromium.org <mattm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-08-26 01:19:43 +0000
committermattm@chromium.org <mattm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-08-26 01:19:43 +0000
commit96690a420b73c1b5a4ae350a1a13963b90dabbec (patch)
tree7870d1347381cb41be4be98fa3a29ddd67629859 /base/string_tokenizer.h
parent6ee15630603b46b24c02dcf4d4cf8b8412af52aa (diff)
downloadchromium_src-96690a420b73c1b5a4ae350a1a13963b90dabbec.zip
chromium_src-96690a420b73c1b5a4ae350a1a13963b90dabbec.tar.gz
chromium_src-96690a420b73c1b5a4ae350a1a13963b90dabbec.tar.bz2
Fix cases that initialized StringTokenizer with a temporary.
Fix examples in StringTokenizer header that recommended doing that. BUG=none TEST=on linux, open options, click proxy configuration button a bunch. It should not fail intermittently. Review URL: http://codereview.chromium.org/174490 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@24398 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/string_tokenizer.h')
-rw-r--r--base/string_tokenizer.h15
1 files changed, 13 insertions, 2 deletions
diff --git a/base/string_tokenizer.h b/base/string_tokenizer.h
index 654e7c9..eb9ac2d 100644
--- a/base/string_tokenizer.h
+++ b/base/string_tokenizer.h
@@ -12,10 +12,18 @@
// refer to the next token in the input string. The user may optionally
// configure the tokenizer to return delimiters.
//
+// Warning: be careful not to pass a C string into the 2-arg constructor:
+// StringTokenizer t("this is a test", " "); // WRONG
+// This will create a temporary std::string, save the begin() and end()
+// iterators, and then the string will be freed before we actually start
+// tokenizing it.
+// Instead, use a std::string or use the 3 arg constructor of CStringTokenizer.
+//
//
// EXAMPLE 1:
//
-// StringTokenizer t("this is a test", " ");
+// char input[] = "this is a test";
+// CStringTokenizer t(input, input + strlen(input), " ");
// while (t.GetNext()) {
// printf("%s\n", t.token().c_str());
// }
@@ -30,7 +38,8 @@
//
// EXAMPLE 2:
//
-// StringTokenizer t("no-cache=\"foo, bar\", private", ", ");
+// std::string input = "no-cache=\"foo, bar\", private";
+// StringTokenizer t(input, ", ");
// t.set_quote_chars("\"");
// while (t.GetNext()) {
// printf("%s\n", t.token().c_str());
@@ -85,6 +94,8 @@ class StringTokenizerT {
RETURN_DELIMS = 1 << 0,
};
+ // The string object must live longer than the tokenizer. (In particular this
+ // should not be constructed with a temporary.)
StringTokenizerT(const str& string,
const str& delims) {
Init(string.begin(), string.end(), delims);