From 02b6c351486134b06077aae6196c7f319ce36eaa Mon Sep 17 00:00:00 2001 From: fabolous005 Date: Wed, 23 Oct 2024 00:54:48 +0200 Subject: [PATCH] add variables and algorithm1 to pms sort --- sorting.sh | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- test | 15 +++++++++ 2 files changed, 103 insertions(+), 2 deletions(-) diff --git a/sorting.sh b/sorting.sh index bbcb818..b21d085 100644 --- a/sorting.sh +++ b/sorting.sh @@ -23,8 +23,94 @@ function pms_sort() { } +# Original python code for portage can be found here: +# /usr/lib/python3.12/site-packages/portage/versions.py:121 function pms_inner_sort() { - echo "first: $1" >> ./test - echo "second: $2" >> ./test + local _unknown_repo="__unknown__" + + # \w is [a-zA-Z0-9_] + + # PMS 3.1.3: A slot name may contain any of the characters [A-Za-z0-9+_.-]. + # It must not begin with a hyphen or a dot. + local _slot="([\w+][\w+.-]*)" + + # 2.1.1 A category name may contain any of the characters [A-Za-z0-9+_.-]. + # It must not begin with a hyphen or a dot. + local _cat="[\w+][\w+.-]*" + + # 2.1.2 A package name may contain any of the characters [A-Za-z0-9+_-]. + # It must not begin with a hyphen, + # and must not end in a hyphen followed by one or more digits. + local _pkg="[\w+][\w+-]*?" + + local _v="([0-9]+)((\.[0-9]+)*)([a-z]?)((_(pre|p|beta|alpha|rc)[0-9]*)*)" + local _rev="[0-9]+" + local _vr="$_v" + _vr+="?(-r(" + _vr+="$_rev" + _vr+="))?" + + local _cp="(" + _cp+="$_cat" + _cp+="/" + _cp+="$_pkg" + _cp+="(-" + _cp+="$_vr" + _cp+=")?)" + + local _cpv="(" + _cpv+="$_cp" + _cpv+="-" + _cpv+="$_vr" + _cpv+=")" + + local _pv="(?P" + _pv+="$_pkg" + _pv+="(?P-" + _pv+="$_vr" + _pv+=")?)" + _pv+="-(?P" + _pv+="$_v" + _pv+=")(-r(?P" + _pv+="$_rev" + _pv+="))?" + + # local ver_regexp= + local ver_regexp="^" + ver_regexp+="$_vr" + ver_regexp+="$" + + declare -a match1=() + declare -a match2=() + + if [[ $1 =~ $ver_regexp ]]; then + for (( i=1; i<${#BASH_REMATCH[@]}; i++ )); do + match1+=("${BASH_REMATCH[$i]}") + done + else + # TODO: replace these with eerrors and ETOOLS_DEBUG checks + echo "ERROR: syntax error in version: $1" && return 2; + fi + if [[ $2 =~ $ver_regexp ]]; then + for (( i=1; i<${#BASH_REMATCH[@]}; i++ )); do + match2+=("${BASH_REMATCH[$i]}") + done + else + # TODO: replace these with eerrors and ETOOLS_DEBUG checks + echo "ERROR: syntax error in version: $2" && return 2; + fi + + echo "$ver_regexp" + echo "${match1[@]}" + echo "${match2[@]}" + + (( match1[0] < match2[0] )) && { + echo "$2" + return 0 + } || { + echo "$1" + return 0 + } + echo "$1" } diff --git a/test b/test index 3dc040c..9cb3c4d 100644 --- a/test +++ b/test @@ -13,3 +13,18 @@ second: 0.12.4 comparing: 1 and 2 args[0] = 12.23.34_dev-r43 and args[1] = 0.12.4 and args[2] = 234.34.76-r6 +comparing: 0 and 1 +args[0] = 12.23.34_dev-r43 and args[1] = 0.12.4 and args[2] = 234.34.76-r6 + +comparing: 1 and 2 +args[0] = 12.23.34_dev-r43 and args[1] = 0.12.4 and args[2] = 234.34.76-r6 + +comparing: 0 and 1 +args[0] = 12.23.34-r43 and args[1] = 0.12.4 and args[2] = 234.34.76-r6 + +comparing: 1 and 2 +args[0] = 12.23.34-r43 and args[1] = 0.12.4 and args[2] = 234.34.76-r6 + +comparing: 0 and 1 +args[0] = 12.23.34-r43 and args[1] = 0.12.4 and args[2] = +