string_split.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. #ifndef BASE_STRINGS_STRING_SPLIT_H_
  5. #define BASE_STRINGS_STRING_SPLIT_H_
  6. #include <string>
  7. #include <utility>
  8. #include <vector>
  9. #include "base/base_export.h"
  10. #include "base/strings/string16.h"
  11. #include "base/strings/string_piece.h"
  12. #include "build/build_config.h"
  13. namespace base {
  14. enum WhitespaceHandling {
  15. KEEP_WHITESPACE,
  16. TRIM_WHITESPACE,
  17. };
  18. enum SplitResult {
  19. // Strictly return all results.
  20. //
  21. // If the input is ",," and the separator is ',' this will return a
  22. // vector of three empty strings.
  23. SPLIT_WANT_ALL,
  24. // Only nonempty results will be added to the results. Multiple separators
  25. // will be coalesced. Separators at the beginning and end of the input will
  26. // be ignored. With TRIM_WHITESPACE, whitespace-only results will be dropped.
  27. //
  28. // If the input is ",," and the separator is ',', this will return an empty
  29. // vector.
  30. SPLIT_WANT_NONEMPTY,
  31. };
  32. // Split the given string on ANY of the given separators, returning copies of
  33. // the result.
  34. //
  35. // Note this is inverse of JoinString() defined in string_util.h.
  36. //
  37. // To split on either commas or semicolons, keeping all whitespace:
  38. //
  39. // std::vector<std::string> tokens = base::SplitString(
  40. // input, ",;", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
  41. BASE_EXPORT std::vector<std::string> SplitString(StringPiece input,
  42. StringPiece separators,
  43. WhitespaceHandling whitespace,
  44. SplitResult result_type)
  45. WARN_UNUSED_RESULT;
  46. BASE_EXPORT std::vector<string16> SplitString(StringPiece16 input,
  47. StringPiece16 separators,
  48. WhitespaceHandling whitespace,
  49. SplitResult result_type)
  50. WARN_UNUSED_RESULT;
  51. // Like SplitString above except it returns a vector of StringPieces which
  52. // reference the original buffer without copying. Although you have to be
  53. // careful to keep the original string unmodified, this provides an efficient
  54. // way to iterate through tokens in a string.
  55. //
  56. // Note this is inverse of JoinString() defined in string_util.h.
  57. //
  58. // To iterate through all whitespace-separated tokens in an input string:
  59. //
  60. // for (const auto& cur :
  61. // base::SplitStringPiece(input, base::kWhitespaceASCII,
  62. // base::KEEP_WHITESPACE,
  63. // base::SPLIT_WANT_NONEMPTY)) {
  64. // ...
  65. BASE_EXPORT std::vector<StringPiece> SplitStringPiece(
  66. StringPiece input,
  67. StringPiece separators,
  68. WhitespaceHandling whitespace,
  69. SplitResult result_type) WARN_UNUSED_RESULT;
  70. BASE_EXPORT std::vector<StringPiece16> SplitStringPiece(
  71. StringPiece16 input,
  72. StringPiece16 separators,
  73. WhitespaceHandling whitespace,
  74. SplitResult result_type) WARN_UNUSED_RESULT;
  75. using StringPairs = std::vector<std::pair<std::string, std::string>>;
  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. BASE_EXPORT bool SplitStringIntoKeyValuePairs(StringPiece input,
  81. char key_value_delimiter,
  82. char key_value_pair_delimiter,
  83. StringPairs* key_value_pairs);
  84. // Similar to SplitStringIntoKeyValuePairs, but use a substring
  85. // |key_value_pair_delimiter| instead of a single char.
  86. BASE_EXPORT bool SplitStringIntoKeyValuePairsUsingSubstr(
  87. StringPiece input,
  88. char key_value_delimiter,
  89. StringPiece key_value_pair_delimiter,
  90. StringPairs* key_value_pairs);
  91. // Similar to SplitString, but use a substring delimiter instead of a list of
  92. // characters that are all possible delimiters.
  93. BASE_EXPORT std::vector<string16> SplitStringUsingSubstr(
  94. StringPiece16 input,
  95. StringPiece16 delimiter,
  96. WhitespaceHandling whitespace,
  97. SplitResult result_type) WARN_UNUSED_RESULT;
  98. BASE_EXPORT std::vector<std::string> SplitStringUsingSubstr(
  99. StringPiece input,
  100. StringPiece delimiter,
  101. WhitespaceHandling whitespace,
  102. SplitResult result_type) WARN_UNUSED_RESULT;
  103. // Like SplitStringUsingSubstr above except it returns a vector of StringPieces
  104. // which reference the original buffer without copying. Although you have to be
  105. // careful to keep the original string unmodified, this provides an efficient
  106. // way to iterate through tokens in a string.
  107. //
  108. // To iterate through all newline-separated tokens in an input string:
  109. //
  110. // for (const auto& cur :
  111. // base::SplitStringUsingSubstr(input, "\r\n",
  112. // base::KEEP_WHITESPACE,
  113. // base::SPLIT_WANT_NONEMPTY)) {
  114. // ...
  115. BASE_EXPORT std::vector<StringPiece16> SplitStringPieceUsingSubstr(
  116. StringPiece16 input,
  117. StringPiece16 delimiter,
  118. WhitespaceHandling whitespace,
  119. SplitResult result_type) WARN_UNUSED_RESULT;
  120. BASE_EXPORT std::vector<StringPiece> SplitStringPieceUsingSubstr(
  121. StringPiece input,
  122. StringPiece delimiter,
  123. WhitespaceHandling whitespace,
  124. SplitResult result_type) WARN_UNUSED_RESULT;
  125. } // namespace base
  126. #if defined(OS_WIN)
  127. #include "base/strings/string_split_win.h"
  128. #endif
  129. #endif // BASE_STRINGS_STRING_SPLIT_H_