-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlowVParser.h
197 lines (175 loc) · 4.95 KB
/
FlowVParser.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
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
#pragma once
#include <algorithm>
#include <string>
#include <vector>
namespace FlowVParser {
using namespace std;
inline std::vector<unsigned char>::iterator
findLastData(std::vector<unsigned char>::iterator pos) {
--pos;
if (*pos == '-')
--pos;
if (*pos == '-')
--pos;
if (*pos == '\n')
--pos;
if (*pos == '\r')
--pos;
++pos;
return pos;
}
inline std::vector<unsigned char>::iterator
findLastData(std::vector<unsigned char>::iterator pos,
const std::vector<unsigned char>::iterator begin) {
if (begin == pos)
return pos;
--pos;
if (*pos == '-') {
if (begin == pos)
return pos;
--pos;
}
if (*pos == '-') {
if (begin == pos)
return pos;
--pos;
}
if (*pos == '\n') {
if (begin == pos)
return pos;
--pos;
}
if (*pos == '\r') {
if (begin == pos)
return pos;
--pos;
}
++pos;
return pos;
}
inline std::vector<unsigned char>::iterator
find_first(const std::vector<unsigned char> &data, const char &c,
std::vector<unsigned char>::iterator pos) {
while (pos != data.end()) {
if (*pos == c)
return pos;
++pos;
}
return pos;
}
inline std::vector<unsigned char>::iterator
find_first_of(const std::vector<unsigned char> &data, const std::string &oneOf,
std::vector<unsigned char>::iterator pos) {
while (pos != data.end()) {
for (char c : oneOf) {
if (*pos == c)
return pos;
}
++pos;
}
return pos;
}
inline std::string goToNextLine(const std::vector<unsigned char> &data,
std::vector<unsigned char>::iterator &pos) {
auto start = pos;
if (*pos == '\n' || (*pos == '\r' && *(++pos) == '\n'))
++pos;
return std::string(start, pos);
}
inline std::vector<unsigned char>::iterator
find_first_not_of(const std::vector<unsigned char> &data,
const std::string ¬Of,
std::vector<unsigned char>::iterator pos) {
while (pos != data.end()) {
bool notOneOf = true;
for (char c : notOf) {
if (*pos == c) {
notOneOf = false;
break;
}
}
if (notOneOf)
return pos;
++pos;
}
return pos;
}
inline std::string goTo(std::vector<unsigned char> &data,
const std::string &toGoTo,
std::vector<unsigned char>::iterator &pos) {
auto start = pos;
pos = search(pos, data.end(), toGoTo.begin(), toGoTo.end());
return std::string(start, pos);
}
inline std::string gotoNextNonWhite(const std::vector<unsigned char> &data,
std::vector<unsigned char>::iterator &pos) {
auto start = pos;
pos = find_first_not_of(data, " \n\r", pos);
return std::string(start, pos);
}
inline std::string gotoNextNonAlpha(const std::vector<unsigned char> &data,
std::vector<unsigned char>::iterator &pos) {
auto start = pos;
pos = find_first_of(data, " \n\r", pos);
return std::string(start, pos);
}
inline std::string goToOne(const std::vector<unsigned char> &data,
const std::string &goToOne,
std::vector<unsigned char>::iterator &pos) {
auto start = pos;
pos = find_first_of(data, goToOne, pos);
return std::string(start, pos);
}
inline bool isOneOf(const std::vector<unsigned char>::iterator &pos,
const std::vector<std::string> &toCheck) {
for (const auto &check : toCheck) {
auto cpos = pos;
for (size_t i = 0; i <= check.size(); ++i) {
if (i == check.size()) {
return true;
}
if (*cpos != check.at(i)) {
break;
}
++cpos;
}
}
return false;
}
inline bool isDoubleNewLine(std::vector<unsigned char>::iterator &pos) {
return (*pos == '\n' && *(++pos) == '\n') ||
(*pos == '\r' && *(++pos) == '\n' && *(++pos) == '\r' &&
*(++pos) == '\n');
}
inline std::string goToEnd(const std::vector<unsigned char> &data,
size_t &pos) {
size_t start = pos;
pos = std::string::npos;
return std::string(start, pos);
}
inline std::string goToPrevLine(const std::vector<unsigned char> &data,
std::vector<unsigned char>::iterator &pos) {
auto start = pos;
if (*(--pos) == '\n' && *(--pos) == '\r')
--pos;
return std::string(pos, start);
}
inline std::string goToNewLine(const std::vector<unsigned char> &data,
std::vector<unsigned char>::iterator &pos) {
auto start = pos;
pos = find_first_of(data, "\n\r", pos);
return std::string(start, pos);
}
inline bool containsAt(const std::vector<unsigned char> &data,
const std::string &toCheck,
std::vector<unsigned char>::iterator &pos) {
if (pos + toCheck.size() > data.end())
return false;
size_t i = 0;
for (pos; i < toCheck.size() && pos != data.end(); ++pos) {
if (*pos != toCheck.at(i++))
return false;
}
return true;
}
}; // namespace FlowVParser