diff options
author | Ulrich Müller <ulm@gentoo.org> | 2017-09-23 10:58:43 +0200 |
---|---|---|
committer | Ulrich Müller <ulm@gentoo.org> | 2017-09-26 20:46:27 +0200 |
commit | 7175c90c8c4bc1332899dffa6b5fa7a7b30ad2a4 (patch) | |
tree | 37cb5b39880900b40b93c43a5c3bc93aff2a69d7 /eclass | |
parent | eapi7-ver.eclass: New algorithm for ver_test(). (diff) | |
download | gentoo-7175c90c8c4bc1332899dffa6b5fa7a7b30ad2a4.tar.gz gentoo-7175c90c8c4bc1332899dffa6b5fa7a7b30ad2a4.tar.bz2 gentoo-7175c90c8c4bc1332899dffa6b5fa7a7b30ad2a4.zip |
eapi7-ver.eclass: Use lexicographic rather than arithmetic comparison.
This removes the 2**63-1 limit for integer components.
Diffstat (limited to 'eclass')
-rw-r--r-- | eclass/eapi7-ver.eclass | 36 | ||||
-rwxr-xr-x | eclass/tests/eapi7-ver.sh | 2 |
2 files changed, 27 insertions, 11 deletions
diff --git a/eclass/eapi7-ver.eclass b/eclass/eapi7-ver.eclass index 5ca8b8143af6..7eb070c68171 100644 --- a/eclass/eapi7-ver.eclass +++ b/eclass/eapi7-ver.eclass @@ -174,6 +174,28 @@ ver_rs() { echo "${comp[*]}" } +# @FUNCTION: _ver_compare_int +# @USAGE: <a> <b> +# @RETURN: 0 if <a> -eq <b>, 1 if <a> -lt <b>, 3 if <a> -gt <b> +# @INTERNAL +# @DESCRIPTION: +# Compare two non-negative integers <a> and <b>, of arbitrary length. +# If <a> is equal to, less than, or greater than <b>, return 0, 1, or 3 +# as exit status, respectively. +_ver_compare_int() { + local a=$1 b=$2 d=$(( ${#1}-${#2} )) + + # Zero-pad to equal length if necessary. + if [[ ${d} -gt 0 ]]; then + printf -v b "%0${d}d%s" 0 "${b}" + elif [[ ${d} -lt 0 ]]; then + printf -v a "%0$(( -d ))d%s" 0 "${a}" + fi + + [[ ${a} > ${b} ]] && return 3 + [[ ${a} == "${b}" ]] +} + # @FUNCTION: _ver_compare # @USAGE: <va> <vb> # @RETURN: 1 if <va> < <vb>, 2 if <va> = <vb>, 3 if <va> > <vb> @@ -200,10 +222,7 @@ _ver_compare() { # 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 + _ver_compare_int "${an%%.*}" "${bn%%.*}" || return while [[ ${an} == *.* && ${bn} == *.* ]]; do # Other components (PMS algorithm 3.3) @@ -218,8 +237,7 @@ _ver_compare() { [[ ${a} > ${b} ]] && return 3 [[ ${a} < ${b} ]] && return 1 else - [[ ${a} -gt ${b} ]] && return 3 - [[ ${a} -lt ${b} ]] && return 1 + _ver_compare_int "${a}" "${b}" || return fi done [[ ${an} == *.* ]] && return 3 @@ -237,8 +255,7 @@ _ver_compare() { 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 + _ver_compare_int "${a##*[a-z]}" "${b##*[a-z]}" || return else # Check for p first [[ ${a%%[0-9]*} == p ]] && return 3 @@ -256,8 +273,7 @@ _ver_compare() { 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 + _ver_compare_int "${ar#-r}" "${br#-r}" || return return 2 } diff --git a/eclass/tests/eapi7-ver.sh b/eclass/tests/eapi7-ver.sh index fd085a415b6e..d4aa4fdbd289 100755 --- a/eclass/tests/eapi7-ver.sh +++ b/eclass/tests/eapi7-ver.sh @@ -150,7 +150,7 @@ teqr 0 ver_test 1.010 -eq 1.01 teqr 0 ver_test 1.01 -lt 1.1 teqr 0 ver_test 1.2_pre08-r09 -eq 1.2_pre8-r9 teqr 0 ver_test 0 -lt 576460752303423488 # 2**59 -#teqr 0 ver_test 0 -lt 9223372036854775808 # 2**63 fails, integer rollover +teqr 0 ver_test 0 -lt 9223372036854775808 # 2**63 # Bad number or ordering of arguments txf ver_test 1 |