blob: 6f8fc7ca1a8a0b781d357d6997f9d7480d3d07a6 [file] [log] [blame]
Scott Graham66962112018-06-08 12:42:08 -07001// 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
Scott Graham66962112018-06-08 12:42:08 -070012#include "base/strings/string16.h"
13#include "base/strings/string_piece.h"
14
15namespace base {
16
17enum WhitespaceHandling {
18 KEEP_WHITESPACE,
19 TRIM_WHITESPACE,
20};
21
22enum SplitResult {
23 // Strictly return all results.
24 //
25 // If the input is ",," and the separator is ',' this will return a
26 // vector of three empty strings.
27 SPLIT_WANT_ALL,
28
29 // Only nonempty results will be added to the results. Multiple separators
30 // will be coalesced. Separators at the beginning and end of the input will
31 // be ignored. With TRIM_WHITESPACE, whitespace-only results will be dropped.
32 //
33 // If the input is ",," and the separator is ',', this will return an empty
34 // vector.
35 SPLIT_WANT_NONEMPTY,
36};
37
38// Split the given string on ANY of the given separators, returning copies of
39// the result.
40//
41// To split on either commas or semicolons, keeping all whitespace:
42//
43// std::vector<std::string> tokens = base::SplitString(
44// input, ",;", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
Scott Graham98cd3ca2018-06-14 22:26:55 -070045std::vector<std::string> SplitString(StringPiece input,
46 StringPiece separators,
47 WhitespaceHandling whitespace,
48 SplitResult result_type);
49std::vector<string16> SplitString(StringPiece16 input,
50 StringPiece16 separators,
51 WhitespaceHandling whitespace,
52 SplitResult result_type);
Scott Graham66962112018-06-08 12:42:08 -070053
54// Like SplitString above except it returns a vector of StringPieces which
55// reference the original buffer without copying. Although you have to be
56// careful to keep the original string unmodified, this provides an efficient
57// way to iterate through tokens in a string.
58//
59// To iterate through all whitespace-separated tokens in an input string:
60//
61// for (const auto& cur :
62// base::SplitStringPiece(input, base::kWhitespaceASCII,
63// base::KEEP_WHITESPACE,
64// base::SPLIT_WANT_NONEMPTY)) {
65// ...
Scott Graham98cd3ca2018-06-14 22:26:55 -070066std::vector<StringPiece> SplitStringPiece(StringPiece input,
67 StringPiece separators,
68 WhitespaceHandling whitespace,
69 SplitResult result_type);
70std::vector<StringPiece16> SplitStringPiece(StringPiece16 input,
71 StringPiece16 separators,
72 WhitespaceHandling whitespace,
73 SplitResult result_type);
Scott Graham66962112018-06-08 12:42:08 -070074
75using StringPairs = std::vector<std::pair<std::string, std::string>>;
76
77// Splits |line| into key value pairs according to the given delimiters and
78// removes whitespace leading each key and trailing each value. Returns true
79// only if each pair has a non-empty key and value. |key_value_pairs| will
80// include ("","") pairs for entries without |key_value_delimiter|.
Scott Graham44598072018-06-14 22:01:37 -070081bool SplitStringIntoKeyValuePairs(StringPiece input,
Scott Graham98cd3ca2018-06-14 22:26:55 -070082 char key_value_delimiter,
83 char key_value_pair_delimiter,
84 StringPairs* key_value_pairs);
Scott Graham66962112018-06-08 12:42:08 -070085
86// Similar to SplitString, but use a substring delimiter instead of a list of
87// characters that are all possible delimiters.
Scott Graham98cd3ca2018-06-14 22:26:55 -070088std::vector<string16> SplitStringUsingSubstr(StringPiece16 input,
89 StringPiece16 delimiter,
90 WhitespaceHandling whitespace,
91 SplitResult result_type);
92std::vector<std::string> SplitStringUsingSubstr(StringPiece input,
93 StringPiece delimiter,
94 WhitespaceHandling whitespace,
95 SplitResult result_type);
Scott Graham66962112018-06-08 12:42:08 -070096
97// Like SplitStringUsingSubstr above except it returns a vector of StringPieces
98// which reference the original buffer without copying. Although you have to be
99// careful to keep the original string unmodified, this provides an efficient
100// way to iterate through tokens in a string.
101//
102// To iterate through all newline-separated tokens in an input string:
103//
104// for (const auto& cur :
105// base::SplitStringUsingSubstr(input, "\r\n",
106// base::KEEP_WHITESPACE,
107// base::SPLIT_WANT_NONEMPTY)) {
108// ...
Scott Graham44598072018-06-14 22:01:37 -0700109std::vector<StringPiece16> SplitStringPieceUsingSubstr(
Scott Graham66962112018-06-08 12:42:08 -0700110 StringPiece16 input,
111 StringPiece16 delimiter,
112 WhitespaceHandling whitespace,
113 SplitResult result_type);
Scott Graham44598072018-06-14 22:01:37 -0700114std::vector<StringPiece> SplitStringPieceUsingSubstr(
Scott Graham66962112018-06-08 12:42:08 -0700115 StringPiece input,
116 StringPiece delimiter,
117 WhitespaceHandling whitespace,
118 SplitResult result_type);
119
120} // namespace base
121
122#endif // BASE_STRINGS_STRING_SPLIT_H_