nspkg / engine / version.v
128 lines · 122 sloc · 3.32 KB · c01b0795d1db56c7bce2539b2c46b110637fdc1a
Raw
1// derived from pacman lib/libalpm/version.c, copyright (c) 2006-2025
2// pacman development team. SPDX-License-Identifier: GPL-2.0-or-later
3module engine
4
5// engine_version identifies this build of the package engine.
6pub const engine_version = $d('nspkg_engine_version', '0.0.1')
7
8struct VersionParts {
9 epoch string
10 version string
11 release string
12 has_release bool
13}
14
15// All spans borrow the input for the duration of pkg_vercmp. Segment comparisons
16// use their lengths, so only the original input needs the C-string boundary.
17fn split_version(input string) VersionParts {
18 nul := input.index_u8(0)
19 value := if nul < 0 { input } else { unsafe { input.substr_unsafe(0, nul) } }
20 mut digit_end := 0
21 for digit_end < value.len && value[digit_end].is_digit() {
22 digit_end++
23 }
24 mut epoch := '0'
25 mut start := 0
26 if digit_end < value.len && value[digit_end] == `:` {
27 if digit_end > 0 {
28 epoch = unsafe { value.substr_unsafe(0, digit_end) }
29 }
30 start = digit_end + 1
31 }
32 release_at := value.last_index_u8(`-`)
33 if release_at >= start {
34 return VersionParts{epoch, unsafe { value.substr_unsafe(start, release_at) }, unsafe { value.substr_unsafe(release_at + 1, value.len) }, true}
35 }
36 return VersionParts{epoch, unsafe { value.substr_unsafe(start, value.len) }, '', false}
37}
38
39// separators and trailing alpha segments follow pacman's rpmvercmp, not semver.
40fn compare_segments(a string, b string) int {
41 if a == b {
42 return 0
43 }
44 mut one := 0
45 mut two := 0
46 mut end_one := 0
47 mut end_two := 0
48 for one < a.len && two < b.len {
49 for one < a.len && !a[one].is_alnum() {
50 one++
51 }
52 for two < b.len && !b[two].is_alnum() {
53 two++
54 }
55 if one == a.len || two == b.len {
56 break
57 }
58 if one - end_one != two - end_two {
59 return if one - end_one < two - end_two { -1 } else { 1 }
60 }
61 end_one = one
62 end_two = two
63 numeric := a[one].is_digit()
64 for end_one < a.len && if numeric { a[end_one].is_digit() } else { a[end_one].is_letter() } {
65 end_one++
66 }
67 for end_two < b.len && if numeric { b[end_two].is_digit() } else { b[end_two].is_letter() } {
68 end_two++
69 }
70 if two == end_two {
71 return if numeric { 1 } else { -1 }
72 }
73 if numeric {
74 for one < end_one && a[one] == `0` {
75 one++
76 }
77 for two < end_two && b[two] == `0` {
78 two++
79 }
80 if end_one - one != end_two - two {
81 return if end_one - one > end_two - two { 1 } else { -1 }
82 }
83 }
84 left := unsafe { a.substr_unsafe(one, end_one) }
85 right := unsafe { b.substr_unsafe(two, end_two) }
86 cmp := left.compare(right)
87 if cmp != 0 { return cmp }
88 one = end_one
89 two = end_two
90 }
91 if one == a.len && two == b.len {
92 return 0
93 }
94 if (one == a.len && !(two < b.len && b[two].is_letter()))
95 || (one < a.len && a[one].is_letter()) {
96 return -1
97 }
98 return 1
99}
100
101// the public binding previously passed C strings; preserve its NUL boundary.
102fn c_string_prefix(value string) string {
103 at := value.index_u8(0)
104 if at >= 0 {
105 return value[..at]
106 }
107 return value
108}
109
110pub fn pkg_vercmp(a string, b string) int {
111 if a == b {
112 return 0
113 }
114 left := split_version(a)
115 right := split_version(b)
116 epoch := compare_segments(left.epoch, right.epoch)
117 if epoch != 0 {
118 return epoch
119 }
120 version := compare_segments(left.version, right.version)
121 if version != 0 {
122 return version
123 }
124 if left.has_release && right.has_release {
125 return compare_segments(left.release, right.release)
126 }
127 return 0
128}
129