blob: 646b4de883595b6021b356fce304741cefda6e91 [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
12namespace base {
13
14bool SplitStringIntoKeyValues(
15 const std::string& line,
16 char key_value_delimiter,
17 std::string* key, std::vector<std::string>* values) {
18 key->clear();
19 values->clear();
20
[email protected]650303702010-05-05 00:36:3421 // Find the key string.
[email protected]0477554f2010-01-21 19:29:2522 size_t end_key_pos = line.find_first_of(key_value_delimiter);
23 if (end_key_pos == std::string::npos) {
24 DLOG(INFO) << "cannot parse key from line: " << line;
25 return false; // no key
26 }
27 key->assign(line, 0, end_key_pos);
28
[email protected]650303702010-05-05 00:36:3429 // Find the values string.
[email protected]0477554f2010-01-21 19:29:2530 std::string remains(line, end_key_pos, line.size() - end_key_pos);
31 size_t begin_values_pos = remains.find_first_not_of(key_value_delimiter);
32 if (begin_values_pos == std::string::npos) {
33 DLOG(INFO) << "cannot parse value from line: " << line;
34 return false; // no value
35 }
36 std::string values_string(remains, begin_values_pos,
37 remains.size() - begin_values_pos);
38
[email protected]650303702010-05-05 00:36:3439 // Construct the values vector.
[email protected]0477554f2010-01-21 19:29:2540 values->push_back(values_string);
41 return true;
42}
43
44bool SplitStringIntoKeyValuePairs(
45 const std::string& line,
46 char key_value_delimiter,
47 char key_value_pair_delimiter,
48 std::vector<std::pair<std::string, std::string> >* kv_pairs) {
49 kv_pairs->clear();
50
51 std::vector<std::string> pairs;
52 SplitString(line, key_value_pair_delimiter, &pairs);
53
54 bool success = true;
55 for (size_t i = 0; i < pairs.size(); ++i) {
[email protected]650303702010-05-05 00:36:3456 // Empty pair. SplitStringIntoKeyValues is more strict about an empty pair
57 // line, so continue with the next pair.
58 if (pairs[i].empty())
59 continue;
60
[email protected]0477554f2010-01-21 19:29:2561 std::string key;
62 std::vector<std::string> value;
63 if (!SplitStringIntoKeyValues(pairs[i],
64 key_value_delimiter,
65 &key, &value)) {
66 // Don't return here, to allow for keys without associated
67 // values; just record that our split failed.
68 success = false;
69 }
70 DCHECK_LE(value.size(), 1U);
71 kv_pairs->push_back(make_pair(key, value.empty()? "" : value[0]));
72 }
73 return success;
74}
75
[email protected]e8478ae2010-09-02 02:01:4876template <typename STR>
77static void SplitStringUsingSubstrT(const STR& str,
78 const STR& s,
79 std::vector<STR>* r) {
80 typename STR::size_type begin_index = 0;
81 while (true) {
82 const typename STR::size_type end_index = str.find(s, begin_index);
83 if (end_index == STR::npos) {
84 const STR term = str.substr(begin_index);
85 STR tmp;
86 TrimWhitespace(term, TRIM_ALL, &tmp);
87 r->push_back(tmp);
88 return;
89 }
90 const STR term = str.substr(begin_index, end_index - begin_index);
91 STR tmp;
92 TrimWhitespace(term, TRIM_ALL, &tmp);
93 r->push_back(tmp);
94 begin_index = end_index + s.size();
95 }
96}
97
98void SplitStringUsingSubstr(const string16& str,
99 const string16& s,
100 std::vector<string16>* r) {
101 SplitStringUsingSubstrT(str, s, r);
102}
103
104void SplitStringUsingSubstr(const std::string& str,
105 const std::string& s,
106 std::vector<std::string>* r) {
107 SplitStringUsingSubstrT(str, s, r);
108}
109
[email protected]7594f6d2010-09-15 13:36:22110template<typename STR>
111static 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
134void 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)
141void 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
149void 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
[email protected]0477554f2010-01-21 19:29:25157} // namespace base