Repository navigation
Expand file tree
/
Copy pathSemVer.qll
More file actions
57 lines (51 loc) · 1.5 KB
/
Copy pathSemVer.qll
File metadata and controls
57 lines (51 loc) · 1.5 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
/**
* Provides logic for working SemVer (Semantic Versioning).
*/
overlay[local?]
module;
bindingset[str]
private string leftPad(string str) { result = ("0000" + str).suffix(str.length()) }
/**
* Gets the major number of a SemVer string.
*/
bindingset[s]
string getMajor(string s) { result = s.regexpCapture("v?(\\d+).*", 1) }
/**
* Gets the minor number of a SemVer string.
*/
bindingset[s]
string getMinor(string s) { result = s.regexpCapture("v?(\\d+)\\.(\\d+).*", 2) }
/**
* Gets the patch number of a SemVer string.
*/
bindingset[s]
string getPatch(string s) { result = s.regexpCapture("v?(\\d+)\\.(\\d+)\\.(\\d+).*", 3) }
/**
* Normalizes a SemVer string such that the lexicographical ordering
* of two normalized strings is consistent with the SemVer ordering.
*
* Pre-release information and build metadata is not yet supported.
*/
bindingset[orig]
string padSemVer(string orig, string major, string minor, string patch) {
major = getMajor(orig) and
(
minor = getMinor(orig)
or
not exists(getMinor(orig)) and minor = "0"
) and
(
patch = getPatch(orig)
or
not exists(getPatch(orig)) and patch = "0"
) and
result = leftPad(major) + "." + leftPad(minor) + "." + leftPad(patch)
}
/**
* Normalizes a SemVer string such that the lexicographical ordering
* of two normalized strings is consistent with the SemVer ordering.
*
* Pre-release information and build metadata is not yet supported.
*/
bindingset[orig]
string padSemVer(string orig) { result = padSemVer(orig, _, _, _) }