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 <sstream>
28 #include <vector>
29 #include <string>
30 #include <queue>
31 
32 namespace fawkes {
33 
34 /** Split string by delimiter.
35  * @param s string to split
36  * @param delim delimiter
37  * @return vector of split strings
38  */
39 static inline
40 std::vector<std::string> str_split(const std::string &s, char delim = '/')
41 {
42  std::vector<std::string> elems;
43  std::stringstream ss(s);
44  std::string item;
45  while(std::getline(ss, item, delim)) {
46  if (item != "") elems.push_back(item);
47  }
48  return elems;
49 }
50 
51 
52 /** Split string by delimiter string.
53  * @param s string to split
54  * @param delim delimiter
55  * @return vector of split strings
56  */
57 static inline
58 std::vector<std::string> str_split(const std::string &s, std::string delim)
59 {
60  std::vector<std::string> elems;
61  std::string::size_type pos = 0;
62  do {
63  std::string::size_type dpos = s.find(delim, pos);
64  std::string sub = s.substr(pos, dpos);
65  elems.push_back(sub);
66  if (dpos != std::string::npos) pos = dpos + delim.length();
67  else pos = dpos;
68  } while (pos != std::string::npos);
69  return elems;
70 }
71 
72 /** Split string by delimiter.
73  * @param s string to split
74  * @param delim delimiter
75  * @return queue of split strings
76  */
77 static inline
78 std::list<std::string> str_split_list(const std::string &s, char delim = '/')
79 {
80  std::list<std::string> elems;
81  std::stringstream ss(s);
82  std::string item;
83  while(std::getline(ss, item, delim)) {
84  if (item != "") elems.push_back(item);
85  }
86  return elems;
87 }
88 
89 
90 /** Join vector of strings string using given delimiter.
91  * @param v vector with strings to join
92  * @param delim delimiter
93  * @return string of strings in vector separated by given delimiter
94  */
95 static inline
96 std::string str_join(const std::vector<std::string> &v, char delim = '/')
97 {
98  std::string rv;
99  for (size_t i = 0; i < v.size(); ++i) {
100  if (i > 0) rv += delim;
101  rv += v[i];
102  }
103  return rv;
104 }
105 
106 /** Join list of strings string using given delimiter.
107  * @param l list with strings to join
108  * @param delim delimiter
109  * @return string of strings in list separated by given delimiter
110  */
111 static inline
112 std::string str_join(const std::list<std::string> &l, char delim = '/')
113 {
114  std::string rv;
115  bool first = true;
116  for (std::list<std::string>::const_iterator i = l.begin(); i != l.end(); ++i) {
117  if (first) first = false;
118  else rv += delim;
119  rv += *i;
120  }
121  return rv;
122 }
123 
124 
125 /** Join list of strings string using given delimiter.
126  * The iterator must be produce a std::string for operator*().
127  * @param first input iterator to beginning of range
128  * @param last input iterator to end of range
129  * @param delim delimiter
130  * @return string of strings in list separated by given delimiter
131  */
132 template <typename InputIterator>
133 std::string
134 str_join(const InputIterator &first, const InputIterator &last, char delim = '/')
135 {
136  std::string rv;
137  bool is_first = true;
138  for (InputIterator i = first; i != last; ++i) {
139  if (is_first) is_first = false;
140  else rv += delim;
141  rv += *i;
142  }
143  return rv;
144 }
145 
146 
147 /** Join list of strings string using given delimiter.
148  * @param l list with strings to join
149  * @param delim delimiter
150  * @return string of strings in list separated by given delimiter
151  */
152 static inline
153  std::string str_join(const std::list<std::string> &l, std::string delim)
154 {
155  std::string rv;
156  bool first = true;
157  for (std::list<std::string>::const_iterator i = l.begin(); i != l.end(); ++i) {
158  if (first) first = false;
159  else rv += delim;
160  rv += *i;
161  }
162  return rv;
163 }
164 
165 /** Join list of strings string using given delimiter.
166  * The iterator must be produce a std::string for operator*().
167  * @param first input iterator to beginning of range
168  * @param last input iterator to end of range
169  * @param delim delimiter
170  * @return string of strings in list separated by given delimiter
171  */
172 template <typename InputIterator>
173 std::string
174 str_join(const InputIterator &first, const InputIterator &last, std::string delim)
175 {
176  std::string rv;
177  bool is_first = true;
178  for (InputIterator i = first; i != last; ++i) {
179  if (is_first) is_first = false;
180  else rv += delim;
181  rv += *i;
182  }
183  return rv;
184 }
185 
186 
187 /** Split string by delimiter.
188  * @param s string to split
189  * @param delim delimiter
190  * @return queue of split strings
191  */
192 static inline
193 std::queue<std::string> str_split_to_queue(const std::string &s, char delim = '/')
194 {
195  std::queue<std::string> elems;
196  std::stringstream ss(s);
197  std::string item;
198  while(std::getline(ss, item, delim)) {
199  if (item != "") elems.push(item);
200  }
201  return elems;
202 }
203 
204 } // end namespace fawkes
205 
206 #endif
Fawkes library namespace.
static std::list< std::string > str_split_list(const std::string &s, char delim='/')
Split string by delimiter.
Definition: string_split.h:78
static std::queue< std::string > str_split_to_queue(const std::string &s, char delim='/')
Split string by delimiter.
Definition: string_split.h:193
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:96
static std::vector< std::string > str_split(const std::string &s, char delim='/')
Split string by delimiter.
Definition: string_split.h:40