1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
base / strings / string_split_internal.h [blame]
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef BASE_STRINGS_STRING_SPLIT_INTERNAL_H_
#define BASE_STRINGS_STRING_SPLIT_INTERNAL_H_
#include <string_view>
#include <vector>
#include "base/strings/string_util.h"
namespace base {
namespace internal {
// Returns either the ASCII or UTF-16 whitespace.
template <typename CharT>
std::basic_string_view<CharT> WhitespaceForType();
template <>
inline std::u16string_view WhitespaceForType<char16_t>() {
return kWhitespaceUTF16;
}
template <>
inline std::string_view WhitespaceForType<char>() {
return kWhitespaceASCII;
}
// General string splitter template. Can take 8- or 16-bit input, can produce
// the corresponding string or std::string_view output.
template <typename OutputStringType,
typename T,
typename CharT = typename T::value_type>
static std::vector<OutputStringType> SplitStringT(T str,
T delimiter,
WhitespaceHandling whitespace,
SplitResult result_type) {
std::vector<OutputStringType> result;
if (str.empty())
return result;
size_t start = 0;
while (start != std::basic_string<CharT>::npos) {
size_t end = str.find_first_of(delimiter, start);
std::basic_string_view<CharT> piece;
if (end == std::basic_string<CharT>::npos) {
piece = str.substr(start);
start = std::basic_string<CharT>::npos;
} else {
piece = str.substr(start, end - start);
start = end + 1;
}
if (whitespace == TRIM_WHITESPACE)
piece = TrimString(piece, WhitespaceForType<CharT>(), TRIM_ALL);
if (result_type == SPLIT_WANT_ALL || !piece.empty())
result.emplace_back(piece);
}
return result;
}
template <typename OutputStringType,
typename T,
typename CharT = typename T::value_type>
std::vector<OutputStringType> SplitStringUsingSubstrT(
T input,
T delimiter,
WhitespaceHandling whitespace,
SplitResult result_type) {
using Piece = std::basic_string_view<CharT>;
using size_type = typename Piece::size_type;
std::vector<OutputStringType> result;
if (delimiter.size() == 0) {
result.emplace_back(input);
return result;
}
for (size_type begin_index = 0, end_index = 0; end_index != Piece::npos;
begin_index = end_index + delimiter.size()) {
end_index = input.find(delimiter, begin_index);
Piece term = end_index == Piece::npos
? input.substr(begin_index)
: input.substr(begin_index, end_index - begin_index);
if (whitespace == TRIM_WHITESPACE)
term = TrimString(term, WhitespaceForType<CharT>(), TRIM_ALL);
if (result_type == SPLIT_WANT_ALL || !term.empty())
result.emplace_back(term);
}
return result;
}
} // namespace internal
} // namespace base
#endif // BASE_STRINGS_STRING_SPLIT_INTERNAL_H_