blob: 954990dcfb0ae995e9d8d782d652606d78976002 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
#
# is-at-least: compare two version strings
#
# Usage: is-at-least minimum_version [current_version]
#
# Returns true if current_version is equal to or newer than minimum_version.
# If current_version is omitted, $ZSH_VERSION is used.
# {minimum,current}_version is a string consisting of a few 'segments'
# joined by either '.' or '-'. For example:
# 1.2-test 3.1.2-zefram4
# 4.4.5beta23 12.34-patch23a
#
# A segment with no digits, such as 'test' above, is ignored.
#
# Two segments are compared numerically if the first differing characters
# are both digits, and lexically otherwise.
#
# If number of segments in the two version strings differ, missing
# segments are assumed to be '0'. This means
# 5.9, 5.9.0, 5.9.0.0, 5.9-test
# are all considered to be the same version.
#
emulate -L zsh
setopt extended_glob
local IFS=".-" min_ver cur_ver n order
: ${2=$ZSH_VERSION}
# sort out the easy corner cases first
[[ -z $1 || -z $2 ]] && return 1 # no version
[[ $1 = $2 ]] && return 0 # same version
# split into segments
min_ver=(${=1})
cur_ver=(${=2})
# remove segments containing no digits
min_ver=( ${min_ver:#[^0-9]##} )
cur_ver=( ${cur_ver:#[^0-9]##} )
(( $#min_ver == 0 || $#cur_ver == 0 )) && return 1
# find the first segment that differs
for (( n = 1; n <= $#min_ver; ++n )) do
[[ ${min_ver[n]} != ${cur_ver[n]-0} ]] && break
done
# if all segments in $min_ver are equal to those in $cur_ver
(( n > $#min_ver )) && return 0
# now compare the two segments that differ
order=( ${cur_ver[n]-0} ${min_ver[n]-0} )
[[ $order != ${${(On)order}} ]] && return 1 || return 0
|