Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_STRINGS_STRING_SPLIT_H_ | 5 #ifndef BASE_STRINGS_STRING_SPLIT_H_ |
| 6 #define BASE_STRINGS_STRING_SPLIT_H_ | 6 #define BASE_STRINGS_STRING_SPLIT_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/base_export.h" | 12 #include "base/base_export.h" |
| 13 #include "base/strings/string16.h" | 13 #include "base/strings/string16.h" |
| 14 #include "base/strings/string_piece.h" | |
| 14 | 15 |
| 15 namespace base { | 16 namespace base { |
| 16 | 17 |
| 17 // Splits |str| into a vector of strings delimited by |c|, placing the results | 18 enum WhitespaceHandling { |
| 18 // in |r|. If several instances of |c| are contiguous, or if |str| begins with | 19 KEEP_WHITESPACE, |
| 19 // or ends with |c|, then an empty string is inserted. | 20 TRIM_WHITESPACE, |
| 21 }; | |
| 22 | |
| 23 enum SplitResult { | |
| 24 // Strictly return all results. | |
| 25 // | |
| 26 // If the input is ",," and the separator is ',', this will return a | |
| 27 // vector of three empty strings. | |
| 28 SPLIT_WANT_ALL, | |
| 29 | |
| 30 // Only nonempty results will be added to the results. Multiple separators | |
| 31 // will be coalesced. Separators at the beginning and end of the input will | |
| 32 // be ignored. With TRIM_WHITESPACE, whitespace-only results will be dropped. | |
| 33 // | |
| 34 // If the input is ",," and the separator is ',', this will return an empty | |
| 35 // vector. | |
| 36 SPLIT_WANT_NONEMPTY, | |
| 37 }; | |
| 38 | |
| 39 // Split the given string on ANY of the given separators, returning copies of | |
| 40 // the result. | |
| 20 // | 41 // |
| 21 // Every substring is trimmed of any leading or trailing white space. | 42 // If the string ends in a separator, it will be ignored rather than treating |
| 22 // NOTE: |c| must be in BMP (Basic Multilingual Plane) | 43 // that as a last empty element. |
| 23 BASE_EXPORT void SplitString(const string16& str, | 44 BASE_EXPORT std::vector<std::string> SplitString( |
| 24 char16 c, | 45 StringPiece input, |
| 25 std::vector<string16>* r); | 46 StringPiece separators, |
| 47 WhitespaceHandling whitespace, | |
| 48 SplitResult result_type); | |
| 49 BASE_EXPORT std::vector<string16> SplitString( | |
| 50 StringPiece16 input, | |
| 51 StringPiece16 separators, | |
| 52 WhitespaceHandling whitespace, | |
| 53 SplitResult result_type); | |
| 26 | 54 |
| 27 // |str| should not be in a multi-byte encoding like Shift-JIS or GBK in which | 55 // As SplitString above except returns a vector of StringPieces which referece |
|
danakj
2015/06/11 23:58:34
reference
| |
| 28 // the trailing byte of a multi-byte character can be in the ASCII range. | 56 // the original buffer without copying. |
| 29 // UTF-8, and other single/multi-byte ASCII-compatible encodings are OK. | 57 BASE_EXPORT std::vector<StringPiece> SplitStringPiece( |
| 30 // Note: |c| must be in the ASCII range. | 58 StringPiece input, |
| 31 BASE_EXPORT void SplitString(const std::string& str, | 59 StringPiece separators, |
| 32 char c, | 60 WhitespaceHandling whitespace, |
| 33 std::vector<std::string>* r); | 61 SplitResult result_type); |
| 62 BASE_EXPORT std::vector<StringPiece16> SplitStringPiece( | |
| 63 StringPiece16 input, | |
| 64 StringPiece16 separators, | |
| 65 WhitespaceHandling whitespace, | |
| 66 SplitResult result_type); | |
| 34 | 67 |
| 35 typedef std::vector<std::pair<std::string, std::string> > StringPairs; | 68 typedef std::vector<std::pair<std::string, std::string> > StringPairs; |
| 36 | 69 |
| 37 // Splits |line| into key value pairs according to the given delimiters and | 70 // Splits |line| into key value pairs according to the given delimiters and |
| 38 // removes whitespace leading each key and trailing each value. Returns true | 71 // removes whitespace leading each key and trailing each value. Returns true |
| 39 // only if each pair has a non-empty key and value. |key_value_pairs| will | 72 // only if each pair has a non-empty key and value. |key_value_pairs| will |
| 40 // include ("","") pairs for entries without |key_value_delimiter|. | 73 // include ("","") pairs for entries without |key_value_delimiter|. |
| 41 BASE_EXPORT bool SplitStringIntoKeyValuePairs(const std::string& line, | 74 BASE_EXPORT bool SplitStringIntoKeyValuePairs(const std::string& line, |
| 42 char key_value_delimiter, | 75 char key_value_delimiter, |
| 43 char key_value_pair_delimiter, | 76 char key_value_pair_delimiter, |
| 44 StringPairs* key_value_pairs); | 77 StringPairs* key_value_pairs); |
| 45 | 78 |
| 46 // The same as SplitString, but use a substring delimiter instead of a char. | 79 // The same as SplitString, but use a substring delimiter instead of a char. |
| 47 BASE_EXPORT void SplitStringUsingSubstr(const string16& str, | 80 BASE_EXPORT void SplitStringUsingSubstr(const string16& str, |
| 48 const string16& s, | 81 const string16& s, |
| 49 std::vector<string16>* r); | 82 std::vector<string16>* r); |
| 50 BASE_EXPORT void SplitStringUsingSubstr(const std::string& str, | 83 BASE_EXPORT void SplitStringUsingSubstr(const std::string& str, |
| 51 const std::string& s, | 84 const std::string& s, |
| 52 std::vector<std::string>* r); | 85 std::vector<std::string>* r); |
| 53 | 86 |
| 87 // ----------------------------------------------------------------------------- | |
| 88 // Backwards-compat wrappers | |
|
danakj
2015/06/11 23:58:35
Wondering, are you planning to remove these?
brettw
2015/06/12 17:37:08
Added a TODO
| |
| 89 // | |
| 90 // New code should use one of the more general variants above. | |
| 91 | |
| 92 // Splits |str| into a vector of strings delimited by |c|, placing the results | |
| 93 // in |r|. If several instances of |c| are contiguous, or if |str| begins with | |
| 94 // or ends with |c|, then an empty string is inserted. | |
| 95 // | |
| 96 // Every substring is trimmed of any leading or trailing white space. | |
| 97 // NOTE: |c| must be in BMP (Basic Multilingual Plane) | |
| 98 BASE_EXPORT void SplitString(const string16& str, | |
| 99 char16 c, | |
| 100 std::vector<string16>* r); | |
| 101 | |
| 102 // |str| should not be in a multi-byte encoding like Shift-JIS or GBK in which | |
| 103 // the trailing byte of a multi-byte character can be in the ASCII range. | |
| 104 // UTF-8, and other single/multi-byte ASCII-compatible encodings are OK. | |
| 105 // Note: |c| must be in the ASCII range. | |
| 106 BASE_EXPORT void SplitString(const std::string& str, | |
| 107 char c, | |
| 108 std::vector<std::string>* r); | |
| 109 | |
| 54 // The same as SplitString, but don't trim white space. | 110 // The same as SplitString, but don't trim white space. |
| 55 // NOTE: |c| must be in BMP (Basic Multilingual Plane) | 111 // NOTE: |c| must be in BMP (Basic Multilingual Plane) |
| 56 BASE_EXPORT void SplitStringDontTrim(const string16& str, | 112 BASE_EXPORT void SplitStringDontTrim(StringPiece16 str, |
| 57 char16 c, | 113 char16 c, |
| 58 std::vector<string16>* r); | 114 std::vector<string16>* r); |
| 59 // |str| should not be in a multi-byte encoding like Shift-JIS or GBK in which | 115 // |str| should not be in a multi-byte encoding like Shift-JIS or GBK in which |
| 60 // the trailing byte of a multi-byte character can be in the ASCII range. | 116 // the trailing byte of a multi-byte character can be in the ASCII range. |
| 61 // UTF-8, and other single/multi-byte ASCII-compatible encodings are OK. | 117 // UTF-8, and other single/multi-byte ASCII-compatible encodings are OK. |
| 62 // Note: |c| must be in the ASCII range. | 118 // Note: |c| must be in the ASCII range. |
| 63 BASE_EXPORT void SplitStringDontTrim(const std::string& str, | 119 BASE_EXPORT void SplitStringDontTrim(StringPiece str, |
| 64 char c, | 120 char c, |
| 65 std::vector<std::string>* r); | 121 std::vector<std::string>* result); |
| 66 | 122 |
| 67 // WARNING: this uses whitespace as defined by the HTML5 spec. If you need | 123 // WARNING: this uses whitespace as defined by the HTML5 spec (ASCII whitespace |
| 68 // a function similar to this but want to trim all types of whitespace, then | 124 // only). |
| 69 // factor this out into a function that takes a string containing the characters | |
| 70 // that are treated as whitespace. | |
| 71 // | 125 // |
| 72 // Splits the string along whitespace (where whitespace is the five space | 126 // The difference between this and calling SplitString with the whitespace |
| 73 // characters defined by HTML 5). Each contiguous block of non-whitespace | 127 // characters as separators is the treatment of the first element when the |
| 74 // characters is added to result. | 128 // string starts with whitespace. |
| 129 // | |
| 130 // Input SplitString SplitStringAlongWhitespace | |
| 131 // -------------------------------------------------------- | |
| 132 // " a " "", "a" "a" | |
| 75 BASE_EXPORT void SplitStringAlongWhitespace(const string16& str, | 133 BASE_EXPORT void SplitStringAlongWhitespace(const string16& str, |
| 76 std::vector<string16>* result); | 134 std::vector<string16>* result); |
| 77 BASE_EXPORT void SplitStringAlongWhitespace(const std::string& str, | 135 BASE_EXPORT void SplitStringAlongWhitespace(const std::string& str, |
| 78 std::vector<std::string>* result); | 136 std::vector<std::string>* result); |
| 79 | 137 |
| 80 } // namespace base | 138 } // namespace base |
| 81 | 139 |
| 82 #endif // BASE_STRINGS_STRING_SPLIT_H_ | 140 #endif // BASE_STRINGS_STRING_SPLIT_H_ |
| OLD | NEW |