diff --git a/tren.py b/tren.py index 845c1b4..ed22a1c 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.213 2010/03/29 20:37:26 tundra Exp $" +RCSID = "$Id: tren.py,v 1.214 2010/03/30 21:02:35 tundra Exp $" VERSION = RCSID.split()[2] # Copyright Information @@ -1479,13 +1479,8 @@ rolling over the count. If 'fmt' is null on entry, the final result length is unlimited. """ - - - fmtlen = len(fmt) + base = len(alphabet) - newval = "" - carry = None - # Do position-wise "addition" via symbol substitution moving from # right-to-left adjusting for the fact that not all symbols in the @@ -1513,9 +1508,16 @@ # may be a different length than the increment string and that not # all characters in the format pattern are guaranteed to exist in # the alphabet. - + + newval = "" + carry = None + fmtlen = len(fmt) + incrlen = len(incr) + calcsize = max(fmtlen, incrlen) + i = -1 - while abs(i) <= len(incr): + done = False + while abs(i) <= calcsize and not done: sum = 0 @@ -1525,7 +1527,8 @@ if fmt and (abs(i) <= fmtlen) and fmt[i] in alphabet: sum += alphabet.index(fmt[i]) - sum += alphabet.index(incr[i]) + if abs(i) <= incrlen: + sum += alphabet.index(incr[i]) # Do arithmetic modulo alphabet length @@ -1534,26 +1537,21 @@ if not carry: carry = None + # We're completely done if we're out of digits in incr and + # there's no carry to propagate. This prevents us from + # tacking on leading 0th characters which could overwrite + # out-of-alphabet characters in the format field. + + if abs(i-1) > incrlen: + done =True + newval = alphabet[digit] + newval i -= 1 if carry: newval = alphabet[carry] + newval - - # Suppress leading alphabet[0] symbols so we don't potentially - # overwrite non-alphabet symbols in the format string. If the - # user wants these leading alphabet[0] symbols, they can specify - # them in their format string. - - # Never clear out a lone Oth character though - - if newval != alphabet[0]: - while newval and newval[0] == alphabet[0]: - newval = newval[1:] - - # Constrain the results to the length of the original format # string, rolling over and warning the user as necessary. The one # exception to this is when a null format string is passed. This