| 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" | 9 #include "base/third_party/icu/icu_utf.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 11 | 11 |
| 12 template<typename STR> |
| 13 static void SplitStringT(const STR& str, |
| 14 const typename STR::value_type s, |
| 15 bool trim_whitespace, |
| 16 std::vector<STR>* r) { |
| 17 size_t last = 0; |
| 18 size_t i; |
| 19 size_t c = str.size(); |
| 20 for (i = 0; i <= c; ++i) { |
| 21 if (i == c || str[i] == s) { |
| 22 size_t len = i - last; |
| 23 STR tmp = str.substr(last, len); |
| 24 if (trim_whitespace) { |
| 25 STR t_tmp; |
| 26 TrimWhitespace(tmp, TRIM_ALL, &t_tmp); |
| 27 r->push_back(t_tmp); |
| 28 } else { |
| 29 r->push_back(tmp); |
| 30 } |
| 31 last = i + 1; |
| 32 } |
| 33 } |
| 34 } |
| 35 |
| 36 void SplitString(const std::wstring& str, |
| 37 wchar_t c, |
| 38 std::vector<std::wstring>* r) { |
| 39 SplitStringT(str, c, true, r); |
| 40 } |
| 41 |
| 42 #if !defined(WCHAR_T_IS_UTF16) |
| 43 void SplitString(const string16& str, |
| 44 char16 c, |
| 45 std::vector<string16>* r) { |
| 46 DCHECK(CBU16_IS_SINGLE(c)); |
| 47 SplitStringT(str, c, true, r); |
| 48 } |
| 49 #endif |
| 50 |
| 51 void SplitString(const std::string& str, |
| 52 char c, |
| 53 std::vector<std::string>* r) { |
| 54 DCHECK(c >= 0 && c < 0x7F); |
| 55 SplitStringT(str, c, true, r); |
| 56 } |
| 57 |
| 12 namespace base { | 58 namespace base { |
| 13 | 59 |
| 14 bool SplitStringIntoKeyValues( | 60 bool SplitStringIntoKeyValues( |
| 15 const std::string& line, | 61 const std::string& line, |
| 16 char key_value_delimiter, | 62 char key_value_delimiter, |
| 17 std::string* key, std::vector<std::string>* values) { | 63 std::string* key, std::vector<std::string>* values) { |
| 18 key->clear(); | 64 key->clear(); |
| 19 values->clear(); | 65 values->clear(); |
| 20 | 66 |
| 21 // Find the key string. | 67 // Find the key string. |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 std::vector<string16>* r) { | 146 std::vector<string16>* r) { |
| 101 SplitStringUsingSubstrT(str, s, r); | 147 SplitStringUsingSubstrT(str, s, r); |
| 102 } | 148 } |
| 103 | 149 |
| 104 void SplitStringUsingSubstr(const std::string& str, | 150 void SplitStringUsingSubstr(const std::string& str, |
| 105 const std::string& s, | 151 const std::string& s, |
| 106 std::vector<std::string>* r) { | 152 std::vector<std::string>* r) { |
| 107 SplitStringUsingSubstrT(str, s, r); | 153 SplitStringUsingSubstrT(str, s, r); |
| 108 } | 154 } |
| 109 | 155 |
| 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, | 156 void SplitStringDontTrim(const std::wstring& str, |
| 135 wchar_t c, | 157 wchar_t c, |
| 136 std::vector<std::wstring>* r) { | 158 std::vector<std::wstring>* r) { |
| 137 SplitStringT(str, c, false, r); | 159 SplitStringT(str, c, false, r); |
| 138 } | 160 } |
| 139 | 161 |
| 140 #if !defined(WCHAR_T_IS_UTF16) | 162 #if !defined(WCHAR_T_IS_UTF16) |
| 141 void SplitStringDontTrim(const string16& str, | 163 void SplitStringDontTrim(const string16& str, |
| 142 char16 c, | 164 char16 c, |
| 143 std::vector<string16>* r) { | 165 std::vector<string16>* r) { |
| 144 DCHECK(CBU16_IS_SINGLE(c)); | 166 DCHECK(CBU16_IS_SINGLE(c)); |
| 145 SplitStringT(str, c, false, r); | 167 SplitStringT(str, c, false, r); |
| 146 } | 168 } |
| 147 #endif | 169 #endif |
| 148 | 170 |
| 149 void SplitStringDontTrim(const std::string& str, | 171 void SplitStringDontTrim(const std::string& str, |
| 150 char c, | 172 char c, |
| 151 std::vector<std::string>* r) { | 173 std::vector<std::string>* r) { |
| 152 DCHECK(IsStringUTF8(str)); | 174 DCHECK(IsStringUTF8(str)); |
| 153 DCHECK(c < 0x7F); | 175 DCHECK(c >= 0 && c < 0x7F); |
| 154 SplitStringT(str, c, false, r); | 176 SplitStringT(str, c, false, r); |
| 155 } | 177 } |
| 156 | 178 |
| 157 } // namespace base | 179 } // namespace base |
| OLD | NEW |