tlx
split.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * tlx/string/split.hpp
3  *
4  * Part of tlx - http://panthema.net/tlx
5  *
6  * Copyright (C) 2007-2017 Timo Bingmann <tb@panthema.net>
7  *
8  * All rights reserved. Published under the Boost Software License, Version 1.0
9  ******************************************************************************/
10 
11 #ifndef TLX_STRING_SPLIT_HEADER
12 #define TLX_STRING_SPLIT_HEADER
13 
14 #include <string>
15 #include <vector>
16 
17 namespace tlx {
18 
19 //! \addtogroup tlx_string
20 //! \{
21 //! \name Split and Join
22 //! \{
23 
24 /******************************************************************************/
25 // split() returning std::vector<std::string>
26 
27 /*!
28  * Split the given string at each separator character into distinct substrings.
29  * Multiple consecutive separators are considered individually and will result
30  * in empty split substrings.
31  *
32  * \param sep separator character
33  * \param str string to split
34  * \param limit maximum number of parts returned
35  * \return vector containing each split substring
36  */
37 std::vector<std::string> split(
38  char sep, const std::string& str,
39  std::string::size_type limit = std::string::npos);
40 
41 /*!
42  * Split the given string at each separator string into distinct substrings.
43  * Multiple consecutive separators are considered individually and will result
44  * in empty split substrings.
45  *
46  * \param sep separator string
47  * \param str string to split
48  * \param limit maximum number of parts returned
49  * \return vector containing each split substring
50  */
51 std::vector<std::string> split(
52  const char* sep, const std::string& str,
53  std::string::size_type limit = std::string::npos);
54 
55 /*!
56  * Split the given string at each separator string into distinct substrings.
57  * Multiple consecutive separators are considered individually and will result
58  * in empty split substrings.
59  *
60  * \param sep separator string
61  * \param str string to split
62  * \param limit maximum number of parts returned
63  * \return vector containing each split substring
64  */
65 std::vector<std::string> split(
66  const std::string& sep, const std::string& str,
67  std::string::size_type limit = std::string::npos);
68 
69 /******************************************************************************/
70 // split() returning std::vector<std::string> with minimum fields
71 
72 /*!
73  * Split the given string at each separator character into distinct substrings.
74  * Multiple consecutive separators are considered individually and will result
75  * in empty split substrings. Returns a vector of strings with at least
76  * min_fields and at most limit_fields, empty fields are added if needed.
77  *
78  * \param sep separator string
79  * \param str string to split
80  * \param min_fields minimum number of parts returned
81  * \param limit maximum number of parts returned
82  * \return vector containing each split substring
83  */
84 std::vector<std::string> split(
85  char sep, const std::string& str,
86  std::string::size_type min_fields, std::string::size_type limit);
87 
88 /*!
89  * Split the given string at each separator string into distinct substrings.
90  * Multiple consecutive separators are considered individually and will result
91  * in empty split substrings. Returns a vector of strings with at least
92  * min_fields and at most limit_fields, empty fields are added if needed.
93  *
94  * \param sep separator string
95  * \param str string to split
96  * \param min_fields minimum number of parts returned
97  * \param limit maximum number of parts returned
98  * \return vector containing each split substring
99  */
100 std::vector<std::string> split(
101  const char* sep, const std::string& str,
102  std::string::size_type min_fields, std::string::size_type limit);
103 
104 /*!
105  * Split the given string at each separator string into distinct substrings.
106  * Multiple consecutive separators are considered individually and will result
107  * in empty split substrings. Returns a vector of strings with at least
108  * min_fields and at most limit_fields, empty fields are added if needed.
109  *
110  * \param sep separator string
111  * \param str string to split
112  * \param min_fields minimum number of parts returned
113  * \param limit maximum number of parts returned
114  * \return vector containing each split substring
115  */
116 std::vector<std::string> split(
117  const std::string& sep, const std::string& str,
118  std::string::size_type min_fields, std::string::size_type limit);
119 
120 /******************************************************************************/
121 // split() into std::vector<std::string>
122 
123 /*!
124  * Split the given string at each separator character into distinct substrings.
125  * Multiple consecutive separators are considered individually and will result
126  * in empty split substrings.
127  *
128  * \param into destination std::vector
129  * \param sep separator character
130  * \param str string to split
131  * \param limit maximum number of parts returned
132  * \return vector containing each split substring
133  */
134 std::vector<std::string>& split(
135  std::vector<std::string>* into,
136  char sep, const std::string& str,
137  std::string::size_type limit = std::string::npos);
138 
139 /*!
140  * Split the given string at each separator string into distinct substrings.
141  * Multiple consecutive separators are considered individually and will result
142  * in empty split substrings.
143  *
144  * \param into destination std::vector
145  * \param sep separator string
146  * \param str string to split
147  * \param limit maximum number of parts returned
148  * \return vector containing each split substring
149  */
150 std::vector<std::string>& split(
151  std::vector<std::string>* into,
152  const char* sep, const std::string& str,
153  std::string::size_type limit = std::string::npos);
154 
155 /*!
156  * Split the given string at each separator string into distinct substrings.
157  * Multiple consecutive separators are considered individually and will result
158  * in empty split substrings.
159  *
160  * \param into destination std::vector
161  * \param sep separator string
162  * \param str string to split
163  * \param limit maximum number of parts returned
164  * \return vector containing each split substring
165  */
166 std::vector<std::string>& split(
167  std::vector<std::string>* into,
168  const std::string& sep, const std::string& str,
169  std::string::size_type limit = std::string::npos);
170 
171 /******************************************************************************/
172 // split() into std::vector<std::string> with minimum fields
173 
174 /*!
175  * Split the given string at each separator character into distinct substrings.
176  * Multiple consecutive separators are considered individually and will result
177  * in empty split substrings. Returns a vector of strings with at least
178  * min_fields and at most limit_fields, empty fields are added if needed.
179  *
180  * \param into destination std::vector
181  * \param sep separator character
182  * \param str string to split
183  * \param min_fields minimum number of parts returned
184  * \param limit maximum number of parts returned
185  * \return vector containing each split substring
186  */
187 std::vector<std::string>& split(
188  std::vector<std::string>* into,
189  char sep, const std::string& str,
190  std::string::size_type min_fields, std::string::size_type limit);
191 
192 /*!
193  * Split the given string at each separator string into distinct substrings.
194  * Multiple consecutive separators are considered individually and will result
195  * in empty split substrings. Returns a vector of strings with at least
196  * min_fields and at most limit_fields, empty fields are added if needed.
197  *
198  * \param into destination std::vector
199  * \param sep separator string
200  * \param str string to split
201  * \param min_fields minimum number of parts returned
202  * \param limit maximum number of parts returned
203  * \return vector containing each split substring
204  */
205 std::vector<std::string>& split(
206  std::vector<std::string>* into,
207  const char* sep, const std::string& str,
208  std::string::size_type min_fields, std::string::size_type limit);
209 
210 /*!
211  * Split the given string at each separator string into distinct substrings.
212  * Multiple consecutive separators are considered individually and will result
213  * in empty split substrings. Returns a vector of strings with at least
214  * min_fields and at most limit_fields, empty fields are added if needed.
215  *
216  * \param into destination std::vector
217  * \param sep separator string
218  * \param str string to split
219  * \param min_fields minimum number of parts returned
220  * \param limit maximum number of parts returned
221  * \return vector containing each split substring
222  */
223 std::vector<std::string>& split(
224  std::vector<std::string>* into,
225  const std::string& sep, const std::string& str,
226  std::string::size_type min_fields, std::string::size_type limit);
227 
228 //! \}
229 //! \}
230 
231 } // namespace tlx
232 
233 #endif // !TLX_STRING_SPLIT_HEADER
234 
235 /******************************************************************************/
std::vector< std::string > split(char sep, const std::string &str, std::string::size_type limit)
Split the given string at each separator character into distinct substrings.
Definition: split.cpp:20