commit: 3ecbbfe53924f4bc312f2f464c11b7eca434534f
Author: Ulrich Müller <ulm <AT> gentoo <DOT> org>
AuthorDate: Thu Sep 21 12:56:04 2017 +0000
Commit: Ulrich Müller <ulm <AT> gentoo <DOT> org>
CommitDate: Thu Sep 21 12:56:04 2017 +0000
URL: https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=3ecbbfe5
eapi7-ver.eclass: Make ver_test() even faster.
eclass/eapi7-ver.eclass | 234 +++++++++++++++---------------------
eclass/tests/eapi7-ver_benchmark.sh | 4 +-
2 files changed, 96 insertions(+), 142 deletions(-)
diff --git a/eclass/eapi7-ver.eclass b/eclass/eapi7-ver.eclass
index 3ddd8954556..c64870cc1fe 100644
--- a/eclass/eapi7-ver.eclass
+++ b/eclass/eapi7-ver.eclass
@@ -176,6 +176,98 @@ ver_rs() {
echo "${comp[*]}"
}
+# @FUNCTION: _ver_compare
+# @USAGE: <va> <vb>
+# @RETURN: 1 if <va> < <vb>, 2 if <va> = <vb>, 3 if <va> > <vb>
+# @INTERNAL
+# @DESCRIPTION:
+# Compare two versions <va> and <vb>. If <va> is less than, equal to,
+# or greater than <vb>, return 1, 2, or 3 as exit status, respectively.
+_ver_compare() {
+ local va=${1} vb=${2} a an al as ar b bn bl bs br re
+
+
re="^([0-9]+(\.[0-9]+)*)([a-z]?)((_(alpha|beta|pre|rc|p)[0-9]*)*)(-r[0-9]+)?$"
+
+ [[ ${va} =~ ${re} ]] || die "${FUNCNAME}: invalid version: ${va}"
+ an=${BASH_REMATCH[1]}
+ al=${BASH_REMATCH[3]}
+ as=${BASH_REMATCH[4]}
+ ar=${BASH_REMATCH[7]}
+
+ [[ ${vb} =~ ${re} ]] || die "${FUNCNAME}: invalid version: ${vb}"
+ bn=${BASH_REMATCH[1]}
+ bl=${BASH_REMATCH[3]}
+ bs=${BASH_REMATCH[4]}
+ br=${BASH_REMATCH[7]}
+
+ # Compare numeric components (PMS algorithm 3.2)
+ # First component
+ a=${an%%.*}
+ b=${bn%%.*}
+ [[ 10#${a} -gt 10#${b} ]] && return 3
+ [[ 10#${a} -lt 10#${b} ]] && return 1
+
+ an=${an}.; an=${an#*.}
+ bn=${bn}.; bn=${bn#*.}
+ while [[ -n ${an} && -n ${bn} ]]; do
+ # Other components (PMS algorithm 3.3)
+ a=${an%%.*}
+ b=${bn%%.*}
+ if [[ ${a} == 0* || ${b} == 0* ]]; then
+ # Remove trailing zeros
+ while [[ ${a} == *0 ]]; do a=${a::-1}; done
+ while [[ ${b} == *0 ]]; do b=${b::-1}; done
+ [[ ${a} > ${b} ]] && return 3
+ [[ ${a} < ${b} ]] && return 1
+ else
+ [[ ${a} -gt ${b} ]] && return 3
+ [[ ${a} -lt ${b} ]] && return 1
+ fi
+ an=${an#*.}
+ bn=${bn#*.}
+ done
+ [[ -n ${an} ]] && return 3
+ [[ -n ${bn} ]] && return 1
+
+ # Compare letter components (PMS algorithm 3.4)
+ [[ ${al} > ${bl} ]] && return 3
+ [[ ${al} < ${bl} ]] && return 1
+
+ # Compare suffixes (PMS algorithm 3.5)
+ as=${as#_}${as+_}
+ bs=${bs#_}${bs+_}
+ while [[ -n ${as} && -n ${bs} ]]; do
+ # Compare each suffix (PMS algorithm 3.6)
+ a=${as%%_*}
+ b=${bs%%_*}
+ if [[ ${a%%[0-9]*} == "${b%%[0-9]*}" ]]; then
+ [[ 10#${a##*[a-z]} -gt 10#${b##*[a-z]} ]] && return 3
+ [[ 10#${a##*[a-z]} -lt 10#${b##*[a-z]} ]] && return 1
+ else
+ # Check for p first
+ [[ ${a%%[0-9]*} == p ]] && return 3
+ [[ ${b%%[0-9]*} == p ]] && return 1
+ # Hack: Use that alpha < beta < pre < rc alphabetically
+ [[ ${a} > ${b} ]] && return 3 || return 1
+ fi
+ as=${as#*_}
+ bs=${bs#*_}
+ done
+ if [[ -n ${as} ]]; then
+ a=${as%%_*}
+ [[ ${a%%[0-9]*} == p ]] && return 3 || return 1
+ elif [[ -n ${bs} ]]; then
+ b=${bs%%_*}
+ [[ ${b%%[0-9]*} == p ]] && return 1 || return 3
+ fi
+
+ # Compare revision components (PMS algorithm 3.7)
+ [[ 10#${ar#-r} -gt 10#${br#-r} ]] && return 3
+ [[ 10#${ar#-r} -lt 10#${br#-r} ]] && return 1
+
+ return 2
+}
+
# @FUNCTION: ver_test
# @USAGE: [<v1>] <op> <v2>
# @DESCRIPTION:
@@ -205,144 +297,6 @@ ver_test() {
*) die "${FUNCNAME}: invalid operator: ${op}" ;;
esac
- local
re="^[0-9]+(\.[0-9]+)*[a-z]?((_alpha|_beta|_pre|_rc|_p)[0-9]*)*(-r[0-9]+)?$"
- [[ ${va} =~ ${re} ]] || die "${FUNCNAME}: invalid version: ${va}"
- [[ ${vb} =~ ${re} ]] || die "${FUNCNAME}: invalid version: ${vb}"
-
- # explicitly strip -r0[00000...] to avoid overcomplexifying the algo
- [[ ${va} == *-r0* && 10#${va#*-r} -eq 0 ]] && va=${va%-r*}
- [[ ${vb} == *-r0* && 10#${vb#*-r} -eq 0 ]] && vb=${vb%-r*}
-
- local comp compb
- _ver_split "${vb}"
- compb=( "${comp[@]}" )
- _ver_split "${va}"
-
- local i sa sb ca cb wa wb result=0
- for (( i = 0;; i += 2 )); do
- sa=${comp[i]}
- ca=${comp[i+1]}
- sb=${compb[i]}
- cb=${compb[i+1]}
-
- # 1. determine the component type for Ca
- if [[ -z ${sa} ]]; then
- if [[ ${ca} == [0-9]* ]]; then
- # number without preceding sep may be either:
- # a. 1st version number
- # b. _foo suffix number
- # c. -rN revision number
- # weight is irrelevant since (a) occurs
simultaneously,
- # and (b)/(c) use weight of preceding component
- wa=0
- # method: plain numeric
- m=pn
- elif [[ -n ${ca} ]]; then
- # letter without preceding sep = letter after
version
- # weight below numeric, lexical method
- wa=9
- m=l
- else
- # empty == end of version string
- # weight below all positive stuff but above
negative
- wa=6
- m=
- fi
- elif [[ ${sa} == . ]]; then
- # number preceded by dot = numeric component
- # highest weight, weird PMS 2+ component comparison
- wa=10
- m=wn
- elif [[ ${sa} == _ ]]; then
- # _ implies _foo suffix
- # weights differ, comparison by weight
- case ${ca} in
- alpha) wa=2 ;;
- beta) wa=3 ;;
- rc) wa=4 ;;
- pre) wa=5 ;;
- p) wa=8 ;;
- *) die "Invalid version suffix: _${ca}";;
- esac
- m=
- elif [[ ${sa} == - ]]; then
- # - implies revision
- # weight below positive suffixes, no comparison
- [[ ${ca} == r ]] || die "Invalid version part: -${ca}"
- wa=7
- m=
- fi
-
- # 2. determine the component type for Cb
- if [[ -z ${sb} ]]; then
- if [[ ${cb} == [0-9]* ]]; then
- wb=0
- elif [[ -n ${cb} ]]; then
- wb=9
- else
- wb=6
- fi
- elif [[ ${sb} == . ]]; then
- wb=10
- elif [[ ${sb} == _ ]]; then
- case ${cb} in
- alpha) wb=2 ;;
- beta) wb=3 ;;
- rc) wb=4 ;;
- pre) wb=5 ;;
- p) wb=8 ;;
- *) die "Invalid version suffix: _${cb}";;
- esac
- elif [[ ${sb} == - ]]; then
- [[ ${cb} == r ]] || die "Invalid version part: -${cb}"
- wb=7
- fi
-
- # DEBUG
- #echo "$sa $ca [$wa] <$m> $sb $cb [$wb]" >&2
-
- # 3. compare weights, we can proceed further only if weights
match
- if [[ ${wa} -ne ${wb} ]]; then
- result=$(( wa - wb ))
- break
- fi
-
- # 4. both empty maybe?
- [[ -z ${ca} && -z ${cb} ]] && break
-
- # 5. compare components according to the algo
-
- # weird numeric is weird and reuses pn/l, so do it first
- # (PMS algo 3.3)
- if [[ ${m} == wn ]]; then
- if [[ ${ca} != 0* && ${cb} != 0* ]]; then
- # if neither of them starts with 0, use normal
numeric
- m=pn
- else
- # strip trailing zeros
- while [[ ${ca} == *0 ]]; do ca=${ca::-1}; done
- while [[ ${cb} == *0 ]]; do cb=${cb::-1}; done
- m=l
- fi
- fi
-
- case ${m} in
- pn) # plain numeric
- if [[ 10#${ca} -ne 10#${cb} ]]; then
- result=$(( 10#${ca} - 10#${cb} ))
- break
- fi
- ;;
- l) # lexical
- if [[ ${ca} != ${cb} ]]; then
- [[ ${ca} > ${cb} ]] && result=1 ||
result=-1
- break
- fi
- ;;
- '') ;;
- *) die "Unexpected comparison method m=${m}";;
- esac
- done
-
- test "${result}" "${op}" 0
+ _ver_compare "${va}" "${vb}"
+ test $? "${op}" 2
}
diff --git a/eclass/tests/eapi7-ver_benchmark.sh
b/eclass/tests/eapi7-ver_benchmark.sh
index b1e3ccb78ae..c4671371336 100755
--- a/eclass/tests/eapi7-ver_benchmark.sh
+++ b/eclass/tests/eapi7-ver_benchmark.sh
@@ -78,7 +78,7 @@ replacing_versionator() {
comparing() {
local x
- for x in {1..500}; do
+ for x in {1..1000}; do
ver_test 1b_p1 -le 1_p1
ver_test 1.1b -le 1.1
ver_test 12.2.5 -le 12.2b
@@ -143,5 +143,5 @@ get_times 1 cutting
get_times 10 cutting_versionator
get_times 1 replacing
get_times 10 replacing_versionator
-get_times 2 comparing
+get_times 1 comparing
get_times 10 comparing_versionator