diff --git a/tren.py b/tren.py index f51ebcb..c9f1d80 100755 --- a/tren.py +++ b/tren.py @@ -8,7 +8,7 @@ PROGNAME = "tren.py" BASENAME = PROGNAME.split(".py")[0] PROGENV = BASENAME.upper() -RCSID = "$Id: tren.py,v 1.140 2010/02/03 23:56:38 tundra Exp $" +RCSID = "$Id: tren.py,v 1.141 2010/02/04 19:55:27 tundra Exp $" VERSION = RCSID.split()[2] # Copyright Information @@ -448,12 +448,30 @@ # Handle global vs. 1st occurence replacement - if renrequest[GLOBAL]: - newname = newname.replace(old, new) + oldstrings = [] - else: - i = newname.find(old) - if i >= 0: + # Build a list of indexes to every occurence of the old string + + i = newname.find(old) + while i >= 0: + + oldstrings.append(i) + + i = newname.find(old, i + len(old)) + + if oldstrings: + + # Only process leftmost occurence if global replace is off + + if not renrequest[GLOBAL]: + oldstrings = [oldstrings[0],] + + # Replace selected substring(s). + # Substitute from R->L in original string + # so as not to mess up the replacement indicies. + + oldstrings.reverse() + for i in oldstrings: newname = newname[:i] + new + newname[i + len(old):] print ColumnPad([target, pathname + newname], padwidth = 50)