tlx
split.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * tlx/string/split.cpp
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 #include <tlx/string/split.hpp>
12 
13 #include <cstring>
14 
15 namespace tlx {
16 
17 /******************************************************************************/
18 // split() returning std::vector<std::string>
19 
20 std::vector<std::string> split(
21  char sep, const std::string& str, std::string::size_type limit) {
22  // call base method with new std::vector
23  std::vector<std::string> out;
24  split(&out, sep, str, limit);
25  return out;
26 }
27 
28 std::vector<std::string> split(
29  const char* sep, const std::string& str, std::string::size_type limit) {
30  // call base method with new std::vector
31  std::vector<std::string> out;
32  split(&out, sep, str, limit);
33  return out;
34 }
35 
36 std::vector<std::string> split(const std::string& sep, const std::string& str,
37  std::string::size_type limit) {
38  // call base method with new std::vector
39  std::vector<std::string> out;
40  split(&out, sep, str, limit);
41  return out;
42 }
43 
44 /******************************************************************************/
45 // split() returning std::vector<std::string> with minimum fields
46 
47 std::vector<std::string> split(
48  char sep, const std::string& str,
49  std::string::size_type min_fields, std::string::size_type limit) {
50  // call base method with new std::vector
51  std::vector<std::string> out;
52  split(&out, sep, str, min_fields, limit);
53  return out;
54 }
55 
56 std::vector<std::string> split(
57  const char* sep, const std::string& str,
58  std::string::size_type min_fields, std::string::size_type limit) {
59  // call base method with new std::vector
60  std::vector<std::string> out;
61  split(&out, sep, str, min_fields, limit);
62  return out;
63 }
64 
65 std::vector<std::string> split(
66  const std::string& sep, const std::string& str,
67  std::string::size_type min_fields, std::string::size_type limit) {
68  // call base method with new std::vector
69  std::vector<std::string> out;
70  split(&out, sep, str, min_fields, limit);
71  return out;
72 }
73 
74 /******************************************************************************/
75 // split() into std::vector<std::string>
76 
77 std::vector<std::string>& split(
78  std::vector<std::string>* into,
79  char sep, const std::string& str, std::string::size_type limit) {
80 
81  into->clear();
82  if (limit == 0) return *into;
83 
84  std::string::const_iterator it = str.begin(), last = it;
85 
86  for ( ; it != str.end(); ++it)
87  {
88  if (*it == sep)
89  {
90  if (into->size() + 1 >= limit)
91  {
92  into->emplace_back(last, str.end());
93  return *into;
94  }
95 
96  into->emplace_back(last, it);
97  last = it + 1;
98  }
99  }
100 
101  into->emplace_back(last, it);
102 
103  return *into;
104 }
105 
106 static inline
107 std::vector<std::string>& split(
108  std::vector<std::string>* into,
109  const char* sep, size_t sep_size, const std::string& str,
110  std::string::size_type limit) {
111 
112  into->clear();
113  if (limit == 0) return *into;
114 
115  if (sep_size == 0)
116  {
117  std::string::const_iterator it = str.begin();
118  while (it != str.end()) {
119  into->emplace_back(it, it + 1);
120  ++it;
121  }
122  return *into;
123  }
124 
125  std::string::const_iterator it = str.begin(), last = it;
126 
127  for ( ; it + sep_size < str.end(); ++it)
128  {
129  if (std::equal(sep, sep + sep_size, it))
130  {
131  if (into->size() + 1 >= limit)
132  {
133  into->emplace_back(last, str.end());
134  return *into;
135  }
136 
137  into->emplace_back(last, it);
138  last = it + sep_size;
139  }
140  }
141 
142  into->emplace_back(last, str.end());
143 
144  return *into;
145 }
146 
147 std::vector<std::string>& split(
148  std::vector<std::string>* into,
149  const char* sep, const std::string& str,
150  std::string::size_type limit) {
151  // call base method
152  return split(into, sep, strlen(sep), str, limit);
153 }
154 
155 std::vector<std::string>& split(
156  std::vector<std::string>* into,
157  const std::string& sep, const std::string& str,
158  std::string::size_type limit) {
159  // call base method
160  return split(into, sep.data(), sep.size(), str, limit);
161 }
162 
163 /******************************************************************************/
164 // split() into std::vector<std::string> with minimum fields
165 
166 std::vector<std::string>& split(
167  std::vector<std::string>* into,
168  char sep, const std::string& str,
169  std::string::size_type min_fields, std::string::size_type limit) {
170  // call base method
171  split(into, sep, str, limit);
172 
173  if (into->size() < min_fields)
174  into->resize(min_fields);
175 
176  return *into;
177 }
178 
179 std::vector<std::string>& split(
180  std::vector<std::string>* into,
181  const char* sep, const std::string& str,
182  std::string::size_type min_fields, std::string::size_type limit) {
183  // call base method
184  split(into, sep, str, limit);
185 
186  if (into->size() < min_fields)
187  into->resize(min_fields);
188 
189  return *into;
190 }
191 
192 std::vector<std::string>& split(
193  std::vector<std::string>* into,
194  const std::string& sep, const std::string& str,
195  std::string::size_type min_fields, std::string::size_type limit) {
196  // call base method
197  split(into, sep, str, limit);
198 
199  if (into->size() < min_fields)
200  into->resize(min_fields);
201 
202  return *into;
203 }
204 
205 } // namespace tlx
206 
207 /******************************************************************************/
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