OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/string_split.h" | 5 #include "base/string_split.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "base/third_party/icu/icu_utf.h" |
| 10 #include "base/utf_string_conversions.h" |
9 | 11 |
10 namespace base { | 12 namespace base { |
11 | 13 |
12 bool SplitStringIntoKeyValues( | 14 bool SplitStringIntoKeyValues( |
13 const std::string& line, | 15 const std::string& line, |
14 char key_value_delimiter, | 16 char key_value_delimiter, |
15 std::string* key, std::vector<std::string>* values) { | 17 std::string* key, std::vector<std::string>* values) { |
16 key->clear(); | 18 key->clear(); |
17 values->clear(); | 19 values->clear(); |
18 | 20 |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 std::vector<string16>* r) { | 100 std::vector<string16>* r) { |
99 SplitStringUsingSubstrT(str, s, r); | 101 SplitStringUsingSubstrT(str, s, r); |
100 } | 102 } |
101 | 103 |
102 void SplitStringUsingSubstr(const std::string& str, | 104 void SplitStringUsingSubstr(const std::string& str, |
103 const std::string& s, | 105 const std::string& s, |
104 std::vector<std::string>* r) { | 106 std::vector<std::string>* r) { |
105 SplitStringUsingSubstrT(str, s, r); | 107 SplitStringUsingSubstrT(str, s, r); |
106 } | 108 } |
107 | 109 |
| 110 template<typename STR> |
| 111 static void SplitStringT(const STR& str, |
| 112 const typename STR::value_type s, |
| 113 bool trim_whitespace, |
| 114 std::vector<STR>* r) { |
| 115 size_t last = 0; |
| 116 size_t i; |
| 117 size_t c = str.size(); |
| 118 for (i = 0; i <= c; ++i) { |
| 119 if (i == c || str[i] == s) { |
| 120 size_t len = i - last; |
| 121 STR tmp = str.substr(last, len); |
| 122 if (trim_whitespace) { |
| 123 STR t_tmp; |
| 124 TrimWhitespace(tmp, TRIM_ALL, &t_tmp); |
| 125 r->push_back(t_tmp); |
| 126 } else { |
| 127 r->push_back(tmp); |
| 128 } |
| 129 last = i + 1; |
| 130 } |
| 131 } |
| 132 } |
| 133 |
| 134 void SplitStringDontTrim(const std::wstring& str, |
| 135 wchar_t c, |
| 136 std::vector<std::wstring>* r) { |
| 137 SplitStringT(str, c, false, r); |
| 138 } |
| 139 |
| 140 #if !defined(WCHAR_T_IS_UTF16) |
| 141 void SplitStringDontTrim(const string16& str, |
| 142 char16 c, |
| 143 std::vector<string16>* r) { |
| 144 DCHECK(CBU16_IS_SINGLE(c)); |
| 145 SplitStringT(str, c, false, r); |
| 146 } |
| 147 #endif |
| 148 |
| 149 void SplitStringDontTrim(const std::string& str, |
| 150 char c, |
| 151 std::vector<std::string>* r) { |
| 152 DCHECK(IsStringUTF8(str)); |
| 153 DCHECK(c < 0x7F); |
| 154 SplitStringT(str, c, false, r); |
| 155 } |
| 156 |
108 } // namespace base | 157 } // namespace base |
OLD | NEW |