string_split.h revision 3345a6884c488ff3a535c2c9acdd33d74b37e311
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
15// TODO(tfarina): Move the following functions into the namespace and update the
16// callers.
17//-----------------------------------------------------------------------------
18
19// Splits |str| into a vector of strings delimited by |s|. Append the results
20// into |r| as they appear. If several instances of |s| are contiguous, or if
21// |str| begins with or ends with |s|, then an empty string is inserted.
22//
23// Every substring is trimmed of any leading or trailing white space.
24// Where wchar_t is char16 (i.e. Windows), |c| must be in BMP
25// (Basic Multilingual Plane). Elsewhere (Linux/Mac), wchar_t
26// should be a valid Unicode code point (32-bit).
27void SplitString(const std::wstring& str,
28                 wchar_t c,
29                 std::vector<std::wstring>* r);
30// NOTE: |c| must be in BMP (Basic Multilingual Plane)
31void SplitString(const string16& str,
32                 char16 c,
33                 std::vector<string16>* r);
34// |str| should not be in a multi-byte encoding like Shift-JIS or GBK in which
35// the trailing byte of a multi-byte character can be in the ASCII range.
36// UTF-8, and other single/multi-byte ASCII-compatible encodings are OK.
37// Note: |c| must be in the ASCII range.
38void SplitString(const std::string& str,
39                 char c,
40                 std::vector<std::string>* r);
41
42namespace base {
43
44bool SplitStringIntoKeyValues(
45    const std::string& line,
46    char key_value_delimiter,
47    std::string* key, std::vector<std::string>* values);
48
49bool SplitStringIntoKeyValuePairs(
50    const std::string& line,
51    char key_value_delimiter,
52    char key_value_pair_delimiter,
53    std::vector<std::pair<std::string, std::string> >* kv_pairs);
54
55// The same as SplitString, but use a substring delimiter instead of a char.
56void SplitStringUsingSubstr(const string16& str,
57                            const string16& s,
58                            std::vector<string16>* r);
59void SplitStringUsingSubstr(const std::string& str,
60                            const std::string& s,
61                            std::vector<std::string>* r);
62
63// The same as SplitString, but don't trim white space.
64// Where wchar_t is char16 (i.e. Windows), |c| must be in BMP
65// (Basic Multilingual Plane). Elsewhere (Linux/Mac), wchar_t
66// should be a valid Unicode code point (32-bit).
67void SplitStringDontTrim(const std::wstring& str,
68                         wchar_t c,
69                         std::vector<std::wstring>* r);
70// NOTE: |c| must be in BMP (Basic Multilingual Plane)
71void SplitStringDontTrim(const string16& str,
72                         char16 c,
73                         std::vector<string16>* r);
74// |str| should not be in a multi-byte encoding like Shift-JIS or GBK in which
75// the trailing byte of a multi-byte character can be in the ASCII range.
76// UTF-8, and other single/multi-byte ASCII-compatible encodings are OK.
77// Note: |c| must be in the ASCII range.
78void SplitStringDontTrim(const std::string& str,
79                         char c,
80                         std::vector<std::string>* r);
81
82}  // namespace base
83
84#endif  // BASE_STRING_SPLIT_H
85