diff options
author | tfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-09-02 02:01:48 +0000 |
---|---|---|
committer | tfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-09-02 02:01:48 +0000 |
commit | e8478ae4ef8bd1dbb83cd46e8af060d5331ba2df (patch) | |
tree | f1b969244d1ffa41c32242429e9218ccd4955fb8 | |
parent | ee940fc4997e1e0369f8e9fd1cce24311e1945a0 (diff) | |
download | chromium_src-e8478ae4ef8bd1dbb83cd46e8af060d5331ba2df.zip chromium_src-e8478ae4ef8bd1dbb83cd46e8af060d5331ba2df.tar.gz chromium_src-e8478ae4ef8bd1dbb83cd46e8af060d5331ba2df.tar.bz2 |
base: Move SplitStringUsingSubstr functions from string_util.h to string_split.h
BUG=None
TEST=trybots
Review URL: http://codereview.chromium.org/3284005
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@58300 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | base/string_split.cc | 34 | ||||
-rw-r--r-- | base/string_split.h | 10 | ||||
-rw-r--r-- | base/string_split_unittest.cc | 48 | ||||
-rw-r--r-- | base/string_util.cc | 34 | ||||
-rw-r--r-- | base/string_util.h | 8 | ||||
-rw-r--r-- | base/string_util_unittest.cc | 45 | ||||
-rw-r--r-- | net/http/http_request_headers.cc | 4 |
7 files changed, 95 insertions, 88 deletions
diff --git a/base/string_split.cc b/base/string_split.cc index dc7e7ad..b2b70ea 100644 --- a/base/string_split.cc +++ b/base/string_split.cc @@ -71,4 +71,38 @@ bool SplitStringIntoKeyValuePairs( return success; } +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); +} + } // namespace base diff --git a/base/string_split.h b/base/string_split.h index d36a71f..4ded704 100644 --- a/base/string_split.h +++ b/base/string_split.h @@ -10,6 +10,8 @@ #include <utility> #include <vector> +#include "base/string16.h" + namespace base { bool SplitStringIntoKeyValues( @@ -23,6 +25,14 @@ bool SplitStringIntoKeyValuePairs( char key_value_pair_delimiter, std::vector<std::pair<std::string, std::string> >* kv_pairs); +// 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); + } // namespace base #endif // BASE_STRING_SPLIT_H diff --git a/base/string_split_unittest.cc b/base/string_split_unittest.cc index 820f74b..f696480b 100644 --- a/base/string_split_unittest.cc +++ b/base/string_split_unittest.cc @@ -3,8 +3,11 @@ // found in the LICENSE file. #include "base/string_split.h" +#include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" +using ::testing::ElementsAre; + namespace base { class SplitStringIntoKeyValuesTest : public testing::Test { @@ -129,4 +132,49 @@ TEST_F(SplitStringIntoKeyValuePairsTest, DelimiterInValue) { EXPECT_EQ("value2", kv_pairs[1].second); } +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 diff --git a/base/string_util.cc b/base/string_util.cc index 56aa39d..a7f5258 100644 --- a/base/string_util.cc +++ b/base/string_util.cc @@ -821,40 +821,6 @@ 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); -} - 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 ca028e1..7788562 100644 --- a/base/string_util.h +++ b/base/string_util.h @@ -534,14 +534,6 @@ 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 b317d83..f0524bee 100644 --- a/base/string_util_unittest.cc +++ b/base/string_util_unittest.cc @@ -1230,49 +1230,4 @@ TEST(StringUtilTest, ContainsOnlyChars) { EXPECT_FALSE(ContainsOnlyChars("123a", "4321")); } -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 diff --git a/net/http/http_request_headers.cc b/net/http/http_request_headers.cc index c534967..9f2eb90 100644 --- a/net/http/http_request_headers.cc +++ b/net/http/http_request_headers.cc @@ -5,6 +5,7 @@ #include "net/http/http_request_headers.h" #include "base/logging.h" +#include "base/string_split.h" #include "base/string_util.h" #include "net/http/http_util.h" @@ -139,7 +140,8 @@ void HttpRequestHeaders::AddHeadersFromString( // TODO(willchan): Consider adding more StringPiece support in string_util.h // to eliminate copies. std::vector<std::string> header_line_vector; - SplitStringUsingSubstr(headers.as_string(), "\r\n", &header_line_vector); + base::SplitStringUsingSubstr(headers.as_string(), "\r\n", + &header_line_vector); for (std::vector<std::string>::const_iterator it = header_line_vector.begin(); it != header_line_vector.end(); ++it) { if (!it->empty()) |