public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-commits] repo/gentoo:eapi7-ver commit in: eclass/tests/, eclass/
@ 2017-09-21  7:04 Michał Górny
  0 siblings, 0 replies; 2+ messages in thread
From: Michał Górny @ 2017-09-21  7:04 UTC (permalink / raw
  To: gentoo-commits

commit:     99cc42fbfa03d6c79fa003b8f490f99e29615f8b
Author:     Michał Górny <mgorny <AT> gentoo <DOT> org>
AuthorDate: Thu Sep 21 06:57:19 2017 +0000
Commit:     Michał Górny <mgorny <AT> gentoo <DOT> org>
CommitDate: Thu Sep 21 07:04:28 2017 +0000
URL:        https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=99cc42fb

eapi7-ver.eclass: Verify input for ver_test

 eclass/eapi7-ver.eclass   | 78 +++++++++++++++++++++++++++++++++++++++++++++++
 eclass/tests/eapi7-ver.sh | 18 +++++------
 2 files changed, 87 insertions(+), 9 deletions(-)

diff --git a/eclass/eapi7-ver.eclass b/eclass/eapi7-ver.eclass
index e833835a56e..94184c484b0 100644
--- a/eclass/eapi7-ver.eclass
+++ b/eclass/eapi7-ver.eclass
@@ -176,6 +176,81 @@ ver_rs() {
 	echo "${comp[*]}"
 }
 
+# @FUNCTION: _ver_validate
+# @USAGE: <comp[0]>...
+# @DESCRIPTION:
+# Verify that the version component array passed as the argument
+# validates according to the PMS version rules. Returns 0 if it does,
+# 1 otherwise.
+_ver_validate() {
+	local prev=start
+
+	while [[ ${1} || ${2} ]]; do
+		local s=${1}
+		local c=${2}
+
+		if [[ -z ${s} ]]; then
+			if [[ ${c} == [0-9]* ]]; then
+				# number without preceding sep may be either:
+				case ${prev} in
+					# a. 1st version number
+					start) prev=numeric;;
+					# b. _foo suffix number
+					suffix) prev=suffix_num;;
+					# c. -rN revision number
+					revision) prev=revision_num;;
+					*) return 1;;
+				esac
+			elif [[ -n ${c} ]]; then
+				# letter without preceding sep = letter after version
+				[[ ${prev} == numeric ]] || return 1
+				[[ ${#c} -eq 1 ]] || return 1
+				prev=letter
+			fi
+		elif [[ -z ${c} ]]; then
+			# trailing suffix?
+			return 1
+		elif [[ ${s} == . ]]; then
+			# number preceded by dot = numeric component
+			[[ ${prev} == numeric ]] || return 1
+		elif [[ ${s} == _ ]]; then
+			# _ implies _foo suffix
+			case ${prev} in
+				numeric|letter|suffix|suffix_num) ;;
+				*) return 1;;
+			esac
+
+			case ${c} in
+				alpha) ;;
+				beta) ;;
+				rc) ;;
+				pre) ;;
+				p) ;;
+				*) return 1;;
+			esac
+			prev=suffix
+		elif [[ ${s} == - ]]; then
+			# - implies revision
+			case ${prev} in
+				numeric|letter|suffix|suffix_num) ;;
+				*) return 1;;
+			esac
+
+			[[ ${c} == r ]] || return 1
+			prev=revision
+		else
+			return 1
+		fi
+
+		shift 2
+	done
+
+	# empty version string?
+	[[ ${prev} != start ]] || return 1
+
+	return 0
+}
+
 # @FUNCTION: ver_test
 # @USAGE: [<v1>] <op> <v2>
 # @DESCRIPTION:
@@ -216,6 +291,9 @@ ver_test() {
 	compb=( "${comp[@]}" )
 	_ver_split "${va}"
 
+	_ver_validate "${comp[@]}" || die "${FUNCNAME}: invalid version: ${va}"
+	_ver_validate "${compb[@]}" || die "${FUNCNAME}: invalid version: ${vb}"
+
 	local i sa sb ca cb wa wb result=0
 	for (( i = 0;; i += 2 )); do
 		sa=${comp[i]}

diff --git a/eclass/tests/eapi7-ver.sh b/eclass/tests/eapi7-ver.sh
index 1ad99a246e1..144bb2bddc3 100755
--- a/eclass/tests/eapi7-ver.sh
+++ b/eclass/tests/eapi7-ver.sh
@@ -161,12 +161,12 @@ txf ver_test 1 lt 2
 txf ver_test 1 -foo 2
 
 # Malformed versions
-#txf ver_test "" -ne 1
-#txf ver_test 1. -ne 1
-#txf ver_test 1ab -ne 1
-#txf ver_test b -ne 1
-#txf ver_test 1-r1_pre -ne 1
-#txf ver_test 1-pre1 -ne 1
-#txf ver_test 1_foo -ne 1
-#txf ver_test 1_pre1.1 -ne 1
-#txf ver_test 1-r1.0 -ne 1
+txf ver_test "" -ne 1
+txf ver_test 1. -ne 1
+txf ver_test 1ab -ne 1
+txf ver_test b -ne 1
+txf ver_test 1-r1_pre -ne 1
+txf ver_test 1-pre1 -ne 1
+txf ver_test 1_foo -ne 1
+txf ver_test 1_pre1.1 -ne 1
+txf ver_test 1-r1.0 -ne 1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [gentoo-commits] repo/gentoo:eapi7-ver commit in: eclass/tests/, eclass/
@ 2017-09-23 10:43 Ulrich Müller
  0 siblings, 0 replies; 2+ messages in thread
From: Ulrich Müller @ 2017-09-23 10:43 UTC (permalink / raw
  To: gentoo-commits

commit:     7637f7476023183cdad69a7b28216e65eb4ad35a
Author:     Ulrich Müller <ulm <AT> gentoo <DOT> org>
AuthorDate: Sat Sep 23 08:58:43 2017 +0000
Commit:     Ulrich Müller <ulm <AT> gentoo <DOT> org>
CommitDate: Sat Sep 23 08:58:43 2017 +0000
URL:        https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=7637f747

eapi7-ver.eclass: Use lexicographic rather than arithmetic comparison.

This removes the 2**63-1 limit for integer components.

 eclass/eapi7-ver.eclass   | 36 ++++++++++++++++++++++++++----------
 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 5ca8b8143af..7eb070c6817 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 7f0ae880767..1bd0bd1d81a 100755
--- a/eclass/tests/eapi7-ver.sh
+++ b/eclass/tests/eapi7-ver.sh
@@ -152,7 +152,7 @@ teqr 0 ver_test 1.2_pre08-r09 -eq 1.2_pre8-r9
 teqr 0 ver_test 0 -lt 2147483648 # 2**31
 teqr 0 ver_test 0 -lt 4294967296 # 2**32
 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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2017-09-23 10:43 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-09-21  7:04 [gentoo-commits] repo/gentoo:eapi7-ver commit in: eclass/tests/, eclass/ Michał Górny
  -- strict thread matches above, loose matches on Subject: below --
2017-09-23 10:43 Ulrich Müller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox