-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrmisc.cpp
More file actions
148 lines (119 loc) · 3.3 KB
/
strmisc.cpp
File metadata and controls
148 lines (119 loc) · 3.3 KB
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
#include "strmisc.h"
#include <algorithm>
#include <ctype.h>
#include <iostream>
std::string& toLower(std::string& s)
{
std::transform(s.begin(), s.end(), s.begin(), (int(*)(int))tolower);
return s;
}
std::string& toUpper(std::string& s)
{
std::transform(s.begin(), s.end(), s.begin(), (int(*)(int))toupper);
return s;
}
bool startsWith(const std::string& data, const std::string& start )
{
if (start.size() > data.size()) { return false; }
return std::equal(start.begin(), start.end(),data.begin());
}
bool endsWith(const std::string& u, const std::string& end )
{
if (end.size() > u.size()) { return false; }
return std::equal(end.rbegin(), end.rend(),u.rbegin());
}
std::string strip(const std::string _s)
{
std::string::size_type start;
std::string::size_type end;
std::string s = _s;
start = s.find_first_not_of(WHITESPACE);
if (start == std::string::npos) {
// String made of spaces...
s.clear();
return s;
}
s = s.substr(start);
// take space out of the end of the string
end = s.find_last_not_of(WHITESPACE);
if (end == std::string::npos) {
s.clear();
return s;
}
s = s.substr(0,end +1);
return s;
}
std::string unichr(unsigned int uv)
{
const unsigned int UTF_MAX = 6; /* They can be bigger, but we just don't care
* about those bastards
*/
char output[UTF_MAX +1] = {0,0,0,0,0,0,0}; /* Strings will
* be null-termiinated
* no matter what
*/
char* d = output;
if (uv < 0x80) {
*d++ = uv;
return std::string(output);
}
if (uv < 0x800) {
*d++ = (( uv >> 6) | 0xc0);
*d++ = (( uv & 0x3f) | 0x80);
return std::string(output);
}
if (uv < 0x10000) {
*d++ = (( uv >> 12) | 0xe0);
*d++ = (((uv >> 6) & 0x3f) | 0x80);
*d++ = (( uv & 0x3f) | 0x80);
return std::string(output);
}
if (uv < 0x200000) {
*d++ = (( uv >> 18) | 0xf0);
*d++ = (((uv >> 12) & 0x3f) | 0x80);
*d++ = (((uv >> 6) & 0x3f) | 0x80);
*d++ = (( uv & 0x3f) | 0x80);
return std::string(output);
}
if (uv < 0x4000000) {
*d++ = (( uv >> 24) | 0xf8);
*d++ = (((uv >> 18) & 0x3f) | 0x80);
*d++ = (((uv >> 12) & 0x3f) | 0x80);
*d++ = (((uv >> 6) & 0x3f) | 0x80);
*d++ = (( uv & 0x3f) | 0x80);
return std::string(output);
}
if (uv < 0x80000000) {
*d++ = (( uv >> 30) | 0xfc);
*d++ = (((uv >> 24) & 0x3f) | 0x80);
*d++ = (((uv >> 18) & 0x3f) | 0x80);
*d++ = (((uv >> 12) & 0x3f) | 0x80);
*d++ = (((uv >> 6) & 0x3f) | 0x80);
*d++ = (( uv & 0x3f) | 0x80);
return std::string(output);
}
// Corner case - returning codepoint 0x7fffffff representation
// in UTF-8
return std::string("\xfd\xbf\xbf\xbf\xbf\xbf");
}
std::vector<std::string>
split (const std::string &inString, const std::string &separator,
int maxsplit)
{
std::vector<std::string> returnVector;
if (inString.empty()) {
//std::cout << "string to split is empty!\n";
return returnVector;
}
std::string::size_type start = 0;
std::string::size_type end = 0;
int count = maxsplit;
while ( ((end=inString.find (separator, start)) != std::string::npos) &&
(maxsplit ? count-- : true) )
{
returnVector.push_back (inString.substr (start, end-start));
start = end+separator.size();
}
returnVector.push_back (inString.substr (start));
return returnVector;
}