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