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 <string_view> 10 #include <utility> 11 #include <vector> 12 13 namespace base { 14 15 enum WhitespaceHandling { 16 KEEP_WHITESPACE, 17 TRIM_WHITESPACE, 18 }; 19 20 enum SplitResult { 21 // Strictly return all results. 22 // 23 // If the input is ",," and the separator is ',' this will return a 24 // vector of three empty strings. 25 SPLIT_WANT_ALL, 26 27 // Only nonempty results will be added to the results. Multiple separators 28 // will be coalesced. Separators at the beginning and end of the input will 29 // be ignored. With TRIM_WHITESPACE, whitespace-only results will be dropped. 30 // 31 // If the input is ",," and the separator is ',', this will return an empty 32 // vector. 33 SPLIT_WANT_NONEMPTY, 34 }; 35 36 // Split the given string on ANY of the given separators, returning copies of 37 // the result. 38 // 39 // To split on either commas or semicolons, keeping all whitespace: 40 // 41 // std::vector<std::string> tokens = base::SplitString( 42 // input, ",;", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL); 43 std::vector<std::string> SplitString(std::string_view input, 44 std::string_view separators, 45 WhitespaceHandling whitespace, 46 SplitResult result_type); 47 std::vector<std::u16string> SplitString(std::u16string_view input, 48 std::u16string_view separators, 49 WhitespaceHandling whitespace, 50 SplitResult result_type); 51 52 // Like SplitString above except it returns a vector of StringPieces which 53 // reference the original buffer without copying. Although you have to be 54 // careful to keep the original string unmodified, this provides an efficient 55 // way to iterate through tokens in a string. 56 // 57 // To iterate through all whitespace-separated tokens in an input string: 58 // 59 // for (const auto& cur : 60 // base::SplitStringPiece(input, base::kWhitespaceASCII, 61 // base::KEEP_WHITESPACE, 62 // base::SPLIT_WANT_NONEMPTY)) { 63 // ... 64 std::vector<std::string_view> SplitStringPiece(std::string_view input, 65 std::string_view separators, 66 WhitespaceHandling whitespace, 67 SplitResult result_type); 68 std::vector<std::u16string_view> SplitStringPiece( 69 std::u16string_view input, 70 std::u16string_view separators, 71 WhitespaceHandling whitespace, 72 SplitResult result_type); 73 74 using StringPairs = std::vector<std::pair<std::string, std::string>>; 75 76 // Splits |line| into key value pairs according to the given delimiters and 77 // removes whitespace leading each key and trailing each value. Returns true 78 // only if each pair has a non-empty key and value. |key_value_pairs| will 79 // include ("","") pairs for entries without |key_value_delimiter|. 80 bool SplitStringIntoKeyValuePairs(std::string_view input, 81 char key_value_delimiter, 82 char key_value_pair_delimiter, 83 StringPairs* key_value_pairs); 84 85 // Similar to SplitString, but use a substring delimiter instead of a list of 86 // characters that are all possible delimiters. 87 std::vector<std::u16string> SplitStringUsingSubstr( 88 std::u16string_view input, 89 std::u16string_view delimiter, 90 WhitespaceHandling whitespace, 91 SplitResult result_type); 92 std::vector<std::string> SplitStringUsingSubstr(std::string_view input, 93 std::string_view delimiter, 94 WhitespaceHandling whitespace, 95 SplitResult result_type); 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 // ... 109 std::vector<std::u16string_view> SplitStringPieceUsingSubstr( 110 std::u16string_view input, 111 std::u16string_view delimiter, 112 WhitespaceHandling whitespace, 113 SplitResult result_type); 114 std::vector<std::string_view> SplitStringPieceUsingSubstr( 115 std::string_view input, 116 std::string_view delimiter, 117 WhitespaceHandling whitespace, 118 SplitResult result_type); 119 120 } // namespace base 121 122 #endif // BASE_STRINGS_STRING_SPLIT_H_ 123