-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.h
80 lines (63 loc) · 1.86 KB
/
tools.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
/*
* File: tools.h
* Author: lanz
*
* Created on den 14 januari 2013, 22:33
*/
#ifndef TOOLS_H
#define TOOLS_H
#include <string>
#include <iostream>
#include <algorithm>
#include <functional>
#include <cctype>
#include <locale>
#include <vector>
// trim from start
static inline std::string <rim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
return s;
}
// trim from end
static inline std::string &rtrim(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
return s;
}
// trim from both ends
static inline std::string &trim(std::string &s) {
return ltrim(rtrim(s));
}
int find_crlf(const char *buf, int length);
std::string filter_nick(std::string host_mask);
std::string filter_user(std::string host_mask);
std::string filter_host(std::string host_mask);
class Splitter {
std::vector<std::string> _tokens;
public:
typedef std::vector<std::string>::size_type size_type;
Splitter(const std::string& src, const std::string& delim) {
reset(src, delim);
}
std::string& operator[] (size_type i) {
return _tokens.at(i);
}
size_type size() const {
return _tokens.size();
}
void reset(const std::string& src, const std::string& delim) {
std::vector<std::string> tokens;
std::string::size_type start = 0;
std::string::size_type end;
for (;;) {
end = src.find(delim, start);
tokens.push_back(src.substr(start, end - start));
// We just copied the last token
if (end == std::string::npos)
break;
// Exclude the delimiter in the next search
start = end + delim.size();
}
_tokens.swap(tokens);
}
};
#endif /* TOOLS_H */