Skip to content

Commit c4f3a02

Browse files
committed
makeqstrdata: permit longer "compressed" outputs
It is possible for this routine to expand some inputs, and in fact it does for certan strings in the proposed Korean translation of CircuitPython (#1858). I did not determine what the maximum expansion is -- it's probably modest, like len()/7+2 bytes or something -- so I tried to just make enc[] an adequate over-allocation, and then ensured that all the strings in the proposed ko.po now worked. The worst actual expansion seems to be a string that goes from 65 UTF-8-encoded bytes to 68 compressed bytes (+4.6%). Only a few out of all strings are reported as non-compressed.
1 parent 95d2694 commit c4f3a02

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

py/makeqstrdata.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def compress(encoding_table, decompressed):
180180
if not isinstance(decompressed, bytes):
181181
raise TypeError()
182182
values, lengths = encoding_table
183-
enc = bytearray(len(decompressed))
183+
enc = bytearray(len(decompressed) * 2)
184184
#print(decompressed)
185185
#print(lengths)
186186
current_bit = 7
@@ -227,6 +227,8 @@ def compress(encoding_table, decompressed):
227227
current_bit -= 1
228228
if current_bit != 7:
229229
current_byte += 1
230+
if current_byte > len(decompressed):
231+
print("Note: compression increased length", repr(decompressed.decode('utf-8')), len(decompressed), current_byte, file=sys.stderr)
230232
return enc[:current_byte]
231233

232234
def qstr_escape(qst):

0 commit comments

Comments
 (0)