There have been a few requests for negative indices in versionator, where -1 would mean "the last component". This conflicts with the range syntax, however, so the meaning of things like -2 would be inconsistent between functions.
However... Getting the last index seems to be something that's needed often enough to warrant a proper solution. The attached implements get_version_component_count and get_last_version_component_index. Thoughts? -- Ciaran McCreesh : Gentoo Developer (Wearer of the shiny hat) Mail : ciaranm at gentoo.org Web : http://dev.gentoo.org/~ciaranm
Index: versionator.eclass =================================================================== RCS file: /var/cvsroot/gentoo-x86/eclass/versionator.eclass,v retrieving revision 1.9 diff -u -r1.9 versionator.eclass --- versionator.eclass 6 Jul 2005 20:23:20 -0000 1.9 +++ versionator.eclass 12 Mar 2006 16:40:08 -0000 @@ -16,15 +16,17 @@ # # Full list of user usable functions provided by this eclass (see the functions # themselves for documentation): -# get_all_version_components ver_str -# get_version_components ver_str -# get_major_version ver_str -# get_version_component_range range ver_str -# get_after_major_version ver_str -# replace_version_separator index newvalue ver_str -# replace_all_version_separators newvalue ver_str -# delete_version_separator index ver_str -# delete_all_version_separators ver_str +# get_all_version_components ver_str +# get_version_components ver_str +# get_major_version ver_str +# get_version_component_range range ver_str +# get_after_major_version ver_str +# replace_version_separator index newvalue ver_str +# replace_all_version_separators newvalue ver_str +# delete_version_separator index ver_str +# delete_all_version_separators ver_str +# get_version_component_count ver_str +# get_last_version_component_index ver_str # # Rather than being a number, the index parameter can be a separator character # such as '-', '.' or '_'. In this case, the first separator of this kind is @@ -34,7 +36,6 @@ # version_is_at_least want have # which may be buggy, so use with caution. - shopt -s extglob # Split up a version string into its component parts. If no parameter is @@ -214,6 +215,25 @@ replace_all_version_separators "" "${1}" } +# How many version components are there in $1 (defaults to $PV)? +# 1.0.1 -> 3 +# 3.0c-r1 -> 4 +# +get_version_component_count() { + local a + a=( $(get_version_components "${1:-${PV}}" ) ) + echo [EMAIL PROTECTED] +} + +# What is the index of the last version component in $1 (defaults to $PV)? +# Equivalent to get_version_component_count - 1. +# 1.0.1 -> 3 +# 3.0c-r1 -> 4 +# +get_last_version_component_index() { + echo $(( $(get_version_component_count "${1:-${PV}}" ) - 1 )) +} + # Is $2 (defaults to $PVR) at least version $1? Intended for use in eclasses # only. May not be reliable, be sure to do very careful testing before actually # using this. Prod ciaranm if you find something it can't handle.
signature.asc
Description: PGP signature