diff options
author | tfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-09-24 04:52:11 +0000 |
---|---|---|
committer | tfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-09-24 04:52:11 +0000 |
commit | 4e5ae20ff403cdcd14d3b86b18a0d0bcec2f47cc (patch) | |
tree | e224d7b5d871995421ac845db1c9406393b66f67 /base | |
parent | 915a3abe017a65e6f3bcc76d2968df476efc4a6e (diff) | |
download | chromium_src-4e5ae20ff403cdcd14d3b86b18a0d0bcec2f47cc.zip chromium_src-4e5ae20ff403cdcd14d3b86b18a0d0bcec2f47cc.tar.gz chromium_src-4e5ae20ff403cdcd14d3b86b18a0d0bcec2f47cc.tar.bz2 |
base: Finish moving the SplitString functions from string_util.h to string_split.h
BUG=None
TEST=trybos
Review URL: http://codereview.chromium.org/3447008
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@60422 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/command_line.cc | 1 | ||||
-rw-r--r-- | base/process_util_linux.cc | 1 | ||||
-rw-r--r-- | base/string_split.cc | 72 | ||||
-rw-r--r-- | base/string_split.h | 27 | ||||
-rw-r--r-- | base/string_split_unittest.cc | 67 | ||||
-rw-r--r-- | base/string_util.cc | 45 | ||||
-rw-r--r-- | base/string_util.h | 25 | ||||
-rw-r--r-- | base/string_util_unittest.cc | 67 | ||||
-rw-r--r-- | base/version.cc | 3 |
9 files changed, 151 insertions, 157 deletions
diff --git a/base/command_line.cc b/base/command_line.cc index d34bcb9..a68c8f3 100644 --- a/base/command_line.cc +++ b/base/command_line.cc @@ -21,6 +21,7 @@ #include "base/file_path.h" #include "base/logging.h" #include "base/singleton.h" +#include "base/string_split.h" #include "base/string_util.h" #include "base/sys_string_conversions.h" #include "base/utf_string_conversions.h" diff --git a/base/process_util_linux.cc b/base/process_util_linux.cc index 8b8a0c7..43e1a5c 100644 --- a/base/process_util_linux.cc +++ b/base/process_util_linux.cc @@ -18,6 +18,7 @@ #include "base/file_util.h" #include "base/logging.h" #include "base/string_number_conversions.h" +#include "base/string_split.h" #include "base/string_tokenizer.h" #include "base/string_util.h" #include "base/sys_info.h" diff --git a/base/string_split.cc b/base/string_split.cc index 646b4de..ca97954 100644 --- a/base/string_split.cc +++ b/base/string_split.cc @@ -9,6 +9,52 @@ #include "base/third_party/icu/icu_utf.h" #include "base/utf_string_conversions.h" +template<typename STR> +static void SplitStringT(const STR& str, + const typename STR::value_type s, + bool trim_whitespace, + std::vector<STR>* r) { + size_t last = 0; + size_t i; + size_t c = str.size(); + for (i = 0; i <= c; ++i) { + if (i == c || str[i] == s) { + size_t len = i - last; + STR tmp = str.substr(last, len); + if (trim_whitespace) { + STR t_tmp; + TrimWhitespace(tmp, TRIM_ALL, &t_tmp); + r->push_back(t_tmp); + } else { + r->push_back(tmp); + } + last = i + 1; + } + } +} + +void SplitString(const std::wstring& str, + wchar_t c, + std::vector<std::wstring>* r) { + SplitStringT(str, c, true, r); +} + +#if !defined(WCHAR_T_IS_UTF16) +void SplitString(const string16& str, + char16 c, + std::vector<string16>* r) { + DCHECK(CBU16_IS_SINGLE(c)); + SplitStringT(str, c, true, r); +} +#endif + +void SplitString(const std::string& str, + char c, + std::vector<std::string>* r) { + DCHECK(c >= 0 && c < 0x7F); + SplitStringT(str, c, true, r); +} + namespace base { bool SplitStringIntoKeyValues( @@ -107,30 +153,6 @@ void SplitStringUsingSubstr(const std::string& str, SplitStringUsingSubstrT(str, s, r); } -template<typename STR> -static void SplitStringT(const STR& str, - const typename STR::value_type s, - bool trim_whitespace, - std::vector<STR>* r) { - size_t last = 0; - size_t i; - size_t c = str.size(); - for (i = 0; i <= c; ++i) { - if (i == c || str[i] == s) { - size_t len = i - last; - STR tmp = str.substr(last, len); - if (trim_whitespace) { - STR t_tmp; - TrimWhitespace(tmp, TRIM_ALL, &t_tmp); - r->push_back(t_tmp); - } else { - r->push_back(tmp); - } - last = i + 1; - } - } -} - void SplitStringDontTrim(const std::wstring& str, wchar_t c, std::vector<std::wstring>* r) { @@ -150,7 +172,7 @@ void SplitStringDontTrim(const std::string& str, char c, std::vector<std::string>* r) { DCHECK(IsStringUTF8(str)); - DCHECK(c < 0x7F); + DCHECK(c >= 0 && c < 0x7F); SplitStringT(str, c, false, r); } diff --git a/base/string_split.h b/base/string_split.h index d47337c..c7cb5e7 100644 --- a/base/string_split.h +++ b/base/string_split.h @@ -12,6 +12,33 @@ #include "base/string16.h" +// TODO(tfarina): Move the following functions into the namespace and update the +// callers. +//----------------------------------------------------------------------------- + +// Splits |str| into a vector of strings delimited by |s|. Append the results +// into |r| as they appear. If several instances of |s| are contiguous, or if +// |str| begins with or ends with |s|, then an empty string is inserted. +// +// Every substring is trimmed of any leading or trailing white space. +// Where wchar_t is char16 (i.e. Windows), |c| must be in BMP +// (Basic Multilingual Plane). Elsewhere (Linux/Mac), wchar_t +// should be a valid Unicode code point (32-bit). +void SplitString(const std::wstring& str, + wchar_t c, + std::vector<std::wstring>* r); +// NOTE: |c| must be in BMP (Basic Multilingual Plane) +void SplitString(const string16& str, + char16 c, + std::vector<string16>* r); +// |str| should not be in a multi-byte encoding like Shift-JIS or GBK in which +// the trailing byte of a multi-byte character can be in the ASCII range. +// UTF-8, and other single/multi-byte ASCII-compatible encodings are OK. +// Note: |c| must be in the ASCII range. +void SplitString(const std::string& str, + char c, + std::vector<std::string>* r); + namespace base { bool SplitStringIntoKeyValues( diff --git a/base/string_split_unittest.cc b/base/string_split_unittest.cc index f3a929d..d4042ad 100644 --- a/base/string_split_unittest.cc +++ b/base/string_split_unittest.cc @@ -139,6 +139,73 @@ TEST(SplitStringUsingSubstrTest, EmptyString) { EXPECT_THAT(results, ElementsAre("")); } +// Test for SplitString +TEST(StringUtilTest, SplitString) { + std::vector<std::wstring> r; + + SplitString(L"", L',', &r); + ASSERT_EQ(1U, r.size()); + EXPECT_EQ(r[0], L""); + r.clear(); + + SplitString(L"a,b,c", L',', &r); + ASSERT_EQ(3U, r.size()); + EXPECT_EQ(r[0], L"a"); + EXPECT_EQ(r[1], L"b"); + EXPECT_EQ(r[2], L"c"); + r.clear(); + + SplitString(L"a, b, c", L',', &r); + ASSERT_EQ(3U, r.size()); + EXPECT_EQ(r[0], L"a"); + EXPECT_EQ(r[1], L"b"); + EXPECT_EQ(r[2], L"c"); + r.clear(); + + SplitString(L"a,,c", L',', &r); + ASSERT_EQ(3U, r.size()); + EXPECT_EQ(r[0], L"a"); + EXPECT_EQ(r[1], L""); + EXPECT_EQ(r[2], L"c"); + r.clear(); + + SplitString(L"", L'*', &r); + ASSERT_EQ(1U, r.size()); + EXPECT_EQ(r[0], L""); + r.clear(); + + SplitString(L"foo", L'*', &r); + ASSERT_EQ(1U, r.size()); + EXPECT_EQ(r[0], L"foo"); + r.clear(); + + SplitString(L"foo ,", L',', &r); + ASSERT_EQ(2U, r.size()); + EXPECT_EQ(r[0], L"foo"); + EXPECT_EQ(r[1], L""); + r.clear(); + + SplitString(L",", L',', &r); + ASSERT_EQ(2U, r.size()); + EXPECT_EQ(r[0], L""); + EXPECT_EQ(r[1], L""); + r.clear(); + + SplitString(L"\t\ta\t", L'\t', &r); + ASSERT_EQ(4U, r.size()); + EXPECT_EQ(r[0], L""); + EXPECT_EQ(r[1], L""); + EXPECT_EQ(r[2], L"a"); + EXPECT_EQ(r[3], L""); + r.clear(); + + SplitString(L"\ta\t\nb\tcc", L'\n', &r); + ASSERT_EQ(2U, r.size()); + EXPECT_EQ(r[0], L"a"); + EXPECT_EQ(r[1], L"b\tcc"); + r.clear(); +} + TEST(SplitStringUsingSubstrTest, StringWithNoDelimiter) { std::vector<std::string> results; SplitStringUsingSubstr("alongwordwithnodelimiter", "DELIMITER", &results); diff --git a/base/string_util.cc b/base/string_util.cc index ba9108b..c30db96 100644 --- a/base/string_util.cc +++ b/base/string_util.cc @@ -757,51 +757,6 @@ void ReplaceSubstringsAfterOffset(std::string* str, true); // replace all instances } -// TODO(tfarina): Remove this when finish moving SplitString functions to -// string_split.[cc/h]. -template<typename STR> -static void SplitStringT(const STR& str, - const typename STR::value_type s, - bool trim_whitespace, - std::vector<STR>* r) { - size_t last = 0; - size_t i; - size_t c = str.size(); - for (i = 0; i <= c; ++i) { - if (i == c || str[i] == s) { - size_t len = i - last; - STR tmp = str.substr(last, len); - if (trim_whitespace) { - STR t_tmp; - TrimWhitespace(tmp, TRIM_ALL, &t_tmp); - r->push_back(t_tmp); - } else { - r->push_back(tmp); - } - last = i + 1; - } - } -} - -void SplitString(const std::wstring& str, - wchar_t s, - std::vector<std::wstring>* r) { - SplitStringT(str, s, true, r); -} - -#if !defined(WCHAR_T_IS_UTF16) -void SplitString(const string16& str, - char16 s, - std::vector<string16>* r) { - SplitStringT(str, s, true, r); -} -#endif - -void SplitString(const std::string& str, - char s, - std::vector<std::string>* r) { - SplitStringT(str, s, true, r); -} template<typename STR> static size_t TokenizeT(const STR& str, diff --git a/base/string_util.h b/base/string_util.h index b1cb828..1b279a0 100644 --- a/base/string_util.h +++ b/base/string_util.h @@ -23,6 +23,12 @@ // and then remove this. #include "base/stringprintf.h" +#ifdef RLZ_WIN_LIB_RLZ_LIB_H_ +// TODO(tfarina): Fix the rlz library to include this instead and remove +// this include. +#include "base/string_split.h" +#endif // RLZ_WIN_LIB_RLZ_LIB_H_ + // Safe standard library wrappers for all platforms. namespace base { @@ -504,25 +510,6 @@ template<typename Char> struct CaseInsensitiveCompareASCII { } }; -// TODO(timsteele): Move these split string functions into their own API on -// string_split.cc/.h files. -//----------------------------------------------------------------------------- - -// Splits |str| into a vector of strings delimited by |s|. Append the results -// into |r| as they appear. If several instances of |s| are contiguous, or if -// |str| begins with or ends with |s|, then an empty string is inserted. -// -// Every substring is trimmed of any leading or trailing white space. -void SplitString(const std::wstring& str, - wchar_t s, - std::vector<std::wstring>* r); -void SplitString(const string16& str, - char16 s, - std::vector<string16>* r); -void SplitString(const std::string& str, - char 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 c4fd5ae..8163ba0 100644 --- a/base/string_util_unittest.cc +++ b/base/string_util_unittest.cc @@ -702,73 +702,6 @@ TEST(StringUtilTest, VAList) { VariableArgsFunc("%d %d %s %lf", 45, 92, "This is interesting", 9.21); } -// Test for SplitString -TEST(StringUtilTest, SplitString) { - std::vector<std::wstring> r; - - SplitString(L"", L',', &r); - ASSERT_EQ(1U, r.size()); - EXPECT_EQ(r[0], L""); - r.clear(); - - SplitString(L"a,b,c", L',', &r); - ASSERT_EQ(3U, r.size()); - EXPECT_EQ(r[0], L"a"); - EXPECT_EQ(r[1], L"b"); - EXPECT_EQ(r[2], L"c"); - r.clear(); - - SplitString(L"a, b, c", L',', &r); - ASSERT_EQ(3U, r.size()); - EXPECT_EQ(r[0], L"a"); - EXPECT_EQ(r[1], L"b"); - EXPECT_EQ(r[2], L"c"); - r.clear(); - - SplitString(L"a,,c", L',', &r); - ASSERT_EQ(3U, r.size()); - EXPECT_EQ(r[0], L"a"); - EXPECT_EQ(r[1], L""); - EXPECT_EQ(r[2], L"c"); - r.clear(); - - SplitString(L"", L'*', &r); - ASSERT_EQ(1U, r.size()); - EXPECT_EQ(r[0], L""); - r.clear(); - - SplitString(L"foo", L'*', &r); - ASSERT_EQ(1U, r.size()); - EXPECT_EQ(r[0], L"foo"); - r.clear(); - - SplitString(L"foo ,", L',', &r); - ASSERT_EQ(2U, r.size()); - EXPECT_EQ(r[0], L"foo"); - EXPECT_EQ(r[1], L""); - r.clear(); - - SplitString(L",", L',', &r); - ASSERT_EQ(2U, r.size()); - EXPECT_EQ(r[0], L""); - EXPECT_EQ(r[1], L""); - r.clear(); - - SplitString(L"\t\ta\t", L'\t', &r); - ASSERT_EQ(4U, r.size()); - EXPECT_EQ(r[0], L""); - EXPECT_EQ(r[1], L""); - EXPECT_EQ(r[2], L"a"); - EXPECT_EQ(r[3], L""); - r.clear(); - - SplitString(L"\ta\t\nb\tcc", L'\n', &r); - ASSERT_EQ(2U, r.size()); - EXPECT_EQ(r[0], L"a"); - EXPECT_EQ(r[1], L"b\tcc"); - r.clear(); -} - // Test for Tokenize template <typename STR> void TokenizeTest() { diff --git a/base/version.cc b/base/version.cc index c784082..8a09142 100644 --- a/base/version.cc +++ b/base/version.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Copyright (c) 2010 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -6,6 +6,7 @@ #include "base/logging.h" #include "base/string_number_conversions.h" +#include "base/string_split.h" #include "base/string_util.h" #include "base/utf_string_conversions.h" |