diff options
author | tfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-09-15 13:36:22 +0000 |
---|---|---|
committer | tfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-09-15 13:36:22 +0000 |
commit | 7594f6d81259f9a299e50c4174af080bb5e0b288 (patch) | |
tree | 8f4a3e06e80b59421f7a566effc8a5fb1f95960e /base | |
parent | 769c462e0ffbbbc2f210a76874d041dbcff921fc (diff) | |
download | chromium_src-7594f6d81259f9a299e50c4174af080bb5e0b288.zip chromium_src-7594f6d81259f9a299e50c4174af080bb5e0b288.tar.gz chromium_src-7594f6d81259f9a299e50c4174af080bb5e0b288.tar.bz2 |
base: Move SplitStringDontTrim functions from string_util.h to string_split.h
BUG=None
TEST=trybots
Review URL: http://codereview.chromium.org/3366011
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@59493 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/mime_util_xdg.cc | 5 | ||||
-rw-r--r-- | base/string_split.cc | 49 | ||||
-rw-r--r-- | base/string_split.h | 19 | ||||
-rw-r--r-- | base/string_split_unittest.cc | 18 | ||||
-rw-r--r-- | base/string_util.cc | 22 | ||||
-rw-r--r-- | base/string_util.h | 11 | ||||
-rw-r--r-- | base/string_util_unittest.cc | 14 |
7 files changed, 91 insertions, 47 deletions
diff --git a/base/mime_util_xdg.cc b/base/mime_util_xdg.cc index d00a568..5dc4960 100644 --- a/base/mime_util_xdg.cc +++ b/base/mime_util_xdg.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. @@ -18,6 +18,7 @@ #include "base/message_loop.h" #include "base/scoped_ptr.h" #include "base/singleton.h" +#include "base/string_split.h" #include "base/string_util.h" #include "base/third_party/xdg_mime/xdgmime.h" @@ -272,7 +273,7 @@ bool IconTheme::LoadIndexTheme(const FilePath& file) { std::string key, value; std::vector<std::string> r; - SplitStringDontTrim(entry, '=', &r); + base::SplitStringDontTrim(entry, '=', &r); if (r.size() < 2) continue; diff --git a/base/string_split.cc b/base/string_split.cc index b2b70ea..646b4de 100644 --- a/base/string_split.cc +++ b/base/string_split.cc @@ -6,6 +6,8 @@ #include "base/logging.h" #include "base/string_util.h" +#include "base/third_party/icu/icu_utf.h" +#include "base/utf_string_conversions.h" namespace base { @@ -105,4 +107,51 @@ 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) { + SplitStringT(str, c, false, r); +} + +#if !defined(WCHAR_T_IS_UTF16) +void SplitStringDontTrim(const string16& str, + char16 c, + std::vector<string16>* r) { + DCHECK(CBU16_IS_SINGLE(c)); + SplitStringT(str, c, false, r); +} +#endif + +void SplitStringDontTrim(const std::string& str, + char c, + std::vector<std::string>* r) { + DCHECK(IsStringUTF8(str)); + DCHECK(c < 0x7F); + SplitStringT(str, c, false, r); +} + } // namespace base diff --git a/base/string_split.h b/base/string_split.h index 4ded704..d47337c 100644 --- a/base/string_split.h +++ b/base/string_split.h @@ -33,6 +33,25 @@ void SplitStringUsingSubstr(const std::string& str, const std::string& s, std::vector<std::string>* r); +// The same as SplitString, but don't trim 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 SplitStringDontTrim(const std::wstring& str, + wchar_t c, + std::vector<std::wstring>* r); +// NOTE: |c| must be in BMP (Basic Multilingual Plane) +void SplitStringDontTrim(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 SplitStringDontTrim(const std::string& str, + char c, + 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 f696480b..f3a929d 100644 --- a/base/string_split_unittest.cc +++ b/base/string_split_unittest.cc @@ -177,4 +177,22 @@ TEST(SplitStringUsingSubstrTest, TrailingDelimitersSkipped) { results, ElementsAre("un", "deux", "trois", "quatre", "", "", "")); } +TEST(StringSplitTest, StringSplitDontTrim) { + std::vector<std::wstring> r; + + SplitStringDontTrim(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(); + + SplitStringDontTrim(L"\ta\t\nb\tcc", L'\n', &r); + ASSERT_EQ(2U, r.size()); + EXPECT_EQ(r[0], L"\ta\t"); + EXPECT_EQ(r[1], L"b\tcc"); + r.clear(); +} + } // namespace base diff --git a/base/string_util.cc b/base/string_util.cc index 68f3d91..ba9108b 100644 --- a/base/string_util.cc +++ b/base/string_util.cc @@ -757,6 +757,8 @@ 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, @@ -801,26 +803,6 @@ void SplitString(const std::string& str, SplitStringT(str, s, true, r); } -void SplitStringDontTrim(const std::wstring& str, - wchar_t s, - std::vector<std::wstring>* r) { - SplitStringT(str, s, false, r); -} - -#if !defined(WCHAR_T_IS_UTF16) -void SplitStringDontTrim(const string16& str, - char16 s, - std::vector<string16>* r) { - SplitStringT(str, s, false, r); -} -#endif - -void SplitStringDontTrim(const std::string& str, - char s, - std::vector<std::string>* r) { - SplitStringT(str, s, false, 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 a0067d3..b1cb828 100644 --- a/base/string_util.h +++ b/base/string_util.h @@ -523,17 +523,6 @@ void SplitString(const std::string& str, char s, std::vector<std::string>* r); -// The same as SplitString, but don't trim white space. -void SplitStringDontTrim(const std::wstring& str, - wchar_t s, - std::vector<std::wstring>* r); -void SplitStringDontTrim(const string16& str, - char16 s, - std::vector<string16>* r); -void SplitStringDontTrim(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 23b1f53..c4fd5ae 100644 --- a/base/string_util_unittest.cc +++ b/base/string_util_unittest.cc @@ -762,25 +762,11 @@ TEST(StringUtilTest, SplitString) { EXPECT_EQ(r[3], L""); r.clear(); - SplitStringDontTrim(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(); - - SplitStringDontTrim(L"\ta\t\nb\tcc", L'\n', &r); - ASSERT_EQ(2U, r.size()); - EXPECT_EQ(r[0], L"\ta\t"); - EXPECT_EQ(r[1], L"b\tcc"); - r.clear(); } // Test for Tokenize |