-
Notifications
You must be signed in to change notification settings - Fork 1
/
StringParsers.h
175 lines (134 loc) · 3.68 KB
/
StringParsers.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
#pragma once
namespace constfuse::string {
struct ParseChar {
using is_parser_type = std::true_type;
using return_type = char;
const char ltr;
constexpr ParseChar(const char& _ltr) :ltr(_ltr) {};
template<typename Iterator>
bool operator()(Iterator& it, Iterator end, return_type* r) const {
if (it == end) return false;
if (*it == ltr) {
if (it != end) ++it;
if (r != nullptr) {
*r = ltr;
}
return true;
}
return false;
};
};
struct ParseNumber {
using is_parser_type = std::true_type;
using return_type = unsigned int;
template<typename Iterator>
bool operator()(Iterator& it, Iterator end, return_type* r) const {
if (it == end) return false;
if (*it >= '0' && *it <= '9') {
char num = *it;
if (it != end) it++;
*r = num - '0';
return true;
}
return false;
};
};
struct CharRange {
using is_parser_type = std::true_type;
using return_type = char;
const char range_start;
const char range_end;
constexpr CharRange(const char& start, const char& end)
:range_start(start), range_end(end) {}
template<typename Iterator>
bool operator()(Iterator& it, Iterator end, return_type* r) const {
if (it == end) return false;
if (*it >= range_start && *it <= range_end) {
*r = *it;
if (it != end) ++it;
return true;
}
return false;
}
};
template<typename Parser>
struct AcceptString {
using is_parser_type = std::true_type;
using return_type = std::string;
using parser_return_type = typename Parser::return_type;
Parser const p;
constexpr AcceptString(Parser const& par) :p(par) {};
template<typename Iterator>
bool operator()(Iterator& it, Iterator end, return_type* result) const {
parser_return_type p_result;
auto res = p(it, end, &p_result);
if (res) {
if constexpr (std::is_same_v<parser_return_type, char>) {
result->append(std::string(1, p_result));
}
else {
result->append(p_result);
}
return true;
}
return false;
}
};
struct ParseASCII {
using is_parser_type = std::true_type;
using return_type = char;
constexpr ParseASCII() {};
template<typename Iterator>
bool operator()(Iterator& it, Iterator end, return_type* result) const {
if (it == end) return false;
if (*it >= 0 && *it <= 255) {
*result = *it;
if (it != end) it++;
return true;
}
return false;
}
};
template<std::size_t Size>
struct ParseLit {
using is_parser_type = std::true_type;
using return_type = std::string;
const char* const p_;
const std::size_t sz_;
constexpr ParseLit(const char(&str)[Size]) :p_(str), sz_(Size - 1) {};
template<typename Iterator>
bool operator()(Iterator& it, Iterator end, return_type* result) const {
if (it == end) return false;
std::size_t cnt = 0;
while (it != end && *it == *(p_ + cnt))
{
result->append(std::string(1, *it));
++it; cnt++;
}
if (cnt == sz_) {
return true;
}
//Backtrack ?
return false;
}
};
constexpr auto operator ""_symb(const char letter) {
return ParseChar(letter);
};
constexpr auto operator ""_asymb(const char letter) {
return AcceptString(ParseChar(letter));
};
/*
If this made it into the spec for C++17 i could have been a templated user defined literal
there was an idea of using a array buffer and applying it to a lambda
but that just looks ugly and eats heap*/
template<char ...c>
constexpr auto symbs() {
return (ParseChar(c) || ...);
}
constexpr auto operator ""_range(const char* string, std::size_t size) {
//static_assert(size == 4,"Invalid Character range specified");
//good idea but not a constant expression :(
return CharRange(string[0], string[3]);
};
}