forked from easz/cpp-semver
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype.hpp
235 lines (195 loc) · 5.95 KB
/
type.hpp
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
#ifndef CPP_SEMVER_TYPE_HPP
#define CPP_SEMVER_TYPE_HPP
#include <string>
#include <vector>
namespace semver
{
// ------------ exception types ------------------------------------ //
struct semver_error : public std::runtime_error
{
semver_error(const std::string& msg) : std::runtime_error(msg) {}
};
// ------------ syntax types ------------------------------------ //
struct syntax
{
/*
* original NPM semver syntax grammar: https://docs.npmjs.com/misc/Syntax#range-grammar
*
* range-set ::= range ( logical-or range ) *
* logical-or ::= ( ' ' ) * '||' ( ' ' ) *
* range ::= hyphen | simple ( ' ' simple ) * | ''
* hyphen ::= partial ' - ' partial
* simple ::= primitive | partial | tilde | caret
* primitive ::= ( '<' | '>' | '>=' | '<=' | '=' | ) partial
* partial ::= xr ( '.' xr ( '.' xr qualifier ? )? )?
* xr ::= 'x' | 'X' | '*' | nr
* nr ::= '0' | ['1'-'9'] ( ['0'-'9'] ) *
* tilde ::= '~' partial
* caret ::= '^' partial
* qualifier ::= ( '-' pre )? ( '+' build )?
* pre ::= parts
* build ::= parts
* parts ::= part ( '.' part ) *
* part ::= nr | [-0-9A-Za-z]+
*/
enum class comparator
{
eq, lt, lte, gt, gte, tilde, caret
};
/// an integer or wildcard
struct xnumber
{
/// represents an integer
explicit xnumber(const int& val) : is_wildcard(false), value(val) {}
/// represents '*'
xnumber() : is_wildcard(true), value(0) {}
bool is_wildcard;
int value;
};
/// represents any type of 'simple', 'primitive', 'partial', 'tilde' or 'caret' from the grammar.
/// The default value represents *.*.*
struct simple
{
xnumber major;
xnumber minor;
xnumber patch;
std::string pre = "";
std::string build = "";
comparator cmp = comparator::eq;
};
/// intersection set (i.e. AND conjunction )
/// it can represents a hyphon or a simple set
struct range
{
bool as_hyphon = false; // hint to interpret the range as hyphon if possible
std::vector< simple > and_set;
};
/// union set (i.e. OR conjunction )
struct range_set
{
std::vector< range > or_set;
};
};
// ------------ semantic types ------------------------------------ //
struct semantic
{
struct boundary
{
int major = 0;
int minor = 0;
int patch = 0;
std::string pre = "";
bool is_max = false;
/// Represent a minimal version boundary
static boundary min()
{
return {};
}
/// Represent a maximal version boundary
static boundary max()
{
boundary b;
b.is_max = true;
return b;
}
};
/// default interval is *.*.* := [min, max]
struct interval
{
bool from_inclusive = true;
bool to_inclusive = true;
boundary from = boundary::min();
boundary to = boundary::max();
};
/// interval set (i.e. OR conjunction )
struct interval_set
{
std::vector< interval > or_set;
};
};
inline bool operator ==(const semantic::boundary& lhs, const semantic::boundary& rhs)
{
return (lhs.major == rhs.major) &&
(lhs.minor == rhs.minor) &&
(lhs.patch == rhs.patch) &&
(lhs.pre == rhs.pre) &&
(lhs.is_max == rhs.is_max);
}
inline bool operator !=(const semantic::boundary& lhs, const semantic::boundary& rhs)
{
return !(lhs == rhs);
}
inline bool operator <(const semantic::boundary& lhs, const semantic::boundary& rhs)
{
// TODO: improve the performance of comparison
// XXX: is_max case
if (lhs.is_max || rhs.is_max)
return !lhs.is_max && rhs.is_max; // 1.2.3 < max
if (lhs.major < rhs.major) // 1.* < 2.*
return true;
if ((lhs.major == rhs.major) && // 1.2.* < 1.3.*
(lhs.minor < rhs.minor))
return true;
if ((lhs.major == rhs.major) && // 1.2.3 < 1.2.4
(lhs.minor == rhs.minor) &&
(lhs.patch < rhs.patch))
return true;
// XXX: pre-release case
if ((lhs.major == rhs.major) && // 1.2.3-alpha < 1.2.3
(lhs.minor == rhs.minor) &&
(lhs.patch == rhs.patch) &&
(!lhs.pre.empty() && rhs.pre.empty()))
return true;
// XXX: pre-release case
if ((lhs.major == rhs.major) && // 1.2.3-alpha < 1.2.3-beta
(lhs.minor == rhs.minor) &&
(lhs.patch == rhs.patch) &&
(!lhs.pre.empty() && !rhs.pre.empty()) &&
(lhs.pre < rhs.pre))
return true;
return false;
}
inline bool operator >(const semantic::boundary& lhs, const semantic::boundary& rhs)
{
return (lhs != rhs) && !(lhs < rhs);
}
inline bool operator <=(const semantic::boundary& lhs, const semantic::boundary& rhs)
{
return (lhs < rhs) || (lhs == rhs);
}
inline bool operator >=(const semantic::boundary& lhs, const semantic::boundary& rhs)
{
return (lhs > rhs) || (lhs == rhs);
}
inline bool operator ==(const semantic::interval& lhs, const semantic::interval& rhs)
{
return (lhs.from_inclusive == rhs.from_inclusive) &&
(lhs.to_inclusive == rhs.to_inclusive) &&
(lhs.from == rhs.from) &&
(lhs.to == rhs.to);
}
inline bool operator !=(const semantic::interval& lhs, const semantic::interval& rhs)
{
return !(lhs == rhs);
}
inline bool operator <(const semantic::interval& lhs, const semantic::interval& rhs)
{
if (lhs.to < rhs.from ||
(lhs.to == rhs.from && (!lhs.to_inclusive || !rhs.from_inclusive)))
return true;
return false;
}
inline bool operator <=(const semantic::interval& lhs, const semantic::interval& rhs)
{
return (lhs < rhs) || (lhs == rhs);
}
inline bool operator >(const semantic::interval& lhs, const semantic::interval& rhs)
{
return (lhs != rhs) && !(lhs < rhs);
}
inline bool operator >=(const semantic::interval& lhs, const semantic::interval& rhs)
{
return (lhs > rhs) || (lhs == rhs);
}
}
#endif