Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(177)

Side by Side Diff: base/strings/string_split.h

Issue 1647803004: Move base to DEPS (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/strings/string_piece_unittest.cc ('k') | base/strings/string_split.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef BASE_STRINGS_STRING_SPLIT_H_
6 #define BASE_STRINGS_STRING_SPLIT_H_
7
8 #include <string>
9 #include <utility>
10 #include <vector>
11
12 #include "base/base_export.h"
13 #include "base/strings/string16.h"
14 #include "base/strings/string_piece.h"
15
16 namespace base {
17
18 enum WhitespaceHandling {
19 KEEP_WHITESPACE,
20 TRIM_WHITESPACE,
21 };
22
23 enum SplitResult {
24 // Strictly return all results.
25 //
26 // If the input is ",," and the separator is ',' this will return a
27 // vector of three empty strings.
28 SPLIT_WANT_ALL,
29
30 // Only nonempty results will be added to the results. Multiple separators
31 // will be coalesced. Separators at the beginning and end of the input will
32 // be ignored. With TRIM_WHITESPACE, whitespace-only results will be dropped.
33 //
34 // If the input is ",," and the separator is ',', this will return an empty
35 // vector.
36 SPLIT_WANT_NONEMPTY,
37 };
38
39 // Split the given string on ANY of the given separators, returning copies of
40 // the result.
41 //
42 // To split on either commas or semicolons, keeping all whitespace:
43 //
44 // std::vector<std::string> tokens = base::SplitString(
45 // input, ",;", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
46 BASE_EXPORT std::vector<std::string> SplitString(StringPiece input,
47 StringPiece separators,
48 WhitespaceHandling whitespace,
49 SplitResult result_type);
50 BASE_EXPORT std::vector<string16> SplitString(StringPiece16 input,
51 StringPiece16 separators,
52 WhitespaceHandling whitespace,
53 SplitResult result_type);
54
55 // Like SplitString above except it returns a vector of StringPieces which
56 // reference the original buffer without copying. Although you have to be
57 // careful to keep the original string unmodified, this provides an efficient
58 // way to iterate through tokens in a string.
59 //
60 // To iterate through all whitespace-separated tokens in an input string:
61 //
62 // for (const auto& cur :
63 // base::SplitStringPiece(input, base::kWhitespaceASCII,
64 // base::KEEP_WHITESPACE,
65 // base::SPLIT_WANT_NONEMPTY)) {
66 // ...
67 BASE_EXPORT std::vector<StringPiece> SplitStringPiece(
68 StringPiece input,
69 StringPiece separators,
70 WhitespaceHandling whitespace,
71 SplitResult result_type);
72 BASE_EXPORT std::vector<StringPiece16> SplitStringPiece(
73 StringPiece16 input,
74 StringPiece16 separators,
75 WhitespaceHandling whitespace,
76 SplitResult result_type);
77
78 using StringPairs = std::vector<std::pair<std::string, std::string>>;
79
80 // Splits |line| into key value pairs according to the given delimiters and
81 // removes whitespace leading each key and trailing each value. Returns true
82 // only if each pair has a non-empty key and value. |key_value_pairs| will
83 // include ("","") pairs for entries without |key_value_delimiter|.
84 BASE_EXPORT bool SplitStringIntoKeyValuePairs(const std::string& line,
85 char key_value_delimiter,
86 char key_value_pair_delimiter,
87 StringPairs* key_value_pairs);
88
89 // Similar to SplitString, but use a substring delimiter instead of a list of
90 // characters that are all possible delimiters.
91 //
92 // TODO(brettw) this should probably be changed and expanded to provide a
93 // mirror of the SplitString[Piece] API above, just with the different
94 // delimiter handling.
95 BASE_EXPORT void SplitStringUsingSubstr(const string16& str,
96 const string16& s,
97 std::vector<string16>* r);
98 BASE_EXPORT void SplitStringUsingSubstr(const std::string& str,
99 const std::string& s,
100 std::vector<std::string>* r);
101
102 // -----------------------------------------------------------------------------
103 // Backwards-compat wrappers
104 //
105 // New code should use one of the more general variants above.
106 // TODO(brettw) remove these and convert to the versions above.
107
108 // Splits |str| into a vector of strings delimited by |c|, placing the results
109 // in |r|. If several instances of |c| are contiguous, or if |str| begins with
110 // or ends with |c|, then an empty string is inserted.
111 //
112 // Every substring is trimmed of any leading or trailing white space.
113 // NOTE: |c| must be in BMP (Basic Multilingual Plane)
114 BASE_EXPORT void SplitString(const string16& str,
115 char16 c,
116 std::vector<string16>* r);
117
118 // |str| should not be in a multi-byte encoding like Shift-JIS or GBK in which
119 // the trailing byte of a multi-byte character can be in the ASCII range.
120 // UTF-8, and other single/multi-byte ASCII-compatible encodings are OK.
121 // Note: |c| must be in the ASCII range.
122 BASE_EXPORT void SplitString(const std::string& str,
123 char c,
124 std::vector<std::string>* r);
125
126 // The same as SplitString, but don't trim white space.
127 // NOTE: |c| must be in BMP (Basic Multilingual Plane)
128 BASE_EXPORT void SplitStringDontTrim(StringPiece16 str,
129 char16 c,
130 std::vector<string16>* r);
131 // |str| should not be in a multi-byte encoding like Shift-JIS or GBK in which
132 // the trailing byte of a multi-byte character can be in the ASCII range.
133 // UTF-8, and other single/multi-byte ASCII-compatible encodings are OK.
134 // Note: |c| must be in the ASCII range.
135 BASE_EXPORT void SplitStringDontTrim(StringPiece str,
136 char c,
137 std::vector<std::string>* result);
138
139 // WARNING: this uses whitespace as defined by the HTML5 spec (ASCII whitespace
140 // only).
141 //
142 // The difference between this and calling SplitString with the whitespace
143 // characters as separators is the treatment of the first element when the
144 // string starts with whitespace.
145 //
146 // Input SplitString SplitStringAlongWhitespace
147 // --------------------------------------------------------
148 // " a " "", "a" "a"
149 BASE_EXPORT void SplitStringAlongWhitespace(const string16& str,
150 std::vector<string16>* result);
151 BASE_EXPORT void SplitStringAlongWhitespace(const std::string& str,
152 std::vector<std::string>* result);
153
154 } // namespace base
155
156 #endif // BASE_STRINGS_STRING_SPLIT_H_
OLDNEW
« no previous file with comments | « base/strings/string_piece_unittest.cc ('k') | base/strings/string_split.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698