Fawkes API  Fawkes Development Version
string_split.h
1 
2 /***************************************************************************
3  * string_split.h - Split string functions
4  *
5  * Created: Wed Apr 03 18:01:30 2013
6  * Copyright 2006-2013 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #ifndef _UTILS_MISC_STRING_SPLIT_H_
25 #define _UTILS_MISC_STRING_SPLIT_H_
26 
27 #include <list>
28 #include <queue>
29 #include <sstream>
30 #include <string>
31 #include <vector>
32 
33 namespace fawkes {
34 
35 /** Split string by delimiter.
36  * @param s string to split
37  * @param delim delimiter
38  * @return vector of split strings
39  */
40 static inline std::vector<std::string>
41 str_split(const std::string &s, char delim = '/')
42 {
43  std::vector<std::string> elems;
44  std::stringstream ss(s);
45  std::string item;
46  while (std::getline(ss, item, delim)) {
47  if (item != "")
48  elems.push_back(item);
49  }
50  return elems;
51 }
52 
53 /** Split string by delimiter string.
54  * @param s string to split
55  * @param delim delimiter
56  * @return vector of split strings
57  */
58 static inline std::vector<std::string>
59 str_split(const std::string &s, std::string delim)
60 {
61  std::vector<std::string> elems;
62  std::string::size_type pos = 0;
63  do {
64  std::string::size_type dpos = s.find(delim, pos);
65  std::string sub = s.substr(pos, dpos);
66  elems.push_back(sub);
67  if (dpos != std::string::npos)
68  pos = dpos + delim.length();
69  else
70  pos = dpos;
71  } while (pos != std::string::npos);
72  return elems;
73 }
74 
75 /** Split string by delimiter.
76  * @param s string to split
77  * @param delim delimiter
78  * @return queue of split strings
79  */
80 static inline std::list<std::string>
81 str_split_list(const std::string &s, char delim = '/')
82 {
83  std::list<std::string> elems;
84  std::stringstream ss(s);
85  std::string item;
86  while (std::getline(ss, item, delim)) {
87  if (item != "")
88  elems.push_back(item);
89  }
90  return elems;
91 }
92 
93 /** Join vector of strings string using given delimiter.
94  * @param v vector with strings to join
95  * @param delim delimiter
96  * @return string of strings in vector separated by given delimiter
97  */
98 static inline std::string
99 str_join(const std::vector<std::string> &v, char delim = '/')
100 {
101  std::string rv;
102  for (size_t i = 0; i < v.size(); ++i) {
103  if (i > 0)
104  rv += delim;
105  rv += v[i];
106  }
107  return rv;
108 }
109 
110 /** Join list of strings string using given delimiter.
111  * @param l list with strings to join
112  * @param delim delimiter
113  * @return string of strings in list separated by given delimiter
114  */
115 static inline std::string
116 str_join(const std::list<std::string> &l, char delim = '/')
117 {
118  std::string rv;
119  bool first = true;
120  for (std::list<std::string>::const_iterator i = l.begin(); i != l.end(); ++i) {
121  if (first)
122  first = false;
123  else
124  rv += delim;
125  rv += *i;
126  }
127  return rv;
128 }
129 
130 /** Join list of strings string using given delimiter.
131  * The iterator must be produce a std::string for operator*().
132  * @param first input iterator to beginning of range
133  * @param last input iterator to end of range
134  * @param delim delimiter
135  * @return string of strings in list separated by given delimiter
136  */
137 template <typename InputIterator>
138 std::string
139 str_join(const InputIterator &first, const InputIterator &last, char delim = '/')
140 {
141  std::string rv;
142  bool is_first = true;
143  for (InputIterator i = first; i != last; ++i) {
144  if (is_first)
145  is_first = false;
146  else
147  rv += delim;
148  rv += *i;
149  }
150  return rv;
151 }
152 
153 /** Join list of strings string using given delimiter.
154  * @param l list with strings to join
155  * @param delim delimiter
156  * @return string of strings in list separated by given delimiter
157  */
158 static inline std::string
159 str_join(const std::list<std::string> &l, std::string delim)
160 {
161  std::string rv;
162  bool first = true;
163  for (std::list<std::string>::const_iterator i = l.begin(); i != l.end(); ++i) {
164  if (first)
165  first = false;
166  else
167  rv += delim;
168  rv += *i;
169  }
170  return rv;
171 }
172 
173 /** Join list of strings string using given delimiter.
174  * The iterator must be produce a std::string for operator*().
175  * @param first input iterator to beginning of range
176  * @param last input iterator to end of range
177  * @param delim delimiter
178  * @return string of strings in list separated by given delimiter
179  */
180 template <typename InputIterator>
181 std::string
182 str_join(const InputIterator &first, const InputIterator &last, std::string delim)
183 {
184  std::string rv;
185  bool is_first = true;
186  for (InputIterator i = first; i != last; ++i) {
187  if (is_first)
188  is_first = false;
189  else
190  rv += delim;
191  rv += *i;
192  }
193  return rv;
194 }
195 
196 /** Split string by delimiter.
197  * @param s string to split
198  * @param delim delimiter
199  * @return queue of split strings
200  */
201 static inline std::queue<std::string>
202 str_split_to_queue(const std::string &s, char delim = '/')
203 {
204  std::queue<std::string> elems;
205  std::stringstream ss(s);
206  std::string item;
207  while (std::getline(ss, item, delim)) {
208  if (item != "")
209  elems.push(item);
210  }
211  return elems;
212 }
213 
214 } // end namespace fawkes
215 
216 #endif
Fawkes library namespace.
static std::queue< std::string > str_split_to_queue(const std::string &s, char delim='/')
Split string by delimiter.
Definition: string_split.h:202
static std::list< std::string > str_split_list(const std::string &s, char delim='/')
Split string by delimiter.
Definition: string_split.h:81
static std::string str_join(const std::vector< std::string > &v, char delim='/')
Join vector of strings string using given delimiter.
Definition: string_split.h:99
static std::vector< std::string > str_split(const std::string &s, char delim='/')
Split string by delimiter.
Definition: string_split.h:41