# [reportlab-users] code128.py

Klaas Feenstra klaas at feenstra.es
Mon Jan 4 06:35:22 EST 2016

```Hello,

After using reportlab for creating barcodes, I realised the optimization
for Code128 was not correct. It was only optimizing the end of the barcode
string.

I made modifications for the trailingDigitsToC function. See below. Now all
string will be analysed for optimizations. Probably there will be a better
algorithm, but this is working.

def _trailingDigitsToC(self, l):
# Optimization: trailing digits -> set C double-digits

i = 0
nl = []
while i < len(l):
startpos = i
rl = ['TO_C']
savings = -1 # the TO_C costs one character
while i < len(l):
if l[i] in (['START_B','TO_A','TO_B']):
set = 'TO' + l[i][-2:]
break
elif l[i] == '\xf1':
rl.append(l[i])
i += 1
continue
elif len(l[i]) == 1 and l[i] in digits \
and len(l[i+1]) == 1 and l[i+1] in digits:
rl.append(l[i] + l[i+1])
i += 2
savings += 1
continue
else:
break

if savings > 0:
nl = nl + rl
if not l[i]=='STOP':
nl.append(set)
nl.append(l[i])
else:
nl = nl + l[startpos:i+1]
i += 1
return nl
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://pairlist2.pair.net/pipermail/reportlab-users/attachments/20160104/e04f6b2c/attachment.html>
```