-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstring_stuff.cpp
298 lines (246 loc) · 6.38 KB
/
string_stuff.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
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
/* Copyright (c) 2012 Cheese and Bacon Games, LLC */
/* This file is licensed under the MIT License. */
/* See the file docs/LICENSE.txt for the full license text. */
#include "string_stuff.h"
#include "world.h"
#include <vector>
#include <boost/algorithm/string.hpp>
using namespace std;
string String_Stuff::first_letter_capital(string str_input){
if(str_input.length()>0){
string first_letter="";
first_letter+=str_input[0];
boost::algorithm::to_upper(first_letter);
str_input.erase(str_input.begin());
str_input.insert(0,first_letter);
}
return str_input;
}
string String_Stuff::capitalize_all_words(string str_input){
string message="";
vector<string> words;
boost::algorithm::split(words,str_input,boost::algorithm::is_any_of(" "));
for(int i=0;i<words.size();i++){
words[i]=first_letter_capital(words[i]);
message+=words[i];
if(i<words.size()-1){
message+=" ";
}
}
return message;
}
string String_Stuff::upper_case(string str_input){
boost::algorithm::to_upper(str_input);
return str_input;
}
string String_Stuff::lower_case(string str_input){
boost::algorithm::to_lower(str_input);
return str_input;
}
string String_Stuff::underscore_to_space(string str_input){
return boost::algorithm::replace_all_copy(str_input,"_"," ");
}
string String_Stuff::add_newlines(string str_input){
string newline="\\";
newline+="n";
boost::algorithm::replace_all(str_input,"\xA",newline);
return str_input;
}
string String_Stuff::process_newlines(string str_input){
string newline="\\";
newline+="n";
boost::algorithm::replace_all(str_input,newline,"\xA");
return str_input;
}
int String_Stuff::newline_count(string str_input){
int newlines=0;
string newline="\xA";
for(int i=0;i<str_input.length();i++){
if(str_input[i]==newline[0]){
newlines++;
}
}
return newlines;
}
int String_Stuff::length_of_last_line(string str_input){
vector<string> lines;
boost::algorithm::split(lines,str_input,boost::algorithm::is_any_of("\xA"));
return lines[lines.size()-1].length();
}
int String_Stuff::longest_line(string str_input){
vector<string> lines;
boost::algorithm::split(lines,str_input,boost::algorithm::is_any_of("\xA"));
int longest_line_length=0;
for(int i=0;i<lines.size();i++){
if(lines[i].length()>longest_line_length){
longest_line_length=lines[i].length();
}
}
return longest_line_length;
}
string String_Stuff::erase_first_line(string str_input){
string newline="\xA";
for(int i=0;i<str_input.length();i++){
if(str_input[i]==newline[0]){
str_input.erase(0,1);
break;
}
else{
str_input.erase(0,1);
i--;
}
}
return str_input;
}
bool String_Stuff::is_number(string str_input){
if(str_input.length()==0 || (str_input.length()==1 && str_input[0]=='-')){
return false;
}
for(int i=0;i<str_input.length();i++){
if(!isdigit(str_input[i]) && (i!=0 || (i==0 && str_input[i]!='-'))){
return false;
}
}
return true;
}
string String_Stuff::time_string(int seconds,bool highest){
string text="";
int minutes=0;
int hours=0;
int days=0;
int weeks=0;
int months=0;
int years=0;
for(int i=seconds;i>=60;){
minutes++;
i-=60;
if(i<60){
seconds=i;
}
}
for(int i=minutes;i>=60;){
hours++;
i-=60;
if(i<60){
minutes=i;
}
}
for(int i=hours;i>=24;){
days++;
i-=24;
if(i<24){
hours=i;
}
}
for(int i=days;i>=7;){
weeks++;
i-=7;
if(i<7){
days=i;
}
}
for(int i=weeks;i>=4;){
months++;
i-=4;
if(i<4){
weeks=i;
}
}
for(int i=months;i>=12;){
years++;
i-=12;
if(i<12){
months=i;
}
}
if(years>0){
text+=num_to_string(years)+" year";
if(years!=1){
text+="s";
}
if(!highest){
text+=", ";
}
}
if((!highest || years<=0) && months>0){
text+=num_to_string(months)+" month";
if(months!=1){
text+="s";
}
if(!highest){
text+=", ";
}
}
if((!highest || (years<=0 && months<=0)) && weeks>0){
text+=num_to_string(weeks)+" week";
if(weeks!=1){
text+="s";
}
if(!highest){
text+=", ";
}
}
if((!highest || (years<=0 && months<=0 && weeks<=0)) && days>0){
text+=num_to_string(days)+" day";
if(days!=1){
text+="s";
}
if(!highest){
text+=", ";
}
}
if((!highest || (years<=0 && months<=0 && weeks<=0 && days<=0)) && hours>0){
text+=num_to_string(hours)+" hour";
if(hours!=1){
text+="s";
}
if(!highest){
text+=", ";
}
}
if((!highest || (years<=0 && months<=0 && weeks<=0 && days<=0 && hours<=0)) && minutes>0){
text+=num_to_string(minutes)+" minute";
if(minutes!=1){
text+="s";
}
if(!highest){
text+=", ";
}
}
if(!highest || (years<=0 && months<=0 && weeks<=0 && days<=0 && hours<=0 && minutes<=0)){
text+=num_to_string(seconds)+" second";
if(seconds!=1){
text+="s";
}
}
return text;
}
bool String_Stuff::string_to_bool(string get_string){
if(get_string=="true" || get_string=="1"){
return true;
}
else if(get_string=="false" || get_string=="0"){
return false;
}
else{
message_log.add_error("Invalid value for string_to_bool: '"+get_string+"'");
return false;
}
}
string String_Stuff::bool_to_string(bool get_bool){
if(get_bool){
return "true";
}
else{
return "false";
}
}
long String_Stuff::string_to_long(string get_string){
return strtol(get_string.c_str(),NULL,0);
}
unsigned long String_Stuff::string_to_unsigned_long(string get_string){
return strtoul(get_string.c_str(),NULL,0);
}
double String_Stuff::string_to_double(string get_string){
return strtod(get_string.c_str(),NULL);
}