16d528ed9Sopenharmony_ci// Copyright (c) 2012 The Chromium Authors. All rights reserved.
26d528ed9Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
36d528ed9Sopenharmony_ci// found in the LICENSE file.
46d528ed9Sopenharmony_ci
56d528ed9Sopenharmony_ci#ifndef BASE_STRINGS_STRING_SPLIT_H_
66d528ed9Sopenharmony_ci#define BASE_STRINGS_STRING_SPLIT_H_
76d528ed9Sopenharmony_ci
86d528ed9Sopenharmony_ci#include <string>
96d528ed9Sopenharmony_ci#include <string_view>
106d528ed9Sopenharmony_ci#include <utility>
116d528ed9Sopenharmony_ci#include <vector>
126d528ed9Sopenharmony_ci
136d528ed9Sopenharmony_cinamespace base {
146d528ed9Sopenharmony_ci
156d528ed9Sopenharmony_cienum WhitespaceHandling {
166d528ed9Sopenharmony_ci  KEEP_WHITESPACE,
176d528ed9Sopenharmony_ci  TRIM_WHITESPACE,
186d528ed9Sopenharmony_ci};
196d528ed9Sopenharmony_ci
206d528ed9Sopenharmony_cienum SplitResult {
216d528ed9Sopenharmony_ci  // Strictly return all results.
226d528ed9Sopenharmony_ci  //
236d528ed9Sopenharmony_ci  // If the input is ",," and the separator is ',' this will return a
246d528ed9Sopenharmony_ci  // vector of three empty strings.
256d528ed9Sopenharmony_ci  SPLIT_WANT_ALL,
266d528ed9Sopenharmony_ci
276d528ed9Sopenharmony_ci  // Only nonempty results will be added to the results. Multiple separators
286d528ed9Sopenharmony_ci  // will be coalesced. Separators at the beginning and end of the input will
296d528ed9Sopenharmony_ci  // be ignored. With TRIM_WHITESPACE, whitespace-only results will be dropped.
306d528ed9Sopenharmony_ci  //
316d528ed9Sopenharmony_ci  // If the input is ",," and the separator is ',', this will return an empty
326d528ed9Sopenharmony_ci  // vector.
336d528ed9Sopenharmony_ci  SPLIT_WANT_NONEMPTY,
346d528ed9Sopenharmony_ci};
356d528ed9Sopenharmony_ci
366d528ed9Sopenharmony_ci// Split the given string on ANY of the given separators, returning copies of
376d528ed9Sopenharmony_ci// the result.
386d528ed9Sopenharmony_ci//
396d528ed9Sopenharmony_ci// To split on either commas or semicolons, keeping all whitespace:
406d528ed9Sopenharmony_ci//
416d528ed9Sopenharmony_ci//   std::vector<std::string> tokens = base::SplitString(
426d528ed9Sopenharmony_ci//       input, ",;", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
436d528ed9Sopenharmony_cistd::vector<std::string> SplitString(std::string_view input,
446d528ed9Sopenharmony_ci                                     std::string_view separators,
456d528ed9Sopenharmony_ci                                     WhitespaceHandling whitespace,
466d528ed9Sopenharmony_ci                                     SplitResult result_type);
476d528ed9Sopenharmony_cistd::vector<std::u16string> SplitString(std::u16string_view input,
486d528ed9Sopenharmony_ci                                        std::u16string_view separators,
496d528ed9Sopenharmony_ci                                        WhitespaceHandling whitespace,
506d528ed9Sopenharmony_ci                                        SplitResult result_type);
516d528ed9Sopenharmony_ci
526d528ed9Sopenharmony_ci// Like SplitString above except it returns a vector of StringPieces which
536d528ed9Sopenharmony_ci// reference the original buffer without copying. Although you have to be
546d528ed9Sopenharmony_ci// careful to keep the original string unmodified, this provides an efficient
556d528ed9Sopenharmony_ci// way to iterate through tokens in a string.
566d528ed9Sopenharmony_ci//
576d528ed9Sopenharmony_ci// To iterate through all whitespace-separated tokens in an input string:
586d528ed9Sopenharmony_ci//
596d528ed9Sopenharmony_ci//   for (const auto& cur :
606d528ed9Sopenharmony_ci//        base::SplitStringPiece(input, base::kWhitespaceASCII,
616d528ed9Sopenharmony_ci//                               base::KEEP_WHITESPACE,
626d528ed9Sopenharmony_ci//                               base::SPLIT_WANT_NONEMPTY)) {
636d528ed9Sopenharmony_ci//     ...
646d528ed9Sopenharmony_cistd::vector<std::string_view> SplitStringPiece(std::string_view input,
656d528ed9Sopenharmony_ci                                               std::string_view separators,
666d528ed9Sopenharmony_ci                                               WhitespaceHandling whitespace,
676d528ed9Sopenharmony_ci                                               SplitResult result_type);
686d528ed9Sopenharmony_cistd::vector<std::u16string_view> SplitStringPiece(
696d528ed9Sopenharmony_ci    std::u16string_view input,
706d528ed9Sopenharmony_ci    std::u16string_view separators,
716d528ed9Sopenharmony_ci    WhitespaceHandling whitespace,
726d528ed9Sopenharmony_ci    SplitResult result_type);
736d528ed9Sopenharmony_ci
746d528ed9Sopenharmony_ciusing StringPairs = std::vector<std::pair<std::string, std::string>>;
756d528ed9Sopenharmony_ci
766d528ed9Sopenharmony_ci// Splits |line| into key value pairs according to the given delimiters and
776d528ed9Sopenharmony_ci// removes whitespace leading each key and trailing each value. Returns true
786d528ed9Sopenharmony_ci// only if each pair has a non-empty key and value. |key_value_pairs| will
796d528ed9Sopenharmony_ci// include ("","") pairs for entries without |key_value_delimiter|.
806d528ed9Sopenharmony_cibool SplitStringIntoKeyValuePairs(std::string_view input,
816d528ed9Sopenharmony_ci                                  char key_value_delimiter,
826d528ed9Sopenharmony_ci                                  char key_value_pair_delimiter,
836d528ed9Sopenharmony_ci                                  StringPairs* key_value_pairs);
846d528ed9Sopenharmony_ci
856d528ed9Sopenharmony_ci// Similar to SplitString, but use a substring delimiter instead of a list of
866d528ed9Sopenharmony_ci// characters that are all possible delimiters.
876d528ed9Sopenharmony_cistd::vector<std::u16string> SplitStringUsingSubstr(
886d528ed9Sopenharmony_ci    std::u16string_view input,
896d528ed9Sopenharmony_ci    std::u16string_view delimiter,
906d528ed9Sopenharmony_ci    WhitespaceHandling whitespace,
916d528ed9Sopenharmony_ci    SplitResult result_type);
926d528ed9Sopenharmony_cistd::vector<std::string> SplitStringUsingSubstr(std::string_view input,
936d528ed9Sopenharmony_ci                                                std::string_view delimiter,
946d528ed9Sopenharmony_ci                                                WhitespaceHandling whitespace,
956d528ed9Sopenharmony_ci                                                SplitResult result_type);
966d528ed9Sopenharmony_ci
976d528ed9Sopenharmony_ci// Like SplitStringUsingSubstr above except it returns a vector of StringPieces
986d528ed9Sopenharmony_ci// which reference the original buffer without copying. Although you have to be
996d528ed9Sopenharmony_ci// careful to keep the original string unmodified, this provides an efficient
1006d528ed9Sopenharmony_ci// way to iterate through tokens in a string.
1016d528ed9Sopenharmony_ci//
1026d528ed9Sopenharmony_ci// To iterate through all newline-separated tokens in an input string:
1036d528ed9Sopenharmony_ci//
1046d528ed9Sopenharmony_ci//   for (const auto& cur :
1056d528ed9Sopenharmony_ci//        base::SplitStringUsingSubstr(input, "\r\n",
1066d528ed9Sopenharmony_ci//                                     base::KEEP_WHITESPACE,
1076d528ed9Sopenharmony_ci//                                     base::SPLIT_WANT_NONEMPTY)) {
1086d528ed9Sopenharmony_ci//     ...
1096d528ed9Sopenharmony_cistd::vector<std::u16string_view> SplitStringPieceUsingSubstr(
1106d528ed9Sopenharmony_ci    std::u16string_view input,
1116d528ed9Sopenharmony_ci    std::u16string_view delimiter,
1126d528ed9Sopenharmony_ci    WhitespaceHandling whitespace,
1136d528ed9Sopenharmony_ci    SplitResult result_type);
1146d528ed9Sopenharmony_cistd::vector<std::string_view> SplitStringPieceUsingSubstr(
1156d528ed9Sopenharmony_ci    std::string_view input,
1166d528ed9Sopenharmony_ci    std::string_view delimiter,
1176d528ed9Sopenharmony_ci    WhitespaceHandling whitespace,
1186d528ed9Sopenharmony_ci    SplitResult result_type);
1196d528ed9Sopenharmony_ci
1206d528ed9Sopenharmony_ci}  // namespace base
1216d528ed9Sopenharmony_ci
1226d528ed9Sopenharmony_ci#endif  // BASE_STRINGS_STRING_SPLIT_H_
123