// derived from pacman lib/libalpm/version.c, copyright (c) 2006-2025 // pacman development team. SPDX-License-Identifier: GPL-2.0-or-later module engine // engine_version identifies this build of the package engine. pub const engine_version = $d('nspkg_engine_version', '0.0.1') struct VersionParts { epoch string version string release string has_release bool } // All spans borrow the input for the duration of pkg_vercmp. Segment comparisons // use their lengths, so only the original input needs the C-string boundary. fn split_version(input string) VersionParts { nul := input.index_u8(0) value := if nul < 0 { input } else { unsafe { input.substr_unsafe(0, nul) } } mut digit_end := 0 for digit_end < value.len && value[digit_end].is_digit() { digit_end++ } mut epoch := '0' mut start := 0 if digit_end < value.len && value[digit_end] == `:` { if digit_end > 0 { epoch = unsafe { value.substr_unsafe(0, digit_end) } } start = digit_end + 1 } release_at := value.last_index_u8(`-`) if release_at >= start { return VersionParts{epoch, unsafe { value.substr_unsafe(start, release_at) }, unsafe { value.substr_unsafe(release_at + 1, value.len) }, true} } return VersionParts{epoch, unsafe { value.substr_unsafe(start, value.len) }, '', false} } // separators and trailing alpha segments follow pacman's rpmvercmp, not semver. fn compare_segments(a string, b string) int { if a == b { return 0 } mut one := 0 mut two := 0 mut end_one := 0 mut end_two := 0 for one < a.len && two < b.len { for one < a.len && !a[one].is_alnum() { one++ } for two < b.len && !b[two].is_alnum() { two++ } if one == a.len || two == b.len { break } if one - end_one != two - end_two { return if one - end_one < two - end_two { -1 } else { 1 } } end_one = one end_two = two numeric := a[one].is_digit() for end_one < a.len && if numeric { a[end_one].is_digit() } else { a[end_one].is_letter() } { end_one++ } for end_two < b.len && if numeric { b[end_two].is_digit() } else { b[end_two].is_letter() } { end_two++ } if two == end_two { return if numeric { 1 } else { -1 } } if numeric { for one < end_one && a[one] == `0` { one++ } for two < end_two && b[two] == `0` { two++ } if end_one - one != end_two - two { return if end_one - one > end_two - two { 1 } else { -1 } } } left := unsafe { a.substr_unsafe(one, end_one) } right := unsafe { b.substr_unsafe(two, end_two) } cmp := left.compare(right) if cmp != 0 { return cmp } one = end_one two = end_two } if one == a.len && two == b.len { return 0 } if (one == a.len && !(two < b.len && b[two].is_letter())) || (one < a.len && a[one].is_letter()) { return -1 } return 1 } // the public binding previously passed C strings; preserve its NUL boundary. fn c_string_prefix(value string) string { at := value.index_u8(0) if at >= 0 { return value[..at] } return value } pub fn pkg_vercmp(a string, b string) int { if a == b { return 0 } left := split_version(a) right := split_version(b) epoch := compare_segments(left.epoch, right.epoch) if epoch != 0 { return epoch } version := compare_segments(left.version, right.version) if version != 0 { return version } if left.has_release && right.has_release { return compare_segments(left.release, right.release) } return 0 }