-
Notifications
You must be signed in to change notification settings - Fork 0
/
ez_cpp_utils.h
138 lines (129 loc) · 5.17 KB
/
ez_cpp_utils.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
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
#include <string>
#include <vector>
// We all need to split a string on a delimiter!
// This version splits on a single char and keeps all empty tokens, so:
// split on "a,b,c,,,f,g,h" with ',' as the delimiter returns a vector
// with size of 8 with two empty strings in indexes 3 and 4.
static void split_keep(std::string str, std::vector<std::string> &token_v, const char delim){
size_t start = str.find_first_not_of(delim), end=start;
while (start != std::string::npos){
// Find next occurence of delimiter
end = str.find(delim, start);
// Push back the token found into vector
// back to back tokens leave an empty string in the vector
token_v.push_back(str.substr(start, end-start));
// slide up one to get to the next starting point
start = end + 1;
}
}
// This version splits on a single char and discards all empty tokens, so:
// split on "a,b,c,,,f,g,h" with ',' as the delimiter returns a vector
// with size of 6 containing <a b c f g h>
static void split_discard(std::string str, std::vector<std::string> &token_v, const char delim){
size_t start = str.find_first_not_of(delim), end=start;
while (start != std::string::npos && end != std::string::npos){
// Find next occurence of delimiter
end = str.find(delim, start);
// Push back the token found into vector
// back to back tokens leave an empty string in the vector
token_v.push_back(str.substr(start, end-start));
// slide up to the next non-delimiter char (skips empty fields)
start = str.find_first_not_of(delim, end);
}
}
// This version splits on a single char and keeps/discards all empty tokens
// based on bool passed. true = keep, false = discard
// with size of 6 containing <a b c f g h>
// Obviously, this could replace the two functions above if only dealing
// by adding the bool parameter: default = true (keep empty tokens)
static void split(std::string str, std::vector<std::string> &token_v, const char delim, bool keep_empty_tokens = true){
size_t start = str.find_first_not_of(delim), end=start;
while (start != std::string::npos && end != std::string::npos){
// Find next occurence of delimiter
end = str.find(delim, start);
// Push back the token found into vector
// back to back tokens leave an empty string in the vector
token_v.push_back(str.substr(start, end-start));
// choose to keep or discard
if(keep_empty_tokens) {
start = end + 1;
}
else {
// slide up to the next non-delimiter char (skips empty fields)
start = str.find_first_not_of(delim, end);
}
}
}
// This version splits on a string token (finds a matching sub-string)
// and keeps/discards all empty tokens based on bool passed. true = keep,
// false = discard
static void split(std::string str, std::vector<std::string> &token_v, const std::string delim, bool keep_empty_tokens = true){
size_t start = str.find_first_not_of(delim), end=start;
while (start != std::string::npos && end != std::string::npos){
// Find next occurence of delimiter
end = str.find(delim, start);
// Push back the token found into vector
// back to back tokens leave an empty string in the vector
token_v.push_back(str.substr(start, end-start));
// choose to keep or discard
if(keep_empty_tokens) {
start = end + 1;
}
else {
// slide up to the next non-delimiter char (skips empty fields)
start = str.find_first_not_of(delim, end);
}
}
}
/*
// Find the day of the year based on the system clock items year, month and day.
// Also accounts for leap years.
static uint16_t get_doy(const uint16_t year, const uint8_t mon, const uint8_t dom)
{
uint8_t days_in_feb = 28, doy; // day of year
doy = dom; // start with the day of the current month
// check for leap year
if( (year % 4 == 0 && year % 100 != 0 ) || (year % 400 == 0) )
{
days_in_feb = 29;
}
switch(mon)
{
// we don't have to check Jan since it will naturally be 1-31 as the DOY
case 2:
doy += 31;
break;
case 3:
doy += 31+days_in_feb;
break;
case 4:
doy += 31+days_in_feb+31;
break;
case 5:
doy += 31+days_in_feb+31+30;
break;
case 6:
doy += 31+days_in_feb+31+30+31;
break;
case 7:
doy += 31+days_in_feb+31+30+31+30;
break;
case 8:
doy += 31+days_in_feb+31+30+31+30+31;
break;
case 9:
doy += 31+days_in_feb+31+30+31+30+31+31;
break;
case 10:
doy += 31+days_in_feb+31+30+31+30+31+31+30;
break;
case 11:
doy += 31+days_in_feb+31+30+31+30+31+31+30+31;
break;
case 12:
doy += 31+days_in_feb+31+30+31+30+31+31+30+31+30;
break;
}
return doy; // return 0 to operating system
}
*/