Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 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 | |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 12 | #include "base/strings/string16.h" |
| 13 | #include "base/strings/string_piece.h" |
| 14 | |
| 15 | namespace base { |
| 16 | |
| 17 | enum WhitespaceHandling { |
| 18 | KEEP_WHITESPACE, |
| 19 | TRIM_WHITESPACE, |
| 20 | }; |
| 21 | |
| 22 | enum 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 Graham | 98cd3ca | 2018-06-14 22:26:55 -0700 | [diff] [blame] | 45 | std::vector<std::string> SplitString(StringPiece input, |
| 46 | StringPiece separators, |
| 47 | WhitespaceHandling whitespace, |
| 48 | SplitResult result_type); |
| 49 | std::vector<string16> SplitString(StringPiece16 input, |
| 50 | StringPiece16 separators, |
| 51 | WhitespaceHandling whitespace, |
| 52 | SplitResult result_type); |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 53 | |
| 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 Graham | 98cd3ca | 2018-06-14 22:26:55 -0700 | [diff] [blame] | 66 | std::vector<StringPiece> SplitStringPiece(StringPiece input, |
| 67 | StringPiece separators, |
| 68 | WhitespaceHandling whitespace, |
| 69 | SplitResult result_type); |
| 70 | std::vector<StringPiece16> SplitStringPiece(StringPiece16 input, |
| 71 | StringPiece16 separators, |
| 72 | WhitespaceHandling whitespace, |
| 73 | SplitResult result_type); |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 74 | |
| 75 | using 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 Graham | 4459807 | 2018-06-14 22:01:37 -0700 | [diff] [blame] | 81 | bool SplitStringIntoKeyValuePairs(StringPiece input, |
Scott Graham | 98cd3ca | 2018-06-14 22:26:55 -0700 | [diff] [blame] | 82 | char key_value_delimiter, |
| 83 | char key_value_pair_delimiter, |
| 84 | StringPairs* key_value_pairs); |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 85 | |
| 86 | // Similar to SplitString, but use a substring delimiter instead of a list of |
| 87 | // characters that are all possible delimiters. |
Scott Graham | 98cd3ca | 2018-06-14 22:26:55 -0700 | [diff] [blame] | 88 | std::vector<string16> SplitStringUsingSubstr(StringPiece16 input, |
| 89 | StringPiece16 delimiter, |
| 90 | WhitespaceHandling whitespace, |
| 91 | SplitResult result_type); |
| 92 | std::vector<std::string> SplitStringUsingSubstr(StringPiece input, |
| 93 | StringPiece delimiter, |
| 94 | WhitespaceHandling whitespace, |
| 95 | SplitResult result_type); |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 96 | |
| 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 Graham | 4459807 | 2018-06-14 22:01:37 -0700 | [diff] [blame] | 109 | std::vector<StringPiece16> SplitStringPieceUsingSubstr( |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 110 | StringPiece16 input, |
| 111 | StringPiece16 delimiter, |
| 112 | WhitespaceHandling whitespace, |
| 113 | SplitResult result_type); |
Scott Graham | 4459807 | 2018-06-14 22:01:37 -0700 | [diff] [blame] | 114 | std::vector<StringPiece> SplitStringPieceUsingSubstr( |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 115 | StringPiece input, |
| 116 | StringPiece delimiter, |
| 117 | WhitespaceHandling whitespace, |
| 118 | SplitResult result_type); |
| 119 | |
| 120 | } // namespace base |
| 121 | |
| 122 | #endif // BASE_STRINGS_STRING_SPLIT_H_ |