blob: 6c7895d33e8eb9e249435f4bd8600b0cfa260fb1 [file] [log] [blame]
[email protected]f5661ca2011-03-24 19:00:201// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]0477554f2010-01-21 19:29:252// 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_
[email protected]32b76ef2010-07-26 23:08:247#pragma once
[email protected]0477554f2010-01-21 19:29:258
9#include <string>
10#include <utility>
11#include <vector>
12
[email protected]0bea7252011-08-05 15:34:0013#include "base/base_export.h"
[email protected]e8478ae2010-09-02 02:01:4814#include "base/string16.h"
15
[email protected]76eb0242010-10-14 00:35:3616namespace base {
[email protected]4e5ae20f2010-09-24 04:52:1117
18// Splits |str| into a vector of strings delimited by |s|. Append the results
19// into |r| as they appear. If several instances of |s| are contiguous, or if
20// |str| begins with or ends with |s|, then an empty string is inserted.
21//
22// Every substring is trimmed of any leading or trailing white space.
[email protected]4e5ae20f2010-09-24 04:52:1123// NOTE: |c| must be in BMP (Basic Multilingual Plane)
[email protected]0bea7252011-08-05 15:34:0024BASE_EXPORT void SplitString(const string16& str,
25 char16 c,
26 std::vector<string16>* r);
[email protected]4e5ae20f2010-09-24 04:52:1127// |str| should not be in a multi-byte encoding like Shift-JIS or GBK in which
28// the trailing byte of a multi-byte character can be in the ASCII range.
29// UTF-8, and other single/multi-byte ASCII-compatible encodings are OK.
30// Note: |c| must be in the ASCII range.
[email protected]0bea7252011-08-05 15:34:0031BASE_EXPORT void SplitString(const std::string& str,
32 char c,
33 std::vector<std::string>* r);
[email protected]4e5ae20f2010-09-24 04:52:1134
[email protected]0bea7252011-08-05 15:34:0035BASE_EXPORT bool SplitStringIntoKeyValues(
[email protected]0477554f2010-01-21 19:29:2536 const std::string& line,
37 char key_value_delimiter,
38 std::string* key, std::vector<std::string>* values);
39
[email protected]0bea7252011-08-05 15:34:0040BASE_EXPORT bool SplitStringIntoKeyValuePairs(
[email protected]0477554f2010-01-21 19:29:2541 const std::string& line,
42 char key_value_delimiter,
43 char key_value_pair_delimiter,
44 std::vector<std::pair<std::string, std::string> >* kv_pairs);
45
[email protected]e8478ae2010-09-02 02:01:4846// The same as SplitString, but use a substring delimiter instead of a char.
[email protected]0bea7252011-08-05 15:34:0047BASE_EXPORT void SplitStringUsingSubstr(const string16& str,
48 const string16& s,
49 std::vector<string16>* r);
50BASE_EXPORT void SplitStringUsingSubstr(const std::string& str,
51 const std::string& s,
52 std::vector<std::string>* r);
[email protected]e8478ae2010-09-02 02:01:4853
[email protected]7594f6d2010-09-15 13:36:2254// The same as SplitString, but don't trim white space.
[email protected]7594f6d2010-09-15 13:36:2255// NOTE: |c| must be in BMP (Basic Multilingual Plane)
[email protected]0bea7252011-08-05 15:34:0056BASE_EXPORT void SplitStringDontTrim(const string16& str,
57 char16 c,
58 std::vector<string16>* r);
[email protected]7594f6d2010-09-15 13:36:2259// |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.
61// UTF-8, and other single/multi-byte ASCII-compatible encodings are OK.
62// Note: |c| must be in the ASCII range.
[email protected]0bea7252011-08-05 15:34:0063BASE_EXPORT void SplitStringDontTrim(const std::string& str,
64 char c,
65 std::vector<std::string>* r);
[email protected]7594f6d2010-09-15 13:36:2266
[email protected]b87c4a72010-11-15 22:03:4267// WARNING: this uses whitespace as defined by the HTML5 spec. If you need
68// a function similar to this but want to trim all types of whitespace, then
69// factor this out into a function that takes a string containing the characters
70// that are treated as whitespace.
71//
72// Splits the string along whitespace (where whitespace is the five space
73// characters defined by HTML 5). Each contiguous block of non-whitespace
74// characters is added to result.
[email protected]0bea7252011-08-05 15:34:0075BASE_EXPORT void SplitStringAlongWhitespace(const string16& str,
76 std::vector<string16>* result);
77BASE_EXPORT void SplitStringAlongWhitespace(const std::string& str,
78 std::vector<std::string>* result);
[email protected]b87c4a72010-11-15 22:03:4279
[email protected]0477554f2010-01-21 19:29:2580} // namespace base
81
82#endif // BASE_STRING_SPLIT_H