add in delete_ functions, make index parameters able to take a character as well...
authorCiaran McCreesh <ciaranm@gentoo.org>
Sat, 1 Jan 2005 22:58:14 +0000 (22:58 +0000)
committerCiaran McCreesh <ciaranm@gentoo.org>
Sat, 1 Jan 2005 22:58:14 +0000 (22:58 +0000)
eclass/versionator.eclass

index a1a3d70f5e71e30ddd77c72c502ae1e659ebc010..7210c89bf6b3754c9c849ac4e75bddd8703258bc 100644 (file)
@@ -1,6 +1,6 @@
 # Copyright 1999-2004 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
-# $Header: /var/cvsroot/gentoo-x86/eclass/versionator.eclass,v 1.2 2004/09/10 21:42:21 ciaranm Exp $
+# $Header: /var/cvsroot/gentoo-x86/eclass/versionator.eclass,v 1.3 2005/01/01 22:58:14 ciaranm Exp $
 #
 # Original Author: Ciaran McCreesh <ciaranm@gentoo.org>
 #
 #     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
+#
+# 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
+# selected.
 #
 # There's also:
 #     version_is_at_least             want      have
-# but it's *really* experimental...
+# but it doesn't work in all cases, so only use it if you know what you're
+# doing.
 
 ECLASS=versionator
 INHERITED="$INHERITED $ECLASS"
@@ -155,18 +162,31 @@ get_after_major_version() {
 #     1 '_' 1.2.3       -> 1_2.3
 #     2 '_' 1.2.3       -> 1.2_3
 #     1 '_' 1b-2.3      -> 1b_2.3
+# Rather than being a number, $1 can be a separator character such as '-', '.'
+# or '_'. In this case, the first separator of this kind is selected.
 replace_version_separator() {
-       local i c found=0 v="${3:-${PV}}"
+       local w i c found=0 v="${3:-${PV}}"
+       w=${1:-1}
        c=( $(get_all_version_components ${v} ) )
-       for (( i = 0 ; i < ${#c[@]} ; i = $i + 1 )) ; do
-               if [[ -n "${c[${i}]//[^-._]}" ]] ; then
-                       found=$(($found + 1))
-                       if [[ "$found" == "${1:-1}" ]] ; then
+       if [[ "${w//[[:digit:]]/}" == "${w}" ]] ; then
+               # it's a character, not an index
+               for (( i = 0 ; i < ${#c[@]} ; i = $i + 1 )) ; do
+                       if [[ "${c[${i}]}" == "${w}" ]] ; then
                                c[${i}]="${2}"
                                break
                        fi
-               fi
-       done
+               done
+       else
+               for (( i = 0 ; i < ${#c[@]} ; i = $i + 1 )) ; do
+                       if [[ -n "${c[${i}]//[^-._]}" ]] ; then
+                               found=$(($found + 1))
+                               if [[ "$found" == "${w}" ]] ; then
+                                       c[${i}]="${2}"
+                                       break
+                               fi
+                       fi
+               done
+       fi
        c=${c[@]}
        echo ${c// }
 }
@@ -180,6 +200,23 @@ replace_all_version_separators() {
        echo ${c// }
 }
 
+# Delete the $1th separator in $3 (defaults to $PV if $3 is not supplied). If
+# there are fewer than $1 separators, don't change anything.
+#     1 1.2.3       -> 12.3
+#     2 1.2.3       -> 1.23
+#     1 1b-2.3      -> 1b2.3
+# Rather than being a number, $1 can be a separator character such as '-', '.'
+# or '_'. In this case, the first separator of this kind is deleted.
+delete_version_separator() {
+       replace_version_separator "${1}" "" "${2}"
+}
+
+# Delete all version separators in $1 (defaults to $PV).
+#     '_' 1b.2.3        -> 1b_2_3
+delete_all_version_separators() {
+       replace_all_version_separators "${1}"
+}
+
 # Is $2 (defaults to $PVR) at least version $1? Intended for use in eclasses
 # only. Not very reliable, doesn't understand most things, make sure you test
 # reaaaallly well before using this. Prod ciaranm if you need it to support more