forked from easz/cpp-semver
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpp-semver.hpp
534 lines (435 loc) · 16.5 KB
/
cpp-semver.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
#ifndef CPP_SEMVER_HPP
#define CPP_SEMVER_HPP
#include "base/type.hpp"
#include "base/util.hpp"
#ifdef USE_PEGTL
#include "parser/peg.hpp" // PETGTL parser
#define PARSER peg::parser
#else
#include "parser/parser.hpp"
#define PARSER semver::parser
#endif
#include <string>
#include <vector>
#include <memory>
namespace semver
{
namespace detail
{
/* parse version range in string into syntactical representation @c syntax::range_set */
inline syntax::range_set parse(const std::string& input)
{
return PARSER(input);
}
/* parse syntactical representation @c syntax::simple and convert it to sementical representation @c semantic::interval */
inline semantic::interval parse(const syntax::simple& input)
{
// default result * := [min, max]
semantic::interval result;
semantic::boundary b; // associate boundary from input x.y.z-pre
// replace * with 0
b.major = (!input.major.is_wildcard ? input.major.value : 0);
b.minor = (!input.minor.is_wildcard ? input.minor.value : 0);
b.patch = (!input.patch.is_wildcard ? input.patch.value : 0);
b.pre = input.pre;
switch (input.cmp)
{
case syntax::comparator::lt: // <x.y.z-pre := [min, x.y.z-pre)
result.to_inclusive = false;
result.to = b;
if (input.major.is_wildcard ||
input.minor.is_wildcard ||
input.patch.is_wildcard) // <x.*-pre := [min, x.0.0)
result.to.pre = ""; // <x.y.*-pre := [min, x,y.0)
break;
case syntax::comparator::lte: // <=x.y.z.pre := [min, x.y.z-pre]
result.to = b;
if (input.major.is_wildcard ||
input.minor.is_wildcard ||
input.patch.is_wildcard) // <=x.*-pre := [min, 'x+1'.0.0)
{ // <=x.y.*-pre := [min, x,'y+1'.0)
result.to_inclusive = false;
result.to.pre = "";
}
if (input.major.is_wildcard) // <=* := [min, max]
{
result = semantic::interval();
}
else if (input.minor.is_wildcard) // <=x.*-pre := [min, 'x+1'.0.0)
{
result.to.major += 1;
result.to.patch = 0;
}
else if (input.patch.is_wildcard) // <=x.y.*-pre := [min, x,'y+1'.0)
{
result.to.minor += 1;
}
break;
case syntax::comparator::gt: // >x.y.z-pre := (x.y.z-pre, max]
result.from_inclusive = false;
result.from = b;
if (input.major.is_wildcard ||
input.minor.is_wildcard ||
input.patch.is_wildcard) // >x.*-pre := ['x+1', max]
{ // >x.y.*-pre := [x.'y+1', max]
result.from_inclusive = true;
result.from.pre = "";
}
if (input.major.is_wildcard)
{
result.from = semantic::boundary::max(); // invalid set
}
else if (input.minor.is_wildcard) // >x.*-pre := ['x+1'.0.0, max]
{
result.from.major += 1;
result.from.patch = 0;
}
else if (input.patch.is_wildcard) // >x.y.*-pre := [x,'y+1'.0, max]
{
result.from.minor += 1;
}
break;
case syntax::comparator::gte: // >=x.y.z-pre := [x.y.z-pre, max]
result.from = b;
if (input.major.is_wildcard ||
input.minor.is_wildcard ||
input.patch.is_wildcard) // >=x.*-pre := [x.0.0, max]
result.from.pre = ""; // >=x.y.*-pre := [x.y.0, max]
break;
case syntax::comparator::caret:
result.from = b;
result.to = b;
if (input.major.is_wildcard ||
input.minor.is_wildcard ||
input.patch.is_wildcard)
result.from.pre = "";
result.to_inclusive = false;
result.to.pre = "";
if (input.major.is_wildcard) // ^* := [min, max]
{
result = semantic::interval();
}
else if (input.major.value != 0 || // ^x.y.z-pre := [x.y.z-pre, 'x+1'.0.0)
input.minor.is_wildcard) // ^x.y := [x.y.0, 'x+1'.0.0)
{ // ^x := [x.0.0, 'x+1'.0.0)
result.to.major += 1; // ^0 := [0.0.0, 1.0.0)
result.to.minor = 0;
result.to.patch = 0;
}
else if (input.minor.value != 0 || // ^0.y.z := [0.y.z, 0.'y+1'.z)
input.patch.is_wildcard) // ^0.y := [0.y.0, 0.'y+1'.0)
{
result.to.minor += 1;
result.to.patch = 0;
}
else if (input.patch.value != 0)
{
result.to.patch += 1; // ^0.0.z := [0.0.z, 0.0.'z+1')
}
break;
case syntax::comparator::tilde:
result.from = b;
result.to = b;
if (input.major.is_wildcard ||
input.minor.is_wildcard ||
input.patch.is_wildcard)
result.from.pre = "";
result.to_inclusive = false;
result.to.pre = "";
if (input.major.is_wildcard) // ~* := [min, max]
{
result = semantic::interval();
}
else if (input.minor.is_wildcard) // ~x-pre := [x.0.0, 'x+1'.0.0)
{
result.from.pre = "";
result.to.major += 1;
result.to.minor = 0;
result.to.patch = 0;
}
else // ~x.y.z-pre := [x.y.z-pre, x.'y+1'.0)
{
result.to.minor += 1;
result.to.patch = 0;
}
break;
case syntax::comparator::eq:
default:
result.from = b; // x.y.z := [x.y.z, x.y.z]
result.to = b;
if (input.major.is_wildcard ||
input.minor.is_wildcard ||
input.patch.is_wildcard) // =x.*-pre := [x.0.0, 'x+1'.0.0)
{ // =x.y.*-pre := [x.y.0, x.'y+1'.0)
result.from.pre = "";
result.to_inclusive = false;
result.to.pre = "";
}
if (input.major.is_wildcard) // * := [min, max]
{
result = semantic::interval();
}
else if (input.minor.is_wildcard) // x := [x.0.0, 'x+1'.0.0)
{
result.from.patch = 0;
result.to.major += 1;
result.to.patch = 0;
}
else if (input.patch.is_wildcard) // x.y := [x.y.0, x.'y+1'.0)
{
result.to.minor += 1;
}
}
return result;
};
/* calculate AND-conjunction from a list of @c semantic::interval */
inline std::unique_ptr<semantic::interval> and_conj(const std::vector< semantic::interval >& input)
{
const size_t size = input.size();
if (size == 0)
return nullptr;
semantic::interval result = input.at(0);
for (size_t i = 1; i < size; i++)
{
const semantic::interval& span = input.at(i);
// prerelease tag case
{
const bool result_is_range = (result.from != result.to || result.from_inclusive != result.to_inclusive);
const bool span_is_range = (span.from != span.to || span.from_inclusive != span.to_inclusive);
if (result_is_range != span_is_range)
{
const semantic::interval& range = (result_is_range ? result : span);
const semantic::interval& target = (result_is_range ? span : result);
const bool range_from_has_pre = !range.from.pre.empty();
const bool range_to_has_pre = !range.to.pre.empty();
const bool target_has_pre = !target.from.pre.empty();
if (target_has_pre)
{
// 1-rc not in '0 - 10'
if (!range_from_has_pre && !range_to_has_pre)
return nullptr;
// 1-rc in '1-beta - 10'
if (range_from_has_pre && !range_to_has_pre)
if (range.from.major != target.from.major ||
range.from.minor != target.from.minor ||
range.from.patch != target.from.patch)
return nullptr;
// 10-beta in '1 - 10-rc'
if (!range_from_has_pre && range_to_has_pre)
if (range.to.major != target.to.major ||
range.to.minor != target.to.minor ||
range.to.patch != target.to.patch)
return nullptr;
// 1-rc or 10-beta in '1-beta - 10-rc'
if (range_from_has_pre && range_to_has_pre)
if (!((range.from.major == target.from.major &&
range.from.minor == target.from.minor &&
range.from.patch == target.from.patch) ||
(range.to.major == target.to.major &&
range.to.minor == target.to.minor &&
range.to.patch == target.to.patch)))
return nullptr;
}
}
} // prerelease tag case
if ((span.from > result.from) ||
((span.from == result.from) && !span.from_inclusive))
{
result.from = span.from;
result.from_inclusive = span.from_inclusive;
}
if ((span.to < result.to) ||
((span.to == result.to) && !span.to_inclusive))
{
result.to = span.to;
result.to_inclusive = span.to_inclusive;
}
}
if ((result.from > result.to) ||
((result.from == result.to) && (result.from_inclusive != result.to_inclusive)))
return nullptr;
return std::unique_ptr<semantic::interval>(new semantic::interval(result));
}
/* parse syntactical representation @c syntax::range_set and convert it to sementical representation @c semantic::interval_set */
inline semantic::interval_set parse(const syntax::range_set& input)
{
semantic::interval_set interval_set;
for (const syntax::range& range : input.or_set)
{
std::vector<semantic::interval> and_set;
for (const syntax::simple& simple : range.and_set)
and_set.emplace_back(parse(simple));
const std::unique_ptr<semantic::interval> conj = and_conj(and_set);
if (conj)
interval_set.or_set.emplace_back(std::move(*conj));
}
return interval_set;
}
/* check if two @c semantic::interval_set intersect with each other */
inline bool intersects(const semantic::interval_set& s1, const semantic::interval_set& s2)
{
if (s1.or_set.empty() || s2.or_set.empty())
return false;
for (const semantic::interval& intval_1 : s1.or_set)
for (const semantic::interval& intval_2 : s2.or_set)
if (and_conj({ intval_1, intval_2 }))
return true;
return false;
}
/* check if two @c syntax::range_set intersect with each other */
inline bool intersects(const syntax::range_set& rs1, const syntax::range_set& rs2)
{
return intersects(parse(rs1), parse(rs2));
}
/** parse or cast as a syntax::simple if possible */
inline syntax::simple as_simple(const std::string& s)
{
syntax::range_set parsed = parse(s);
if (!parsed.or_set.empty() && !parsed.or_set.at(0).and_set.empty())
return std::move(parsed.or_set.at(0).and_set.at(0));
return{};
}
}
// *************** API **************************************** //
/** Return true if the comparator or range intersect */
inline bool intersects(const std::string& range)
{
return detail::intersects(detail::parse(range), detail::parse("*"));
}
/** Return true if the two supplied ranges or comparators intersect. */
inline bool intersects(const std::string& range1, const std::string& range2)
{
return detail::intersects(detail::parse(range1), detail::parse(range2));
}
/** Return true if the version satisfies the range */
inline bool satisfies(const std::string& version, const std::string& range)
{
return detail::intersects(detail::parse(version), detail::parse(range));
}
/** v1 == v2 */
inline bool eq(const std::string& v1, const std::string& v2)
{
semantic::interval_set interval_set_1 = detail::parse(detail::parse(v1));
semantic::interval_set interval_set_2 = detail::parse(detail::parse(v2));
if (interval_set_1.or_set.empty() && interval_set_2.or_set.empty())
return true;
else if (interval_set_1.or_set.empty() != interval_set_2.or_set.empty())
return false;
for (const semantic::interval& interval_1 : interval_set_1.or_set)
for (const semantic::interval& interval_2 : interval_set_2.or_set)
if (interval_1 == interval_2)
return true;
return false;
}
/** v1 != v2 */
inline bool neq(const std::string& v1, const std::string& v2)
{
return !eq(v1, v2);
}
/** v1 > v2 */
inline bool gt(const std::string& v1, const std::string& v2)
{
semantic::interval_set interval_set_1 = detail::parse(detail::parse(v1));
semantic::interval_set interval_set_2 = detail::parse(detail::parse(v2));
if (!interval_set_1.or_set.empty() && interval_set_2.or_set.empty())
return true;
else if (interval_set_1.or_set.empty() || interval_set_2.or_set.empty())
return false;
for (const semantic::interval& interval_1 : interval_set_1.or_set)
for (const semantic::interval& interval_2 : interval_set_2.or_set)
if (interval_1 > interval_2)
return true;
return false;
}
/** v1 >= v2 */
inline bool gte(const std::string& v1, const std::string& v2)
{
if (eq(v1, v2) || gt(v1, v2))
return true;
return false;
}
/** v1 < v2 */
inline bool lt(const std::string& v1, const std::string& v2)
{
if (!eq(v1, v2) && !gt(v1, v2))
return true;
return false;
}
/** v1 <= v2 */
inline bool lte(const std::string& v1, const std::string& v2)
{
if (eq(v1, v2) || lt(v1, v2))
return true;
return false;
}
/** Return true if version is greater than all the versions possible in the range. */
inline bool gtr(const std::string& version, const std::string& range)
{
semantic::interval_set interval_set_v = detail::parse(detail::parse(version));
semantic::interval_set interval_set_r = detail::parse(detail::parse(range));
if (!interval_set_v.or_set.empty() && interval_set_r.or_set.empty())
return true;
else if (interval_set_v.or_set.empty() || interval_set_r.or_set.empty())
return false;
for (const semantic::interval& interval_v : interval_set_v.or_set)
for (const semantic::interval& interval_r : interval_set_r.or_set)
if (interval_v <= interval_r)
return false;
return true;
}
/** Return true if version is less than all the versions possible in the range. */
inline bool ltr(const std::string& version, const std::string& range)
{
semantic::interval_set interval_set_v = detail::parse(detail::parse(version));
semantic::interval_set interval_set_r = detail::parse(detail::parse(range));
if (interval_set_v.or_set.empty() && !interval_set_r.or_set.empty())
return true;
else if (interval_set_v.or_set.empty() || interval_set_r.or_set.empty())
return false;
for (const semantic::interval& interval_v : interval_set_v.or_set)
for (const semantic::interval& interval_r : interval_set_r.or_set)
if (interval_v >= interval_r)
return false;
return true;
}
/** Return true if the version or range is valid */
inline bool valid(const std::string& s)
{
try
{
semantic::interval_set interval_set_s = detail::parse(detail::parse(s));
return !interval_set_s.or_set.empty();
}
catch (semver_error const&)
{
return false;
}
}
/** Return the major version number. */
inline int major(const std::string& version)
{
const auto& major = detail::as_simple(version).major;
return !major.is_wildcard ? major.value : 0;
}
/** Return the minor version number. */
inline int minor(const std::string& version)
{
const auto& minor = detail::as_simple(version).minor;
return !minor.is_wildcard ? minor.value : 0;
}
/** Return the patch version number. */
inline int patch(const std::string& version)
{
const auto& patch = detail::as_simple(version).patch;
return !patch.is_wildcard ? patch.value : 0;
}
/** Returns an array of prerelease components. */
inline std::vector<std::string> prerelease(const std::string& version)
{
const auto& pre = detail::as_simple(version).pre;
if (pre.empty())
return {};
return split(pre, ".");
}
}
#endif