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 <utility> 10 #include <vector> 11 12 #include "base/base_export.h" 13 #include "base/strings/string16.h" 14 15 namespace base { 16 17 // Splits |str| into a vector of strings delimited by |c|, placing the results 18 // in |r|. If several instances of |c| are contiguous, or if |str| begins with 19 // or ends with |c|, then an empty string is inserted. 20 // 21 // Every substring is trimmed of any leading or trailing white space. 22 // NOTE: |c| must be in BMP (Basic Multilingual Plane) 23 BASE_EXPORT void SplitString(const string16& str, 24 char16 c, 25 std::vector<string16>* r); 26 // |str| should not be in a multi-byte encoding like Shift-JIS or GBK in which 27 // the trailing byte of a multi-byte character can be in the ASCII range. 28 // UTF-8, and other single/multi-byte ASCII-compatible encodings are OK. 29 // Note: |c| must be in the ASCII range. 30 BASE_EXPORT void SplitString(const std::string& str, 31 char c, 32 std::vector<std::string>* r); 33 34 BASE_EXPORT bool SplitStringIntoKeyValues(const std::string& line, 35 char key_value_delimiter, 36 std::string* key, 37 std::vector<std::string>* values); 38 39 typedef std::vector<std::pair<std::string, std::string> > StringPairs;; 40 41 BASE_EXPORT bool SplitStringIntoKeyValuePairs( 42 const std::string& line, 43 char key_value_delimiter, 44 char key_value_pair_delimiter, 45 StringPairs* key_value_pairs); 46 47 // The same as SplitString, but use a substring delimiter instead of a char. 48 BASE_EXPORT void SplitStringUsingSubstr(const string16& str, 49 const string16& s, 50 std::vector<string16>* r); 51 BASE_EXPORT void SplitStringUsingSubstr(const std::string& str, 52 const std::string& s, 53 std::vector<std::string>* r); 54 55 // The same as SplitString, but don't trim white space. 56 // NOTE: |c| must be in BMP (Basic Multilingual Plane) 57 BASE_EXPORT void SplitStringDontTrim(const string16& str, 58 char16 c, 59 std::vector<string16>* r); 60 // |str| should not be in a multi-byte encoding like Shift-JIS or GBK in which 61 // the trailing byte of a multi-byte character can be in the ASCII range. 62 // UTF-8, and other single/multi-byte ASCII-compatible encodings are OK. 63 // Note: |c| must be in the ASCII range. 64 BASE_EXPORT void SplitStringDontTrim(const std::string& str, 65 char c, 66 std::vector<std::string>* r); 67 68 // WARNING: this uses whitespace as defined by the HTML5 spec. If you need 69 // a function similar to this but want to trim all types of whitespace, then 70 // factor this out into a function that takes a string containing the characters 71 // that are treated as whitespace. 72 // 73 // Splits the string along whitespace (where whitespace is the five space 74 // characters defined by HTML 5). Each contiguous block of non-whitespace 75 // characters is added to result. 76 BASE_EXPORT void SplitStringAlongWhitespace(const string16& str, 77 std::vector<string16>* result); 78 BASE_EXPORT void SplitStringAlongWhitespace(const std::string& str, 79 std::vector<std::string>* result); 80 81 } // namespace base 82 83 #endif // BASE_STRINGS_STRING_SPLIT_H_ 84