diff options
author | willchan@chromium.org <willchan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-23 20:25:20 +0000 |
---|---|---|
committer | willchan@chromium.org <willchan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-23 20:25:20 +0000 |
commit | aa98311d5083e2505e1e089bb21ed3735d4623f8 (patch) | |
tree | 6b771930d2f86a7c3d804b610004a6aab9f9bd5b /base | |
parent | 6b889fb5d046cdecc27c2b2d56e2400dd7c10270 (diff) | |
download | chromium_src-aa98311d5083e2505e1e089bb21ed3735d4623f8.zip chromium_src-aa98311d5083e2505e1e089bb21ed3735d4623f8.tar.gz chromium_src-aa98311d5083e2505e1e089bb21ed3735d4623f8.tar.bz2 |
Reland r42300: "HttpRequestHeaders refactor.""
This time, make sure HttpRequestHeaders::FindHeader() checks key lengths are equal.
* Create HttpRequestHeaders.
* Switch HttpNetworkTransaction to build request headers.
TODO: Change extra_headers to use HttpRequestHeaders.
BUG=22588
Review URL: http://codereview.chromium.org/1110014
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@42376 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/string_util.cc | 48 | ||||
-rw-r--r-- | base/string_util.h | 8 | ||||
-rw-r--r-- | base/string_util_unittest.cc | 50 |
3 files changed, 105 insertions, 1 deletions
diff --git a/base/string_util.cc b/base/string_util.cc index 19c1735..494d09d 100644 --- a/base/string_util.cc +++ b/base/string_util.cc @@ -1324,6 +1324,54 @@ void SplitStringDontTrim(const std::string& str, SplitStringT(str, s, false, r); } +template <typename STR> +static void SplitStringUsingSubstrT(const STR& str, + const STR& s, + std::vector<STR>* r) { + typename STR::size_type begin_index = 0; + while (true) { + const typename STR::size_type end_index = str.find(s, begin_index); + if (end_index == STR::npos) { + const STR term = str.substr(begin_index); + STR tmp; + TrimWhitespace(term, TRIM_ALL, &tmp); + r->push_back(tmp); + return; + } + const STR term = str.substr(begin_index, end_index - begin_index); + STR tmp; + TrimWhitespace(term, TRIM_ALL, &tmp); + r->push_back(tmp); + begin_index = end_index + s.size(); + } +} + +void SplitStringUsingSubstr(const string16& str, + const string16& s, + std::vector<string16>* r) { + SplitStringUsingSubstrT(str, s, r); +} + +void SplitStringUsingSubstr(const std::string& str, + const std::string& s, + std::vector<std::string>* r) { + SplitStringUsingSubstrT(str, s, r); +} + +std::vector<string16> SplitStringUsingSubstr(const string16& str, + const string16& s) { + std::vector<string16> result; + SplitStringUsingSubstr(str, s, &result); + return result; +} + +std::vector<std::string> SplitStringUsingSubstr(const std::string& str, + const std::string& s) { + std::vector<std::string> result; + SplitStringUsingSubstr(str, s, &result); + return result; +} + template<typename STR> static size_t TokenizeT(const STR& str, const STR& delimiters, diff --git a/base/string_util.h b/base/string_util.h index 586c60c..28cd26f 100644 --- a/base/string_util.h +++ b/base/string_util.h @@ -575,6 +575,14 @@ void SplitStringDontTrim(const std::string& str, char s, std::vector<std::string>* r); +// The same as SplitString, but use a substring delimiter instead of a char. +void SplitStringUsingSubstr(const string16& str, + const string16& s, + std::vector<string16>* r); +void SplitStringUsingSubstr(const std::string& str, + const std::string& s, + std::vector<std::string>* r); + // Splits a string into its fields delimited by any of the characters in // |delimiters|. Each field is added to the |tokens| vector. Returns the // number of tokens found. diff --git a/base/string_util_unittest.cc b/base/string_util_unittest.cc index 78939f5..8fc8f15 100644 --- a/base/string_util_unittest.cc +++ b/base/string_util_unittest.cc @@ -11,8 +11,11 @@ #include "base/basictypes.h" #include "base/string_util.h" #include "base/utf_string_conversions.h" +#include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" +using ::testing::ElementsAre; + namespace base { namespace { @@ -1492,4 +1495,49 @@ TEST(StringUtilTest, ContainsOnlyChars) { EXPECT_FALSE(ContainsOnlyChars("123a", "4321")); } -} // base +TEST(SplitStringUsingSubstrTest, EmptyString) { + std::vector<std::string> results; + SplitStringUsingSubstr("", "DELIMITER", &results); + ASSERT_EQ(1u, results.size()); + EXPECT_THAT(results, ElementsAre("")); +} + +TEST(SplitStringUsingSubstrTest, StringWithNoDelimiter) { + std::vector<std::string> results; + SplitStringUsingSubstr("alongwordwithnodelimiter", "DELIMITER", &results); + ASSERT_EQ(1u, results.size()); + EXPECT_THAT(results, ElementsAre("alongwordwithnodelimiter")); +} + +TEST(SplitStringUsingSubstrTest, LeadingDelimitersSkipped) { + std::vector<std::string> results; + SplitStringUsingSubstr( + "DELIMITERDELIMITERDELIMITERoneDELIMITERtwoDELIMITERthree", + "DELIMITER", + &results); + ASSERT_EQ(6u, results.size()); + EXPECT_THAT(results, ElementsAre("", "", "", "one", "two", "three")); +} + +TEST(SplitStringUsingSubstrTest, ConsecutiveDelimitersSkipped) { + std::vector<std::string> results; + SplitStringUsingSubstr( + "unoDELIMITERDELIMITERDELIMITERdosDELIMITERtresDELIMITERDELIMITERcuatro", + "DELIMITER", + &results); + ASSERT_EQ(7u, results.size()); + EXPECT_THAT(results, ElementsAre("uno", "", "", "dos", "tres", "", "cuatro")); +} + +TEST(SplitStringUsingSubstrTest, TrailingDelimitersSkipped) { + std::vector<std::string> results; + SplitStringUsingSubstr( + "unDELIMITERdeuxDELIMITERtroisDELIMITERquatreDELIMITERDELIMITERDELIMITER", + "DELIMITER", + &results); + ASSERT_EQ(7u, results.size()); + EXPECT_THAT( + results, ElementsAre("un", "deux", "trois", "quatre", "", "", "")); +} + +} // namespace base |