-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSemver.cpp
More file actions
728 lines (583 loc) · 15.8 KB
/
Copy pathSemver.cpp
File metadata and controls
728 lines (583 loc) · 15.8 KB
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
/**
*
* @file Semver.cpp
* @author Gaspard Kirira
*
* Copyright 2025, Gaspard Kirira. All rights reserved.
* https://github.com/vixcpp/vix
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
* Vix.cpp
*/
#include <vix/cli/util/Semver.hpp>
#include <algorithm>
#include <cctype>
#include <optional>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
namespace vix::cli::util::semver
{
namespace
{
struct Identifier
{
bool numeric{false};
std::string text;
};
struct Version
{
int major{0};
int minor{0};
int patch{0};
std::vector<Identifier> prerelease;
};
enum class Op
{
Any,
Eq,
Gt,
Gte,
Lt,
Lte
};
struct Comparator
{
Op op{Op::Any};
Version version{};
};
struct Clause
{
bool any{false};
std::vector<Comparator> comparators;
};
struct Range
{
std::vector<Clause> clauses;
};
struct PartialVersion
{
bool any{false};
std::optional<int> major;
std::optional<int> minor;
std::optional<int> patch;
bool wildcardMajor{false};
bool wildcardMinor{false};
bool wildcardPatch{false};
int specifiedComponents{0};
std::vector<Identifier> prerelease;
bool hasPrerelease{false};
};
std::string trim_copy(std::string s)
{
auto isws = [](unsigned char c)
{ return std::isspace(c) != 0; };
while (!s.empty() && isws(static_cast<unsigned char>(s.front())))
s.erase(s.begin());
while (!s.empty() && isws(static_cast<unsigned char>(s.back())))
s.pop_back();
return s;
}
bool is_digits(const std::string &s)
{
if (s.empty())
return false;
for (char c : s)
{
if (!std::isdigit(static_cast<unsigned char>(c)))
return false;
}
return true;
}
std::vector<std::string> split(const std::string &s, char delim)
{
std::vector<std::string> out;
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim))
out.push_back(item);
return out;
}
std::vector<std::string> split_ws(const std::string &s)
{
std::vector<std::string> out;
std::stringstream ss(s);
std::string item;
while (ss >> item)
out.push_back(item);
return out;
}
std::vector<std::string> split_or(const std::string &s)
{
std::vector<std::string> out;
std::string current;
for (std::size_t i = 0; i < s.size(); ++i)
{
if (i + 1 < s.size() && s[i] == '|' && s[i + 1] == '|')
{
out.push_back(trim_copy(current));
current.clear();
++i;
continue;
}
current.push_back(s[i]);
}
out.push_back(trim_copy(current));
return out;
}
std::vector<Identifier> parse_identifiers(const std::string &s)
{
std::vector<Identifier> ids;
if (s.empty())
return ids;
for (const auto &part : split(s, '.'))
{
Identifier id;
id.numeric = is_digits(part);
id.text = part;
ids.push_back(id);
}
return ids;
}
std::optional<Version> parse_version(const std::string &raw)
{
std::string s = trim_copy(raw);
if (s.empty())
return std::nullopt;
if (s.front() == 'v' || s.front() == 'V')
s.erase(s.begin());
const auto plusPos = s.find('+');
if (plusPos != std::string::npos)
s = s.substr(0, plusPos);
std::string core = s;
std::string prerelease;
const auto dashPos = s.find('-');
if (dashPos != std::string::npos)
{
core = s.substr(0, dashPos);
prerelease = s.substr(dashPos + 1);
}
const auto parts = split(core, '.');
if (parts.size() != 3)
return std::nullopt;
if (!is_digits(parts[0]) || !is_digits(parts[1]) || !is_digits(parts[2]))
return std::nullopt;
Version v;
v.major = std::stoi(parts[0]);
v.minor = std::stoi(parts[1]);
v.patch = std::stoi(parts[2]);
v.prerelease = parse_identifiers(prerelease);
return v;
}
int compare_identifiers(const Identifier &a, const Identifier &b)
{
if (a.numeric && b.numeric)
{
const long long av = std::stoll(a.text);
const long long bv = std::stoll(b.text);
if (av < bv)
return -1;
if (av > bv)
return 1;
return 0;
}
if (a.numeric && !b.numeric)
return -1;
if (!a.numeric && b.numeric)
return 1;
if (a.text < b.text)
return -1;
if (a.text > b.text)
return 1;
return 0;
}
int compare_versions(const Version &a, const Version &b)
{
if (a.major != b.major)
return (a.major < b.major) ? -1 : 1;
if (a.minor != b.minor)
return (a.minor < b.minor) ? -1 : 1;
if (a.patch != b.patch)
return (a.patch < b.patch) ? -1 : 1;
const bool aPre = !a.prerelease.empty();
const bool bPre = !b.prerelease.empty();
if (!aPre && !bPre)
return 0;
if (!aPre && bPre)
return 1;
if (aPre && !bPre)
return -1;
const std::size_t n = std::max(a.prerelease.size(), b.prerelease.size());
for (std::size_t i = 0; i < n; ++i)
{
if (i >= a.prerelease.size())
return -1;
if (i >= b.prerelease.size())
return 1;
const int cmp = compare_identifiers(a.prerelease[i], b.prerelease[i]);
if (cmp != 0)
return cmp;
}
return 0;
}
bool is_wildcard_token(const std::string &s)
{
return s == "*" || s == "x" || s == "X";
}
std::optional<PartialVersion> parse_partial_version(const std::string &raw)
{
std::string s = trim_copy(raw);
if (s.empty())
return std::nullopt;
if (s == "*" || s == "x" || s == "X" || s == "latest")
{
PartialVersion pv;
pv.any = true;
return pv;
}
if (s.front() == 'v' || s.front() == 'V')
s.erase(s.begin());
const auto plusPos = s.find('+');
if (plusPos != std::string::npos)
s = s.substr(0, plusPos);
std::string core = s;
std::string prerelease;
const auto dashPos = s.find('-');
if (dashPos != std::string::npos)
{
core = s.substr(0, dashPos);
prerelease = s.substr(dashPos + 1);
}
const auto parts = split(core, '.');
if (parts.empty() || parts.size() > 3)
return std::nullopt;
PartialVersion pv;
auto assign_part = [&](std::size_t index, const std::string &part) -> bool
{
if (is_wildcard_token(part))
{
if (index == 0)
pv.wildcardMajor = true;
else if (index == 1)
pv.wildcardMinor = true;
else
pv.wildcardPatch = true;
return true;
}
if (!is_digits(part))
return false;
const int value = std::stoi(part);
if (index == 0)
pv.major = value;
else if (index == 1)
pv.minor = value;
else
pv.patch = value;
pv.specifiedComponents++;
return true;
};
for (std::size_t i = 0; i < parts.size(); ++i)
{
if (!assign_part(i, parts[i]))
return std::nullopt;
}
pv.prerelease = parse_identifiers(prerelease);
pv.hasPrerelease = !pv.prerelease.empty();
return pv;
}
Version normalize_lower(const PartialVersion &pv)
{
Version v;
v.major = pv.major.value_or(0);
v.minor = pv.minor.value_or(0);
v.patch = pv.patch.value_or(0);
v.prerelease = pv.prerelease;
return v;
}
Version bump_major(const Version &v)
{
Version out = v;
out.major += 1;
out.minor = 0;
out.patch = 0;
out.prerelease.clear();
return out;
}
Version bump_minor(const Version &v)
{
Version out = v;
out.minor += 1;
out.patch = 0;
out.prerelease.clear();
return out;
}
Version bump_patch(const Version &v)
{
Version out = v;
out.patch += 1;
out.prerelease.clear();
return out;
}
std::vector<Comparator> make_xrange(const PartialVersion &pv)
{
if (pv.any || pv.wildcardMajor || !pv.major.has_value())
return {};
const Version lower = normalize_lower(pv);
if (pv.wildcardMinor || !pv.minor.has_value() || pv.specifiedComponents == 1)
{
return {
Comparator{Op::Gte, lower},
Comparator{Op::Lt, bump_major(lower)}};
}
if (pv.wildcardPatch || !pv.patch.has_value() || pv.specifiedComponents == 2)
{
return {
Comparator{Op::Gte, lower},
Comparator{Op::Lt, bump_minor(lower)}};
}
return {Comparator{Op::Eq, lower}};
}
std::vector<Comparator> make_caret_range(const PartialVersion &pv)
{
if (pv.any)
return {};
const Version lower = normalize_lower(pv);
Version upper = lower;
if (lower.major > 0)
upper = bump_major(lower);
else if (lower.minor > 0)
upper = bump_minor(lower);
else
upper = bump_patch(lower);
return {
Comparator{Op::Gte, lower},
Comparator{Op::Lt, upper}};
}
std::vector<Comparator> make_tilde_range(const PartialVersion &pv)
{
if (pv.any)
return {};
const Version lower = normalize_lower(pv);
Version upper = lower;
if (!pv.minor.has_value() || pv.specifiedComponents <= 1)
upper = bump_major(lower);
else
upper = bump_minor(lower);
return {
Comparator{Op::Gte, lower},
Comparator{Op::Lt, upper}};
}
std::optional<std::vector<Comparator>> parse_token_to_comparators(const std::string &tokenRaw)
{
const std::string token = trim_copy(tokenRaw);
if (token.empty())
return std::vector<Comparator>{};
if (token == "*" || token == "x" || token == "X" || token == "latest")
return std::vector<Comparator>{};
auto parse_after_prefix = [&](const std::string &rest) -> std::optional<PartialVersion>
{
return parse_partial_version(rest);
};
if (token.rfind("^", 0) == 0)
{
const auto pv = parse_after_prefix(token.substr(1));
if (!pv)
return std::nullopt;
return make_caret_range(*pv);
}
if (token.rfind("~", 0) == 0)
{
const auto pv = parse_after_prefix(token.substr(1));
if (!pv)
return std::nullopt;
return make_tilde_range(*pv);
}
auto make_single = [&](Op op, const std::string &rest) -> std::optional<std::vector<Comparator>>
{
const auto pv = parse_partial_version(rest);
if (!pv || pv->any)
return std::nullopt;
return std::vector<Comparator>{
Comparator{op, normalize_lower(*pv)}};
};
if (token.rfind(">=", 0) == 0)
return make_single(Op::Gte, token.substr(2));
if (token.rfind("<=", 0) == 0)
return make_single(Op::Lte, token.substr(2));
if (token.rfind(">", 0) == 0)
return make_single(Op::Gt, token.substr(1));
if (token.rfind("<", 0) == 0)
return make_single(Op::Lt, token.substr(1));
if (token.rfind("=", 0) == 0)
{
const auto pv = parse_partial_version(token.substr(1));
if (!pv)
return std::nullopt;
return make_xrange(*pv);
}
const auto pv = parse_partial_version(token);
if (!pv)
return std::nullopt;
return make_xrange(*pv);
}
std::optional<Range> parse_range(const std::string &raw)
{
const std::string s = trim_copy(raw);
Range range;
if (s.empty() || s == "*" || s == "latest")
{
Clause clause;
clause.any = true;
range.clauses.push_back(clause);
return range;
}
for (const auto &clauseStr : split_or(s))
{
Clause clause;
const auto tokens = split_ws(clauseStr);
if (tokens.empty())
{
clause.any = true;
range.clauses.push_back(clause);
continue;
}
for (const auto &token : tokens)
{
const auto maybeComparators = parse_token_to_comparators(token);
if (!maybeComparators)
return std::nullopt;
for (const auto &cmp : *maybeComparators)
clause.comparators.push_back(cmp);
}
if (clause.comparators.empty())
clause.any = true;
range.clauses.push_back(clause);
}
if (range.clauses.empty())
{
Clause clause;
clause.any = true;
range.clauses.push_back(clause);
}
return range;
}
bool comparator_matches(const Version &v, const Comparator &cmp)
{
const int c = compare_versions(v, cmp.version);
switch (cmp.op)
{
case Op::Any:
return true;
case Op::Eq:
return c == 0;
case Op::Gt:
return c > 0;
case Op::Gte:
return c >= 0;
case Op::Lt:
return c < 0;
case Op::Lte:
return c <= 0;
}
return false;
}
bool clause_allows_prerelease(const Version &v, const Clause &clause)
{
if (v.prerelease.empty())
return true;
for (const auto &cmp : clause.comparators)
{
if (!cmp.version.prerelease.empty() &&
cmp.version.major == v.major &&
cmp.version.minor == v.minor &&
cmp.version.patch == v.patch)
{
return true;
}
}
return false;
}
bool clause_matches(const Version &v, const Clause &clause)
{
if (clause.any)
return true;
if (!clause_allows_prerelease(v, clause))
return false;
for (const auto &cmp : clause.comparators)
{
if (!comparator_matches(v, cmp))
return false;
}
return true;
}
}
int compare(const std::string &lhs, const std::string &rhs)
{
const auto a = parse_version(lhs);
const auto b = parse_version(rhs);
if (!a && !b)
return 0;
if (!a)
return -1;
if (!b)
return 1;
return compare_versions(*a, *b);
}
bool satisfies(const std::string &version, const std::string &range)
{
const auto v = parse_version(version);
if (!v)
return false;
const auto r = parse_range(range);
if (!r)
return false;
for (const auto &clause : r->clauses)
{
if (clause_matches(*v, clause))
return true;
}
return false;
}
std::optional<std::string> resolveMaxSatisfying(
const std::vector<std::string> &versions,
const std::string &range)
{
std::optional<std::string> best;
for (const auto &version : versions)
{
if (!satisfies(version, range))
continue;
if (!best.has_value() || compare(version, *best) > 0)
best = version;
}
return best;
}
std::string findLatest(const std::vector<std::string> &versions)
{
std::string best;
for (const auto &version : versions)
{
if (best.empty() || compare(version, best) > 0)
best = version;
}
return best;
}
void sortAscending(std::vector<std::string> &versions)
{
std::sort(versions.begin(), versions.end(),
[](const std::string &a, const std::string &b)
{
return compare(a, b) < 0;
});
}
void sortDescending(std::vector<std::string> &versions)
{
std::sort(versions.begin(), versions.end(),
[](const std::string &a, const std::string &b)
{
return compare(a, b) > 0;
});
}
}