blob: bc7955d74134407e8342e8345cf9e33c3d80cb10 [file] [log] [blame]
[email protected]0477554f2010-01-21 19:29:251// 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
3// found in the LICENSE file.
4
5#include "base/string_split.h"
6
[email protected]f1633932010-08-17 23:05:287#include "base/logging.h"
[email protected]0477554f2010-01-21 19:29:258#include "base/string_util.h"
[email protected]7594f6d2010-09-15 13:36:229#include "base/third_party/icu/icu_utf.h"
10#include "base/utf_string_conversions.h"
[email protected]0477554f2010-01-21 19:29:2511
[email protected]76eb0242010-10-14 00:35:3612namespace base {
13
[email protected]4e5ae20f2010-09-24 04:52:1114template<typename STR>
15static void SplitStringT(const STR& str,
16 const typename STR::value_type s,
17 bool trim_whitespace,
18 std::vector<STR>* r) {
19 size_t last = 0;
20 size_t i;
21 size_t c = str.size();
22 for (i = 0; i <= c; ++i) {
23 if (i == c || str[i] == s) {
24 size_t len = i - last;
25 STR tmp = str.substr(last, len);
26 if (trim_whitespace) {
27 STR t_tmp;
28 TrimWhitespace(tmp, TRIM_ALL, &t_tmp);
29 r->push_back(t_tmp);
30 } else {
31 r->push_back(tmp);
32 }
33 last = i + 1;
34 }
35 }
36}
37
38void SplitString(const std::wstring& str,
39 wchar_t c,
40 std::vector<std::wstring>* r) {
41 SplitStringT(str, c, true, r);
42}
43
44#if !defined(WCHAR_T_IS_UTF16)
45void SplitString(const string16& str,
46 char16 c,
47 std::vector<string16>* r) {
48 DCHECK(CBU16_IS_SINGLE(c));
49 SplitStringT(str, c, true, r);
50}
51#endif
52
53void SplitString(const std::string& str,
54 char c,
55 std::vector<std::string>* r) {
56 DCHECK(c >= 0 && c < 0x7F);
57 SplitStringT(str, c, true, r);
58}
59
[email protected]0477554f2010-01-21 19:29:2560bool SplitStringIntoKeyValues(
61 const std::string& line,
62 char key_value_delimiter,
63 std::string* key, std::vector<std::string>* values) {
64 key->clear();
65 values->clear();
66
[email protected]650303702010-05-05 00:36:3467 // Find the key string.
[email protected]0477554f2010-01-21 19:29:2568 size_t end_key_pos = line.find_first_of(key_value_delimiter);
69 if (end_key_pos == std::string::npos) {
[email protected]b026e35d2010-10-19 02:31:0370 DVLOG(1) << "cannot parse key from line: " << line;
[email protected]0477554f2010-01-21 19:29:2571 return false; // no key
72 }
73 key->assign(line, 0, end_key_pos);
74
[email protected]650303702010-05-05 00:36:3475 // Find the values string.
[email protected]0477554f2010-01-21 19:29:2576 std::string remains(line, end_key_pos, line.size() - end_key_pos);
77 size_t begin_values_pos = remains.find_first_not_of(key_value_delimiter);
78 if (begin_values_pos == std::string::npos) {
[email protected]b026e35d2010-10-19 02:31:0379 DVLOG(1) << "cannot parse value from line: " << line;
[email protected]0477554f2010-01-21 19:29:2580 return false; // no value
81 }
82 std::string values_string(remains, begin_values_pos,
83 remains.size() - begin_values_pos);
84
[email protected]650303702010-05-05 00:36:3485 // Construct the values vector.
[email protected]0477554f2010-01-21 19:29:2586 values->push_back(values_string);
87 return true;
88}
89
90bool SplitStringIntoKeyValuePairs(
91 const std::string& line,
92 char key_value_delimiter,
93 char key_value_pair_delimiter,
94 std::vector<std::pair<std::string, std::string> >* kv_pairs) {
95 kv_pairs->clear();
96
97 std::vector<std::string> pairs;
98 SplitString(line, key_value_pair_delimiter, &pairs);
99
100 bool success = true;
101 for (size_t i = 0; i < pairs.size(); ++i) {
[email protected]650303702010-05-05 00:36:34102 // Empty pair. SplitStringIntoKeyValues is more strict about an empty pair
103 // line, so continue with the next pair.
104 if (pairs[i].empty())
105 continue;
106
[email protected]0477554f2010-01-21 19:29:25107 std::string key;
108 std::vector<std::string> value;
109 if (!SplitStringIntoKeyValues(pairs[i],
110 key_value_delimiter,
111 &key, &value)) {
112 // Don't return here, to allow for keys without associated
113 // values; just record that our split failed.
114 success = false;
115 }
116 DCHECK_LE(value.size(), 1U);
117 kv_pairs->push_back(make_pair(key, value.empty()? "" : value[0]));
118 }
119 return success;
120}
121
[email protected]e8478ae2010-09-02 02:01:48122template <typename STR>
123static void SplitStringUsingSubstrT(const STR& str,
124 const STR& s,
125 std::vector<STR>* r) {
126 typename STR::size_type begin_index = 0;
127 while (true) {
128 const typename STR::size_type end_index = str.find(s, begin_index);
129 if (end_index == STR::npos) {
130 const STR term = str.substr(begin_index);
131 STR tmp;
132 TrimWhitespace(term, TRIM_ALL, &tmp);
133 r->push_back(tmp);
134 return;
135 }
136 const STR term = str.substr(begin_index, end_index - begin_index);
137 STR tmp;
138 TrimWhitespace(term, TRIM_ALL, &tmp);
139 r->push_back(tmp);
140 begin_index = end_index + s.size();
141 }
142}
143
144void SplitStringUsingSubstr(const string16& str,
145 const string16& s,
146 std::vector<string16>* r) {
147 SplitStringUsingSubstrT(str, s, r);
148}
149
150void SplitStringUsingSubstr(const std::string& str,
151 const std::string& s,
152 std::vector<std::string>* r) {
153 SplitStringUsingSubstrT(str, s, r);
154}
155
[email protected]7594f6d2010-09-15 13:36:22156void SplitStringDontTrim(const string16& str,
157 char16 c,
158 std::vector<string16>* r) {
159 DCHECK(CBU16_IS_SINGLE(c));
160 SplitStringT(str, c, false, r);
161}
[email protected]7594f6d2010-09-15 13:36:22162
163void SplitStringDontTrim(const std::string& str,
164 char c,
165 std::vector<std::string>* r) {
166 DCHECK(IsStringUTF8(str));
[email protected]4e5ae20f2010-09-24 04:52:11167 DCHECK(c >= 0 && c < 0x7F);
[email protected]7594f6d2010-09-15 13:36:22168 SplitStringT(str, c, false, r);
169}
170
[email protected]0477554f2010-01-21 19:29:25171} // namespace base