| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 namespace base { | 12 namespace base { |
| 13 | 13 |
| 14 template<typename STR> | 14 template<typename STR> |
| 15 static void SplitStringT(const STR& str, | 15 static void SplitStringT(const STR& str, |
| 16 const typename STR::value_type s, | 16 const typename STR::value_type s, |
| 17 bool trim_whitespace, | 17 bool trim_whitespace, |
| 18 std::vector<STR>* r) { | 18 std::vector<STR>* r) { |
| 19 r->clear(); |
| 19 size_t last = 0; | 20 size_t last = 0; |
| 20 size_t c = str.size(); | 21 size_t c = str.size(); |
| 21 for (size_t i = 0; i <= c; ++i) { | 22 for (size_t i = 0; i <= c; ++i) { |
| 22 if (i == c || str[i] == s) { | 23 if (i == c || str[i] == s) { |
| 23 STR tmp(str, last, i - last); | 24 STR tmp(str, last, i - last); |
| 24 if (trim_whitespace) | 25 if (trim_whitespace) |
| 25 TrimWhitespace(tmp, TRIM_ALL, &tmp); | 26 TrimWhitespace(tmp, TRIM_ALL, &tmp); |
| 26 // Avoid converting an empty or all-whitespace source string into a vector | 27 // Avoid converting an empty or all-whitespace source string into a vector |
| 27 // of one empty string. | 28 // of one empty string. |
| 28 if (i != c || !r->empty() || !tmp.empty()) | 29 if (i != c || !r->empty() || !tmp.empty()) |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 DCHECK_LE(value.size(), 1U); | 109 DCHECK_LE(value.size(), 1U); |
| 109 kv_pairs->push_back(make_pair(key, value.empty()? "" : value[0])); | 110 kv_pairs->push_back(make_pair(key, value.empty()? "" : value[0])); |
| 110 } | 111 } |
| 111 return success; | 112 return success; |
| 112 } | 113 } |
| 113 | 114 |
| 114 template <typename STR> | 115 template <typename STR> |
| 115 static void SplitStringUsingSubstrT(const STR& str, | 116 static void SplitStringUsingSubstrT(const STR& str, |
| 116 const STR& s, | 117 const STR& s, |
| 117 std::vector<STR>* r) { | 118 std::vector<STR>* r) { |
| 119 r->clear(); |
| 118 typename STR::size_type begin_index = 0; | 120 typename STR::size_type begin_index = 0; |
| 119 while (true) { | 121 while (true) { |
| 120 const typename STR::size_type end_index = str.find(s, begin_index); | 122 const typename STR::size_type end_index = str.find(s, begin_index); |
| 121 if (end_index == STR::npos) { | 123 if (end_index == STR::npos) { |
| 122 const STR term = str.substr(begin_index); | 124 const STR term = str.substr(begin_index); |
| 123 STR tmp; | 125 STR tmp; |
| 124 TrimWhitespace(term, TRIM_ALL, &tmp); | 126 TrimWhitespace(term, TRIM_ALL, &tmp); |
| 125 r->push_back(tmp); | 127 r->push_back(tmp); |
| 126 return; | 128 return; |
| 127 } | 129 } |
| (...skipping 30 matching lines...) Expand all Loading... |
| 158 DCHECK(IsStringUTF8(str)); | 160 DCHECK(IsStringUTF8(str)); |
| 159 #if CHAR_MIN < 0 | 161 #if CHAR_MIN < 0 |
| 160 DCHECK(c >= 0); | 162 DCHECK(c >= 0); |
| 161 #endif | 163 #endif |
| 162 DCHECK(c < 0x7F); | 164 DCHECK(c < 0x7F); |
| 163 SplitStringT(str, c, false, r); | 165 SplitStringT(str, c, false, r); |
| 164 } | 166 } |
| 165 | 167 |
| 166 template<typename STR> | 168 template<typename STR> |
| 167 void SplitStringAlongWhitespaceT(const STR& str, std::vector<STR>* result) { | 169 void SplitStringAlongWhitespaceT(const STR& str, std::vector<STR>* result) { |
| 170 result->clear(); |
| 168 const size_t length = str.length(); | 171 const size_t length = str.length(); |
| 169 if (!length) | 172 if (!length) |
| 170 return; | 173 return; |
| 171 | 174 |
| 172 bool last_was_ws = false; | 175 bool last_was_ws = false; |
| 173 size_t last_non_ws_start = 0; | 176 size_t last_non_ws_start = 0; |
| 174 for (size_t i = 0; i < length; ++i) { | 177 for (size_t i = 0; i < length; ++i) { |
| 175 switch (str[i]) { | 178 switch (str[i]) { |
| 176 // HTML 5 defines whitespace as: space, tab, LF, line tab, FF, or CR. | 179 // HTML 5 defines whitespace as: space, tab, LF, line tab, FF, or CR. |
| 177 case L' ': | 180 case L' ': |
| (...skipping 29 matching lines...) Expand all Loading... |
| 207 std::vector<string16>* result) { | 210 std::vector<string16>* result) { |
| 208 SplitStringAlongWhitespaceT(str, result); | 211 SplitStringAlongWhitespaceT(str, result); |
| 209 } | 212 } |
| 210 | 213 |
| 211 void SplitStringAlongWhitespace(const std::string& str, | 214 void SplitStringAlongWhitespace(const std::string& str, |
| 212 std::vector<std::string>* result) { | 215 std::vector<std::string>* result) { |
| 213 SplitStringAlongWhitespaceT(str, result); | 216 SplitStringAlongWhitespaceT(str, result); |
| 214 } | 217 } |
| 215 | 218 |
| 216 } // namespace base | 219 } // namespace base |
| OLD | NEW |