Skip to content

Commit

Permalink
Fix CRLF optimal sequence bug
Browse files Browse the repository at this point in the history
See #4.

I thought there'd be a more elegant fix here, but in the end I went with the
same one as inhttps://github.com/delimitry/pull/8/files#diff-bf7c11e2b0865687a2785c4fa2a55920d92137aa2b6548c5f87835d98f73339fR356-R357
  • Loading branch information
dlenski authored and dlenskiSB committed Dec 17, 2024
1 parent 1001836 commit a7fe98f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
7 changes: 4 additions & 3 deletions aztec_code_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,10 @@ def find_optimal_sequence(data, encoding=None):
if last_mode == Mode.PUNCT:
last_c = cur_seq[x][-1]
if isinstance(last_c, int) and bytes((last_c, c)) in punct_2_chars:
if cur_len[x] < next_len[x]:
next_len[x] = cur_len[x]
next_seq[x] = cur_seq[x][:-1] + [ bytes((last_c, c)) ]
if x != Mode.MIXED: # we need to avoid this because it contains '\r', '\n' individually, but not combined
if cur_len[x] < next_len[x]:
next_len[x] = cur_len[x]
next_seq[x] = cur_seq[x][:-1] + [ bytes((last_c, c)) ]
if len(next_seq[Mode.BINARY]) - 2 == 32:
next_len[Mode.BINARY] += 11
cur_len = next_len.copy()
Expand Down
9 changes: 4 additions & 5 deletions test_aztec_code_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,20 +119,19 @@ def test_find_optimal_sequence_bytes(self):
self.assertEqual(find_optimal_sequence(b'@\xff'), b(Shift.BINARY, 2, '@', '\xff'))
self.assertEqual(find_optimal_sequence(b'. @\xff'), b(Shift.PUNCT, '. ', Shift.BINARY, 2, '@', '\xff'))

@unittest.expectedFailure
def test_find_optimal_sequence_CRLF_bug(self):
""" Demonstrate a known bug in find_optimal_sequence.
""" Demonstrate a known bug in find_optimal_sequence (https://github.com/dlenski/aztec_code_generator/pull/4)
This is a much more minimal example of https://github.com/delimitry/aztec_code_generator/issues/7
The string '\t<\r\n':
SHOULD be sequenced as: Latch.MIXED '\t' < '\r' '\n'
but is incorrectly sequenced as: Latch.MIXED '\t' < '\r\n'
SHOULD be sequenced as: Latch.MIXED '\t' Latch.PUNCT < '\r' '\n'
but is incorrectly sequenced as: Latch.MIXED '\t' Shift.PUNCT < '\r\n'
... which is impossible since no encoding of the 2 byte sequence b'\r\n' exists in MIXED mode. """

self.assertEqual(find_optimal_sequence(b'\t<\r\n'), b(
Latch.MIXED, '\t', Shift.PUNCT, '<', '\r', '\n'
Latch.MIXED, '\t', Latch.PUNCT, '<', '\r\n'
))

def test_optimal_sequence_to_bits(self):
Expand Down

0 comments on commit a7fe98f

Please sign in to comment.