Corrected ComputeSeqString() to properly count and roll over no matter whether fmt or incr is longer.
ComputeSeqString() now also properly preserves out-of-alphabet characters in the fmt string unless a carry propagates into that position.
Reorganized ComputeSeqString() slightly.
1 parent 215f127 commit ecd2df0c4113a0df899be378cab6929067896d50
@tundra tundra authored on 30 Mar 2010
Showing 1 changed file
View
69
tren.py
 
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
 
result longer than fmt has MSD dropped, thereby effectively
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
# format string will be in the alphabet.
# right-to-left. Take into account that the format pattern string
# 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
 
if carry:
 
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
carry, digit = sum/base, sum % base
 
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
# is understood to mean that sequences of any length are