| 6 |
| 7 | #include <stddef.h> |
| 8 |
| 9 | #include "base/logging.h" |
| 10 | #include "base/strings/string_util.h" |
| 11 | #include "base/third_party/icu/icu_utf.h" |
| 12 |
| 13 | namespace base { |
| 14 |
| 15 | namespace { |
| 16 |
| 17 | // Returns either the ASCII or UTF-16 whitespace. |
| 18 | template<typename Str> BasicStringPiece<Str> WhitespaceForType(); |
| 19 | #if defined(OS_WIN) && defined(BASE_STRING16_IS_STD_U16STRING) |
| 20 | template <> |
| 21 | WStringPiece WhitespaceForType<std::wstring>() { |
| 22 | return kWhitespaceWide; |
| 23 | } |
| 24 | #endif |
| 25 |
| 26 | template<> StringPiece16 WhitespaceForType<string16>() { |
| 27 | return kWhitespaceUTF16; |
| 28 | } |
| 29 | template<> StringPiece WhitespaceForType<std::string>() { |
| 30 | return kWhitespaceASCII; |
| 31 | } |
| 32 |
| 33 | // General string splitter template. Can take 8- or 16-bit input, can produce |
| 34 | // the corresponding string or StringPiece output. |
| 35 | template <typename OutputStringType, typename Str> |
| 36 | static std::vector<OutputStringType> SplitStringT( |
| 37 | BasicStringPiece<Str> str, |
| 38 | BasicStringPiece<Str> delimiter, |
| 39 | WhitespaceHandling whitespace, |
| 40 | SplitResult result_type) { |
| 41 | std::vector<OutputStringType> result; |
| 42 | if (str.empty()) |
| 43 | return result; |
| 44 |
| 45 | size_t start = 0; |
| 46 | while (start != Str::npos) { |
| 47 | size_t end = str.find_first_of(delimiter, start); |
| 48 |
| 49 | BasicStringPiece<Str> piece; |
| 50 | if (end == Str::npos) { |
| 51 | piece = str.substr(start); |
| 52 | start = Str::npos; |
| 53 | } else { |
| 54 | piece = str.substr(start, end - start); |
| 55 | start = end + 1; |
| 56 | } |
| 57 |
| 58 | if (whitespace == TRIM_WHITESPACE) |
| 59 | piece = TrimString(piece, WhitespaceForType<Str>(), TRIM_ALL); |
| 60 |
| 61 | if (result_type == SPLIT_WANT_ALL || !piece.empty()) |
| 62 | result.emplace_back(piece); |
| 63 | } |
| 64 | return result; |
| 65 | } |
| 66 |
| 67 | bool AppendStringKeyValue(StringPiece input, |
| 68 | char delimiter, |
| 69 | StringPairs* result) { |
| 70 | // Always append a new item regardless of success (it might be empty). The |
| 71 | // below code will copy the strings directly into the result pair. |
| 72 | result->resize(result->size() + 1); |
| 73 | auto& result_pair = result->back(); |
| 74 |
| 75 | // Find the delimiter. |
| 76 | size_t end_key_pos = input.find_first_of(delimiter); |
| 77 | if (end_key_pos == std::string::npos) { |
| 78 | DVLOG(1) << "cannot find delimiter in: " << input; |
| 79 | return false; // No delimiter. |
| 80 | } |
| 81 | result_pair.first = std::string(input.substr(0, end_key_pos)); |
| 82 |
| 83 | // Find the value string. |
| 84 | StringPiece remains = input.substr(end_key_pos, input.size() - end_key_pos); |
| 85 | size_t begin_value_pos = remains.find_first_not_of(delimiter); |
| 86 | if (begin_value_pos == StringPiece::npos) { |
| 87 | DVLOG(1) << "cannot parse value from input: " << input; |
| 88 | return false; // No value. |
| 89 | } |
| 90 |
| 91 | result_pair.second = std::string( |
| 92 | remains.substr(begin_value_pos, remains.size() - begin_value_pos)); |
| 93 |
| 94 | return true; |
| 95 | } |
| 96 |
| 97 | template <typename OutputStringType, typename Str> |
| 98 | std::vector<OutputStringType> SplitStringUsingSubstrT( |
| 99 | BasicStringPiece<Str> input, |
| 100 | BasicStringPiece<Str> delimiter, |
| 101 | WhitespaceHandling whitespace, |
| 102 | SplitResult result_type) { |
| 103 | using Piece = BasicStringPiece<Str>; |
| 104 | using size_type = typename Piece::size_type; |
| 105 |
| 106 | std::vector<OutputStringType> result; |
| 107 | if (delimiter.size() == 0) { |
| 108 | result.emplace_back(input); |
| 109 | return result; |
| 110 | } |
| 111 |
| 112 | for (size_type begin_index = 0, end_index = 0; end_index != Piece::npos; |
| 113 | begin_index = end_index + delimiter.size()) { |
| 114 | end_index = input.find(delimiter, begin_index); |
| 115 | Piece term = end_index == Piece::npos |
| 116 | ? input.substr(begin_index) |
| 117 | : input.substr(begin_index, end_index - begin_index); |
| 118 |
| 119 | if (whitespace == TRIM_WHITESPACE) |
| 120 | term = TrimString(term, WhitespaceForType<Str>(), TRIM_ALL); |
| 121 |
| 122 | if (result_type == SPLIT_WANT_ALL || !term.empty()) |
| 123 | result.emplace_back(term); |
| 124 | } |
| 125 |
| 126 | return result; |
| 127 | } |
| 128 |
| 129 | } // namespace |
| 130 |
| 131 | std::vector<std::string> SplitString(StringPiece input, |
| 132 | StringPiece separators, |
| 133 | WhitespaceHandling whitespace, |
| 134 | SplitResult result_type) { |
| 135 | return SplitStringT<std::string>(input, separators, whitespace, result_type); |
| 136 | } |
| 137 |
| 138 | std::vector<string16> SplitString(StringPiece16 input, |
| 139 | StringPiece16 separators, |
| 140 | WhitespaceHandling whitespace, |
| 141 | SplitResult result_type) { |
| 142 | return SplitStringT<string16>(input, separators, whitespace, result_type); |
| 143 | } |
| 144 |
| 145 | std::vector<StringPiece> SplitStringPiece(StringPiece input, |
| 146 | StringPiece separators, |
| 147 | WhitespaceHandling whitespace, |
| 148 | SplitResult result_type) { |
| 149 | return SplitStringT<StringPiece>(input, separators, whitespace, result_type); |
| 150 | } |
| 151 |
| 152 | std::vector<StringPiece16> SplitStringPiece(StringPiece16 input, |
| 153 | StringPiece16 separators, |
| 154 | WhitespaceHandling whitespace, |
| 155 | SplitResult result_type) { |
| 156 | return SplitStringT<StringPiece16>(input, separators, whitespace, |
| 157 | result_type); |
| 158 | } |
| 159 |
| 160 | bool SplitStringIntoKeyValuePairs(StringPiece input, |
| 161 | char key_value_delimiter, |
| 162 | char key_value_pair_delimiter, |
| 163 | StringPairs* key_value_pairs) { |
| 164 | return SplitStringIntoKeyValuePairsUsingSubstr( |
| 165 | input, key_value_delimiter, StringPiece(&key_value_pair_delimiter, 1), |
| 166 | key_value_pairs); |
| 167 | } |
| 168 |
| 169 | bool SplitStringIntoKeyValuePairsUsingSubstr( |
| 170 | StringPiece input, |
| 171 | char key_value_delimiter, |
| 172 | StringPiece key_value_pair_delimiter, |
| 173 | StringPairs* key_value_pairs) { |
| 174 | key_value_pairs->clear(); |
| 175 |
| 176 | std::vector<StringPiece> pairs = SplitStringPieceUsingSubstr( |
| 177 | input, key_value_pair_delimiter, TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY); |
| 178 | key_value_pairs->reserve(pairs.size()); |
| 179 |
| 180 | bool success = true; |
| 181 | for (const StringPiece& pair : pairs) { |
| 182 | if (!AppendStringKeyValue(pair, key_value_delimiter, key_value_pairs)) { |
| 183 | // Don't return here, to allow for pairs without associated |
| 184 | // value or key; just record that the split failed. |
| 185 | success = false; |
| 186 | } |
| 187 | } |
| 188 | return success; |
| 189 | } |
| 190 |
| 191 | std::vector<string16> SplitStringUsingSubstr(StringPiece16 input, |
| 192 | StringPiece16 delimiter, |
| 193 | WhitespaceHandling whitespace, |
| 194 | SplitResult result_type) { |
| 195 | return SplitStringUsingSubstrT<string16>(input, delimiter, whitespace, |
| 196 | result_type); |
| 197 | } |
| 198 |
| 199 | std::vector<std::string> SplitStringUsingSubstr(StringPiece input, |
| 200 | StringPiece delimiter, |
| 201 | WhitespaceHandling whitespace, |
| 202 | SplitResult result_type) { |
| 203 | return SplitStringUsingSubstrT<std::string>(input, delimiter, whitespace, |
| 204 | result_type); |
| 205 | } |
| 206 |
| 207 | std::vector<StringPiece16> SplitStringPieceUsingSubstr( |
| 208 | StringPiece16 input, |
| 209 | StringPiece16 delimiter, |
| 210 | WhitespaceHandling whitespace, |
| 211 | SplitResult result_type) { |
| 212 | std::vector<StringPiece16> result; |
| 213 | return SplitStringUsingSubstrT<StringPiece16>(input, delimiter, whitespace, |
| 214 | result_type); |
| 215 | } |
| 216 |
| 217 | std::vector<StringPiece> SplitStringPieceUsingSubstr( |
| 218 | StringPiece input, |
| 219 | StringPiece delimiter, |
| 220 | WhitespaceHandling whitespace, |
| 221 | SplitResult result_type) { |
| 222 | return SplitStringUsingSubstrT<StringPiece>(input, delimiter, whitespace, |
| 223 | result_type); |
| 224 | } |
| 225 |
| 226 | #if defined(OS_WIN) && defined(BASE_STRING16_IS_STD_U16STRING) |
| 227 | std::vector<std::wstring> SplitString(WStringPiece input, |
| 228 | WStringPiece separators, |
| 229 | WhitespaceHandling whitespace, |
| 230 | SplitResult result_type) { |
| 231 | return SplitStringT<std::wstring>(input, separators, whitespace, result_type); |
| 232 | } |
| 233 |
| 234 | std::vector<WStringPiece> SplitStringPiece(WStringPiece input, |
| 235 | WStringPiece separators, |
| 236 | WhitespaceHandling whitespace, |
| 237 | SplitResult result_type) { |
| 238 | return SplitStringT<WStringPiece>(input, separators, whitespace, result_type); |
| 239 | } |
| 240 |
| 241 | std::vector<std::wstring> SplitStringUsingSubstr(WStringPiece input, |
| 242 | WStringPiece delimiter, |
| 243 | WhitespaceHandling whitespace, |
| 244 | SplitResult result_type) { |
| 245 | return SplitStringUsingSubstrT<std::wstring>(input, delimiter, whitespace, |
| 246 | result_type); |
| 247 | } |
| 248 |
| 249 | std::vector<WStringPiece> SplitStringPieceUsingSubstr( |
| 250 | WStringPiece input, |
| 251 | WStringPiece delimiter, |
| 252 | WhitespaceHandling whitespace, |
| 253 | SplitResult result_type) { |
| 254 | return SplitStringUsingSubstrT<WStringPiece>(input, delimiter, whitespace, |
| 255 | result_type); |
| 256 | } |
| 257 | #endif |
| 258 |
| 259 | } // namespace base |
| 260 |