Skip to content

Instantly share code, notes, and snippets.

@Oldes
Last active March 7, 2024 10:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Oldes/5e864af638300f93e802760913d3c868 to your computer and use it in GitHub Desktop.
Save Oldes/5e864af638300f93e802760913d3c868 to your computer and use it in GitHub Desktop.
Sorts block of semantic versioning strings with applied Maven exceptions
Rebol [
Name: semantic-version
Type: module
Purpose: "Sorts block of semantic versioning strings with applied Maven exceptions"
Exports: [sort-versions compare-versions]
Usage: [
sort-versions ["2-alpha" "2.0" "1" "2-sp" "1.2" "2-beta" "2-rc1" "2-rc2"]
;== ["1" "1.2" "2-alpha" "2-beta" "2-rc1" "2-rc2" "2.0" "2-sp"]
]
Version: 1.0.0
]
numeric: system/catalog/bitsets/numeric
exceptions: make map! [
"" <release>
"cr" "rc"
"release" <release>
"ga" <release>
"sp" <sp>
"alpha" "a"
"beta" "b"
"milestone" "m"
]
num-padding: 4
split-cache: make map! 8
split-version: function [
"Split semantic version into block with numbers and a qualifier with applied Maven exceptions"
version [string!]
][
if tmp: split-cache/:version [return tmp]
tmp: make block! 8
parse version [
some [
copy val: some numeric (append tmp to integer! val) opt #"."
]
opt [#"-"]
copy val: to end (
if num-padding > len: length? tmp [append/dup tmp 0 num-padding - len]
append tmp any [exceptions/:val val]
)
]
split-cache/:version: tmp
]
compare-versions: function[a [string!] b [string!]][
a: split-version a
b: split-version b
case [
a = b [return 0]
a > b [return 1]
]
-1
]
sort-versions: function [
"Sorts block of semantic versioning strings with applied Maven exceptions"
;; https://maven.apache.org/pom.html#version-order-specification
versions [block!] "Block of version strings"
][
sort/compare versions :compare-versions
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment