forked from albertz/planet_wars-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.cpp
65 lines (54 loc) · 1.55 KB
/
utils.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
// code under GPLv3
// by Albert Zeyer
#include "utils.h"
#include <iostream>
#include <cstring>
#include <sys/time.h> // gettimeofday
long currentTimeMillis() {
struct Timeval : timeval {
Timeval() {
gettimeofday(this, NULL);
}
};
static Timeval start;
struct timeval end;
gettimeofday(&end, NULL);
long mtime, seconds, useconds;
seconds = end.tv_sec - start.tv_sec;
useconds = end.tv_usec - start.tv_usec;
mtime = (seconds * 1000.0 + useconds / 1000.0) + 0.5;
return mtime;
}
typedef unsigned char uchar;
void Tokenize(const std::string& s,
const std::string& delimiters,
std::vector<std::string>& tokens) {
std::string::size_type lastPos = s.find_first_not_of(delimiters, 0);
std::string::size_type pos = s.find_first_of(delimiters, lastPos);
while (std::string::npos != pos || std::string::npos != lastPos) {
tokens.push_back(s.substr(lastPos, pos - lastPos));
lastPos = s.find_first_not_of(delimiters, pos);
pos = s.find_first_of(delimiters, lastPos);
}
}
std::string TrimSpaces(const std::string& str) {
size_t start = 0;
for(size_t i = 0; i < str.size(); ++i)
if(!isspace((uchar)str[i]) || isgraph((uchar)str[i])) {
start = i;
break;
}
size_t n = 0;
for(size_t i = str.size(); i > start; --i)
if(!isspace((uchar)str[i-1]) || isgraph((uchar)str[i-1])) {
n = i - start;
break;
}
return str.substr(start, n);
}
std::string ToLower(const std::string& str) {
std::string res;
for(std::string::const_iterator i = str.begin(); i != str.end(); i++)
res += tolower((uchar)*i);
return res;
}