string_split.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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, ", WARN_UNUSED_RESULT;", base::KEEP_WHITESPACE,
  41. // base::SPLIT_WANT_ALL) WARN_UNUSED_RESULT;
  42. BASE_EXPORT std::vector<std::string> SplitString(StringPiece input,
  43. StringPiece separators,
  44. WhitespaceHandling whitespace,
  45. SplitResult result_type)
  46. WARN_UNUSED_RESULT;
  47. BASE_EXPORT std::vector<string16> SplitString(StringPiece16 input,
  48. StringPiece16 separators,
  49. WhitespaceHandling whitespace,
  50. SplitResult result_type)
  51. WARN_UNUSED_RESULT;
  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. // Note this is inverse of JoinString() defined in string_util.h.
  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. // ...
  66. BASE_EXPORT std::vector<StringPiece> SplitStringPiece(
  67. StringPiece input,
  68. StringPiece separators,
  69. WhitespaceHandling whitespace,
  70. SplitResult result_type) WARN_UNUSED_RESULT;
  71. BASE_EXPORT std::vector<StringPiece16> SplitStringPiece(
  72. StringPiece16 input,
  73. StringPiece16 separators,
  74. WhitespaceHandling whitespace,
  75. SplitResult result_type) WARN_UNUSED_RESULT;
  76. using StringPairs = std::vector<std::pair<std::string, std::string>>;
  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|.
  81. BASE_EXPORT bool SplitStringIntoKeyValuePairs(StringPiece input,
  82. char key_value_delimiter,
  83. char key_value_pair_delimiter,
  84. StringPairs* key_value_pairs);
  85. // Similar to SplitStringIntoKeyValuePairs, but use a substring
  86. // |key_value_pair_delimiter| instead of a single char.
  87. BASE_EXPORT bool SplitStringIntoKeyValuePairsUsingSubstr(
  88. StringPiece input,
  89. char key_value_delimiter,
  90. StringPiece key_value_pair_delimiter,
  91. StringPairs* key_value_pairs);
  92. // Similar to SplitString, but use a substring delimiter instead of a list of
  93. // characters that are all possible delimiters.
  94. BASE_EXPORT std::vector<string16> SplitStringUsingSubstr(
  95. StringPiece16 input,
  96. StringPiece16 delimiter,
  97. WhitespaceHandling whitespace,
  98. SplitResult result_type) WARN_UNUSED_RESULT;
  99. BASE_EXPORT std::vector<std::string> SplitStringUsingSubstr(
  100. StringPiece input,
  101. StringPiece delimiter,
  102. WhitespaceHandling whitespace,
  103. SplitResult result_type) WARN_UNUSED_RESULT;
  104. // Like SplitStringUsingSubstr above except it returns a vector of StringPieces
  105. // which reference the original buffer without copying. Although you have to be
  106. // careful to keep the original string unmodified, this provides an efficient
  107. // way to iterate through tokens in a string.
  108. //
  109. // To iterate through all newline-separated tokens in an input string:
  110. //
  111. // for (const auto& cur :
  112. // base::SplitStringUsingSubstr(input, "\r\n",
  113. // base::KEEP_WHITESPACE,
  114. // base::SPLIT_WANT_NONEMPTY)) {
  115. // ...
  116. BASE_EXPORT std::vector<StringPiece16> SplitStringPieceUsingSubstr(
  117. StringPiece16 input,
  118. StringPiece16 delimiter,
  119. WhitespaceHandling whitespace,
  120. SplitResult result_type) WARN_UNUSED_RESULT;
  121. BASE_EXPORT std::vector<StringPiece> SplitStringPieceUsingSubstr(
  122. StringPiece input,
  123. StringPiece delimiter,
  124. WhitespaceHandling whitespace,
  125. SplitResult result_type) WARN_UNUSED_RESULT;
  126. } // namespace base
  127. #if defined(OS_WIN)
  128. #include "base/strings/string_split_win.h"
  129. #endif
  130. #endif // BASE_STRINGS_STRING_SPLIT_H_