blob: e8c677be6eb0bf67e5caf6c8caa990d5cf7dc2c7 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2012 The Chromium Authors
[email protected]5ae0b763e2013-02-07 23:01:392// 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"
brettw977caaa2015-06-12 19:57:5013#include "base/strings/string_piece.h"
Jan Wilken Dörrie7179fc92019-10-08 07:44:0514#include "build/build_config.h"
[email protected]5ae0b763e2013-02-07 23:01:3915
16namespace base {
17
brettw977caaa2015-06-12 19:57:5018enum WhitespaceHandling {
19 KEEP_WHITESPACE,
20 TRIM_WHITESPACE,
21};
22
23enum 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//
Robert Liao793242a2019-12-04 22:17:3342// Note this is inverse of JoinString() defined in string_util.h.
43//
brettw977caaa2015-06-12 19:57:5044// To split on either commas or semicolons, keeping all whitespace:
45//
46// std::vector<std::string> tokens = base::SplitString(
Hajime Hoshi3bd6f0ef2020-09-10 17:14:5047// input, ",;", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
Daniel Cheng4455c9842022-01-13 23:26:3748[[nodiscard]] BASE_EXPORT std::vector<std::string> SplitString(
49 StringPiece input,
50 StringPiece separators,
51 WhitespaceHandling whitespace,
52 SplitResult result_type);
53[[nodiscard]] BASE_EXPORT std::vector<std::u16string> SplitString(
Jan Wilken Dörrie085b2aa2021-03-12 16:26:5754 StringPiece16 input,
55 StringPiece16 separators,
56 WhitespaceHandling whitespace,
Daniel Cheng4455c9842022-01-13 23:26:3757 SplitResult result_type);
brettw977caaa2015-06-12 19:57:5058
59// Like SplitString above except it returns a vector of StringPieces which
60// reference the original buffer without copying. Although you have to be
61// careful to keep the original string unmodified, this provides an efficient
62// way to iterate through tokens in a string.
63//
Robert Liao793242a2019-12-04 22:17:3364// Note this is inverse of JoinString() defined in string_util.h.
65//
brettw977caaa2015-06-12 19:57:5066// To iterate through all whitespace-separated tokens in an input string:
67//
68// for (const auto& cur :
69// base::SplitStringPiece(input, base::kWhitespaceASCII,
70// base::KEEP_WHITESPACE,
71// base::SPLIT_WANT_NONEMPTY)) {
72// ...
Daniel Cheng4455c9842022-01-13 23:26:3773[[nodiscard]] BASE_EXPORT std::vector<StringPiece> SplitStringPiece(
brettw977caaa2015-06-12 19:57:5074 StringPiece input,
75 StringPiece separators,
76 WhitespaceHandling whitespace,
Daniel Cheng4455c9842022-01-13 23:26:3777 SplitResult result_type);
78[[nodiscard]] BASE_EXPORT std::vector<StringPiece16> SplitStringPiece(
brettw977caaa2015-06-12 19:57:5079 StringPiece16 input,
80 StringPiece16 separators,
81 WhitespaceHandling whitespace,
Daniel Cheng4455c9842022-01-13 23:26:3782 SplitResult result_type);
brettw977caaa2015-06-12 19:57:5083
84using StringPairs = std::vector<std::pair<std::string, std::string>>;
85
86// Splits |line| into key value pairs according to the given delimiters and
87// removes whitespace leading each key and trailing each value. Returns true
88// only if each pair has a non-empty key and value. |key_value_pairs| will
89// include ("","") pairs for entries without |key_value_delimiter|.
brettwce0fbef2015-08-13 22:10:0390BASE_EXPORT bool SplitStringIntoKeyValuePairs(StringPiece input,
brettw977caaa2015-06-12 19:57:5091 char key_value_delimiter,
92 char key_value_pair_delimiter,
93 StringPairs* key_value_pairs);
94
Luum Habtemariam9268d9a2018-10-29 22:43:1295// Similar to SplitStringIntoKeyValuePairs, but use a substring
96// |key_value_pair_delimiter| instead of a single char.
97BASE_EXPORT bool SplitStringIntoKeyValuePairsUsingSubstr(
98 StringPiece input,
99 char key_value_delimiter,
100 StringPiece key_value_pair_delimiter,
101 StringPairs* key_value_pairs);
102
brettw977caaa2015-06-12 19:57:50103// Similar to SplitString, but use a substring delimiter instead of a list of
104// characters that are all possible delimiters.
Daniel Cheng4455c9842022-01-13 23:26:37105[[nodiscard]] BASE_EXPORT std::vector<std::u16string> SplitStringUsingSubstr(
brettw8d858df92016-09-23 03:13:29106 StringPiece16 input,
107 StringPiece16 delimiter,
108 WhitespaceHandling whitespace,
Daniel Cheng4455c9842022-01-13 23:26:37109 SplitResult result_type);
110[[nodiscard]] BASE_EXPORT std::vector<std::string> SplitStringUsingSubstr(
brettw8d858df92016-09-23 03:13:29111 StringPiece input,
112 StringPiece delimiter,
113 WhitespaceHandling whitespace,
Daniel Cheng4455c9842022-01-13 23:26:37114 SplitResult result_type);
brettw977caaa2015-06-12 19:57:50115
arjanl0e8b35a2016-01-05 10:24:54116// Like SplitStringUsingSubstr above except it returns a vector of StringPieces
117// which reference the original buffer without copying. Although you have to be
118// careful to keep the original string unmodified, this provides an efficient
119// way to iterate through tokens in a string.
120//
121// To iterate through all newline-separated tokens in an input string:
122//
123// for (const auto& cur :
124// base::SplitStringUsingSubstr(input, "\r\n",
125// base::KEEP_WHITESPACE,
126// base::SPLIT_WANT_NONEMPTY)) {
127// ...
Daniel Cheng4455c9842022-01-13 23:26:37128[[nodiscard]] BASE_EXPORT std::vector<StringPiece16>
129SplitStringPieceUsingSubstr(StringPiece16 input,
130 StringPiece16 delimiter,
131 WhitespaceHandling whitespace,
132 SplitResult result_type);
133[[nodiscard]] BASE_EXPORT std::vector<StringPiece> SplitStringPieceUsingSubstr(
arjanl0e8b35a2016-01-05 10:24:54134 StringPiece input,
135 StringPiece delimiter,
136 WhitespaceHandling whitespace,
Daniel Cheng4455c9842022-01-13 23:26:37137 SplitResult result_type);
arjanl0e8b35a2016-01-05 10:24:54138
[email protected]5ae0b763e2013-02-07 23:01:39139} // namespace base
140
Xiaohan Wang6700dcf12022-01-15 14:47:00141#if BUILDFLAG(IS_WIN)
Jan Wilken Dörrie665969c2020-06-04 11:46:25142#include "base/strings/string_split_win.h"
143#endif
144
[email protected]5ae0b763e2013-02-07 23:01:39145#endif // BASE_STRINGS_STRING_SPLIT_H_