-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStringUtils.h
340 lines (322 loc) · 11.6 KB
/
StringUtils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/**
* \file StringUtils.h
*
* \brief Various string-related utilities.
*
* This is originally from Nate Barney circa Moore Lab days 2003-2007.
* His function naming follows lowercase with underscores style,
* while my additions are camelCase.
*
* \author Bill White, Nate Barney
* \version 1.0
*
* Contact: [email protected]
* Created on: 10/7/04
*/
#ifndef STRINGUTILS_H
#define STRINGUTILS_H
#include <string>
#include <cctype>
#include <vector>
#include <clocale>
#include <functional>
#include <algorithm>
#include <iterator>
#include <climits>
#include <iomanip>
#include <iostream>
#include <sstream>
namespace insilico
{
// Predicate class to test if a character is of a given
// class. For example: is_classified<std::ctype_base::space>()
// tests to see if a character is whitespace for the given
// locale. There are similar types for each of the is* functions
// in <cctype>.
template <std::ctype_base::mask Type, class charT = char>
class is_classified : public std::unary_function<charT, bool>
{
public:
// ctor from a ctype
is_classified(std::ctype<charT> &ct) : m_ctype(ct) {
}
// ctor from a locale (for convenience)
is_classified(const std::locale &loc = std::locale())
: m_ctype(std::use_facet<std::ctype<charT> >(loc)) {
}
bool operator()(charT c) const {
return m_ctype.is(Type, c);
}
private:
std::ctype<charT> const & m_ctype;
};
// Unary functor to call toupper under the specified locale
template <class charT = char>
class do_to_upper : public std::unary_function<charT, charT>
{
public:
// ctor from a ctype
do_to_upper(std::ctype<charT> &ct) : m_ctype(ct) {
}
// ctor from a locale (for convenience)
do_to_upper(const std::locale &loc = std::locale())
: m_ctype(std::use_facet<std::ctype<charT> >(loc)) {
}
charT operator()(charT c) const {
return m_ctype.toupper(c);
}
private:
std::ctype<charT> const & m_ctype;
};
// Unary functor to call tolower under the specified locale
template <class charT = char>
class do_to_lower : public std::unary_function<charT, charT>
{
public:
// ctor from a ctype
do_to_lower(std::ctype<charT> &ct) : m_ctype(ct) {
}
// ctor from a locale (for convenience)
do_to_lower(const std::locale &loc = std::locale())
: m_ctype(std::use_facet<std::ctype<charT> >(loc)) {
}
charT operator()(charT c) const {
return m_ctype.tolower(c);
}
private:
std::ctype<charT> const & m_ctype;
};
// For all string functions, the stringT type must be a template instance
// of std::basic_string.
// remove leading whitespace
template <typename stringT>
stringT trim_left(const stringT &s, const std::locale &loc = std::locale()) {
// find first non-whitespace character
typename stringT::const_iterator it = std::find_if(s.begin(), s.end(),
std::not1(is_classified < std::ctype_base::space,
typename stringT::value_type > (loc)));
// return appropriate substring
return stringT(it, s.end());
}
// remove trailing whitespace
template <typename stringT>
stringT trim_right(const stringT &s, const std::locale &loc = std::locale()) {
// find last non-whitespace character
typename stringT::const_reverse_iterator it = std::find_if(s.rbegin(),
s.rend(), std::not1(is_classified < std::ctype_base::space,
typename stringT::value_type > (loc)));
// return appropriate substring
return stringT(s.begin(), it.base());
}
// remove leading and trailing whitespace
template <typename stringT>
stringT trim(const stringT &s,
const std::locale &loc = std::locale()) {
typename stringT::const_iterator b;
typename stringT::const_reverse_iterator e;
// find first non-whitespace character
b = std::find_if(s.begin(), s.end(),
std::not1(is_classified < std::ctype_base::space,
typename stringT::value_type > (loc)));
// if none, return empty string
if(b == s.end())
return stringT();
// find last non-whitespace character
e = std::find_if(s.rbegin(), s.rend(),
std::not1(is_classified < std::ctype_base::space,
typename stringT::value_type > (loc)));
// return appropriate substribg
return stringT(b, e.base());
}
// For all split functions, the Container type must contain stringT's
// and it must have a push_back(const stringT &) member to add items
// to the end of the sequence.
// perl-like split on whitespace
template <typename Container, typename stringT>
inline void split(Container &cont, const stringT &s,
const std::locale &loc = std::locale()) {
// split on any whitespace character
split_if(cont, s, is_classified < std::ctype_base::space,
typename stringT::value_type > (loc));
}
// perl-like split with delimiter
template <typename Container, typename stringT>
void split(Container &cont, const stringT &s, const stringT &delim) {
typename stringT::size_type i, j;
// loop through positions where delimiter is found
for(i = 0; i < s.size(); i = j + delim.size()) {
// get next delimiter pos
j = s.find(delim, i);
// if not found, return remainder of string
if(j == std::string::npos) {
cont.push_back(s.substr(i));
break;
}
// store current field, if nonempty
if(j != i)
cont.push_back(s.substr(i, j - i));
}
}
// perl-like split with predicate
// (pred must be a unary predicate that takes a stringT::value_type
// as input and returns a boolean. It is used to determine whether
// a given character is a delimiter.)
template <typename Container, typename stringT, typename Pred>
void split_if(Container &cont, const stringT &s, const Pred &pred) {
typename stringT::const_iterator i, j;
// loop through positions where delimiter is found
for(i = s.begin(); i != s.end(); i = j + 1) {
// get next delimiter pos
j = std::find_if(i, s.end(), pred);
// if not found, return remainder of string
if(j == s.end()) {
cont.push_back(s.substr(i - s.begin()));
break;
}
// store current field, if nonempty
if(j != i)
cont.push_back(s.substr(i - s.begin(), j - i));
}
}
// perl-like join
// It type must be an iterator that points to stringT's.
template <typename It, typename stringT>
stringT join(const It &begin, const It &end, const stringT &delim) {
stringT ret;
// loop through input fields
for(It i = begin; i != end; ++i) {
// add delimiter
if(i != begin)
ret += delim;
// add field
ret += *i;
}
// return result
return ret;
}
// return uppercased copy of string
template <typename stringT>
stringT to_upper(const stringT &str,
const std::locale &loc = std::locale()) {
stringT s = str;
std::transform(s.begin(), s.end(), s.begin(),
do_to_upper<typename stringT::value_type > (loc));
return s;
}
// return lowercased copy of string
template <typename stringT>
stringT to_lower(const stringT &str,
const std::locale &loc = std::locale()) {
stringT s = str;
std::transform(s.begin(), s.end(), s.begin(),
do_to_lower<typename stringT::value_type > (loc));
return s;
}
// char and wchar_t versions
inline std::string trim_left(const char *s,
const std::locale &loc = std::locale()) {
return trim_left(std::string(s), loc);
}
inline std::wstring trim_left(const wchar_t *s,
const std::locale &loc = std::locale()) {
return trim_left(std::wstring(s), loc);
}
inline std::string trim_right(const char *s,
const std::locale &loc = std::locale()) {
return trim_right(std::string(s), loc);
}
inline std::wstring trim_right(const wchar_t *s,
const std::locale &loc = std::locale()) {
return trim_right(std::wstring(s), loc);
}
inline std::string trim(const char *s,
const std::locale &loc = std::locale()) {
return trim(std::string(s), loc);
}
inline std::wstring trim(const wchar_t *s,
const std::locale &loc = std::locale()) {
return trim(std::wstring(s), loc);
}
template <typename Container>
inline void split(Container &cont, const char *s,
const std::locale &loc = std::locale()) {
split(cont, std::string(s), loc);
}
template <typename Container>
inline void split(Container &cont, const wchar_t *s,
const std::locale &loc = std::locale()) {
split(cont, std::wstring(s), loc);
}
template <typename Container>
inline void split(Container &cont, const std::string &s, const char *delim) {
split(cont, s, std::string(delim));
}
template <typename Container>
inline void split(Container &cont, const char *s, const std::string &delim) {
split(cont, std::string(s), delim);
}
template <typename Container>
inline void split(Container &cont, const char *s, const char *delim) {
split(cont, std::string(s), std::string(delim));
}
template <typename Container>
inline void split(Container &cont, const std::wstring &s, const wchar_t *delim) {
split(cont, s, std::wstring(delim));
}
template <typename Container>
inline void split(Container &cont, const wchar_t *s, const std::wstring &delim) {
split(cont, std::wstring(s), delim);
}
template <typename Container>
inline void split(Container &cont, const wchar_t *s, const wchar_t *delim) {
split(cont, std::wstring(s), std::wstring(delim));
}
template <typename Container, typename Pred>
inline void split_if(Container &cont, const char *s, const Pred &pred) {
split_if(cont, std::string(s), pred);
}
template <typename Container, typename Pred>
inline void split_if(Container &cont, const wchar_t *s, const Pred &pred) {
split_if(cont, std::wstring(s), pred);
}
template <typename It>
inline std::string join(const It &begin, const It &end, const char *delim) {
return join(begin, end, std::string(delim));
}
template <typename It>
inline std::wstring join(const It &begin, const It &end, const wchar_t *delim) {
return join(begin, end, std::wstring(delim));
}
inline std::string to_upper(const char *s,
const std::locale &loc = std::locale()) {
return to_upper(std::string(s), loc);
}
inline std::wstring to_upper(const wchar_t *s,
const std::locale &loc = std::locale()) {
return to_upper(std::wstring(s), loc);
}
inline std::string to_lower(const char *s,
const std::locale &loc = std::locale()) {
return to_lower(std::string(s), loc);
}
inline std::wstring to_lower(const wchar_t *s,
const std::locale &loc = std::locale()) {
return to_lower(std::wstring(s), loc);
}
template<typename T>
std::string get_bits(T value) {
int size = sizeof(value) * CHAR_BIT;
std::string ret;
ret.reserve(size);
for(int i = size - 1; i >= 0; --i)
ret += (value & (1 << i)) == 0 ? '0' : '1';
return ret;
}
template<typename T>
std::string zeroPadNumber(T num, int padSize) {
std::ostringstream ss;
ss << std::setw(padSize) << std::setfill('0') << num;
return ss.str();
}
} // namespace insilico
#endif // STRINGUTIL_H