diff --git a/tren.py b/tren.py index 9824e30..797e922 100755 --- a/tren.py +++ b/tren.py @@ -9,7 +9,7 @@ BASENAME = PROGNAME.split(".py")[0] PROGENV = BASENAME.upper() INCLENV = PROGENV + "INCL" -RCSID = "$Id: tren.py,v 1.231 2010/08/26 18:50:09 tundra Exp $" +RCSID = "$Id: tren.py,v 1.232 2010/08/27 15:28:55 tundra Exp $" VERSION = RCSID.split()[2] # Copyright Information @@ -953,6 +953,7 @@ lname = "" rname = "" + newname = "" tstart = renrequest[TARGETSTART] tend = renrequest[TARGETEND] @@ -968,45 +969,53 @@ tend = len(name) if tstart or tend: - + + bound = len(name) + + # Normalize negative refs so we can use consistent + # logic below + + if tstart < 0: + tstart = bound + tstart + + if (tend != SINGLEINST and tend < 0): + tend = bound + tend + + if tstart > tend: + ErrorMsg(eBADSLICE % "%d:%d" % (renrequest[TARGETSTART], renrequest[TARGETEND])) + + # Condition and bounds check the target range as needed # Handle single position references if (tend == SINGLEINST): - # Compute bounds for positive and negative - # indicies. This is necessary because positive - # indicies are 0-based, but negative indicies - # start at -1. - - bound = len(name) - if tstart < 0: - bound += 1 - # Select the desired position. Notice that - # out-of-bounds references are ignored. This is - # so the user can specify renaming operations on - # file names of varying lengths and have them - # apply only to those files long enough to - # accommodate the request without blowing up on - # the ones that are not long enough. + # out-of-bounds references are ignored and the + # name is left untouched. This is so the user + # can specify renaming operations on file + # names of varying lengths and have them apply + # only to those files long enough to + # accommodate the request without blowing up + # on the ones that are not long enough. - if abs(tstart) < bound: + if 0 <= tstart < bound: lname, name, rname = name[:tstart], name[tstart], name[tstart+1:] + # Reference is out of bounds - leave name untouched + + else: + lname, name, rname = name, "", "" + # Handle slice range requests else: lname, name, rname = name[:tstart], name[tstart:tend], name[tend:] - # Save current state of name - - newname = name - # Handle conventional string replacement renaming requests - if renrequest[OLD] or renrequest[NEW]: + if name and (renrequest[OLD] or renrequest[NEW]): # Resolve any embedded renaming tokens