-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstringfunc.cpp
168 lines (145 loc) · 3.77 KB
/
stringfunc.cpp
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
#include "stringfunc.h"
std::vector<std::string> break_into_lines(std::string text, int linesize)
{
std::vector<std::string> ret;
size_t chars = 0; // Number of actually-printed characters at...
size_t pos = 0; // ... this point in the string
size_t linebreak = std::string::npos; // The last acceptable breakpoint
std::string active_color_tag;
while (text.length() > linesize && pos < text.size()) {
bool force = false;
if (text.substr(pos, 3) == "<c=") {
size_t tmppos = text.find('>', pos);
if (tmppos == std::string::npos) {
//debugmsg("Bad colortag!");
return ret;
} else {
active_color_tag = text.substr(pos, tmppos - pos + 1);
pos = tmppos;
}
linebreak = pos;
chars--;
} else if (text[pos] == '\n') {
linebreak = pos;
force = true;
} else if (text[pos] == ' ')
linebreak = pos;
pos++;
chars++;
if (force || chars > linesize) {
std::string tmp;
if (linebreak == std::string::npos) {
linebreak = linesize - 1;
tmp = text.substr(0, linebreak) + "-";
text = text.substr(linebreak);
} else if (text[linebreak] == '\n' || text[linebreak] == ' ') {
tmp = text.substr(0, linebreak);
text = text.substr(linebreak + 1);
} else if (text[linebreak] == '>') {
linebreak++;
tmp = text.substr(0, linebreak);
text = text.substr(linebreak);
}
ret.push_back(tmp);
if (!active_color_tag.empty()) {
text = active_color_tag + text;
}
pos = 0;
chars = 0;
linebreak = std::string::npos;
}
}
ret.push_back(text);
return ret;
/*
size_t linebreak = text.find_last_of(" ", linesize);
std::string tmp;
if (linebreak == std::string::npos) {
linebreak = linesize - 1;
tmp = text.substr(0, linebreak) + "-";
} else
tmp = text.substr(0, linebreak);
ret.push_back(tmp);
text = text.substr(linebreak + 1);
}
ret.push_back(text);
*/
return ret;
}
std::string load_to_delim(std::istream &datastream, std::string delim)
{
std::string ret, tmp;
do {
datastream >> tmp;
if (tmp != delim)
ret += tmp + " ";
} while (tmp != delim && !(datastream.eof()));
if (!ret.empty() && ret[ret.size() - 1] == ' ')
ret = ret.substr(0, ret.size() - 1);
return ret;
}
std::string load_to_character(std::istream &datastream, char ch, bool _trim)
{
std::string ret;
char *tmp = new char [1];
do {
datastream.read(tmp, 1);
if (tmp[0] != ch) {
ret.push_back(tmp[0]);
}
} while (tmp[0] != ch && !(datastream.eof()));
if (_trim)
ret = trim(ret);
delete [] tmp;
return ret;
}
std::string load_to_character(std::istream &datastream, std::string chars,
bool _trim)
{
std::string ret;
char *tmp = new char [1];
do {
datastream.read(tmp, 1);
if (chars.find(tmp[0]) == std::string::npos) {
ret.push_back(tmp[0]);
}
} while (chars.find(tmp[0]) == std::string::npos && chars[0] >= 0 &&
!(datastream.eof()));
if (_trim)
ret = trim(ret);
delete [] tmp;
return ret;
}
std::string trim(const std::string &orig)
{
std::string ret = orig;
int front = 0, back = ret.length() - 1;
while (front < ret.length() &&
(ret[front] == ' ' || ret[front] == '\n' || ret[front] == '\t'))
front++;
ret = ret.substr(front);
back = ret.length() - 1;
while (back >= 0 &&
(ret[back] == ' ' || ret[back] == '\n' || ret[back] == '\t'))
back--;
ret = ret.substr(0, back + 1);
return ret;
}
std::string all_caps(const std::string &orig)
{
std::string ret = orig;
for (int i = 0; i < ret.length(); i++) {
if (ret[i] >= 'a' && ret[i] <= 'z')
ret[i] += 'A' - 'a';
}
return ret;
}
std::string no_caps(const std::string &orig)
{
std::string ret = orig;
for (int i = 0; i < ret.length(); i++) {
if (ret[i] >= 'A' && ret[i] <= 'Z')
ret[i] += 'a' - 'A';
}
return ret;
}