Skip to content

Stop dropping a leading "99" pair from Code128 barcodes (#251)#263

Open
gaoflow wants to merge 1 commit into
WhyNotHugo:mainfrom
gaoflow:fix-code128-leading-99
Open

Stop dropping a leading "99" pair from Code128 barcodes (#251)#263
gaoflow wants to merge 1 commit into
WhyNotHugo:mainfrom
gaoflow:fix-code128-leading-99

Conversation

@gaoflow

@gaoflow gaoflow commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Fixes #251.

A Code128 barcode whose data begins with 99 silently loses that pair:

from barcode.codex import Code128
Code128("9912345678").encoded
# [105, 12, 34, 56, 78]   -> decodes to "12345678", the leading 99 is gone
# correct: [105, 99, 12, 34, 56, 78]

Cause. In charset C the digit pair "99" encodes to the code number 99, which is also the TO_C charset-switch marker (code128.TO = {101: START_A, 100: START_B, 99: START_C}). Code128._try_to_optimize folds a START_C immediately followed by a switch code into a single start code, but it tested encoded[1] in code128.TO unconditionally, so a leading data pair of 99 was mistaken for a redundant switch and dropped.

Fix. Restrict the fold to a genuine START_C followed by a switch to charset A or B (TO_A/TO_B). The data value 99 is preserved, and the legitimate optimization still applies — e.g. "Wikipedia", which starts in charset B, still folds to a single START_B.

Tests (tests/test_builds.py): leading "99" now round-trips; non-colliding leading pairs ("00…", "12…") are unchanged; and the start-code folding optimization is asserted to still apply. Full suite: 50 passed.

In charset C the digit pair "99" encodes to the code number 99, which
is also the TO_C charset-switch marker. Code128._try_to_optimize folded
away a leading switch code, so a barcode whose data began with "99" had
that pair silently dropped: Code128("9912345678").encoded produced
[105, 12, 34, 56, 78], decoding to "12345678".

Restrict the START-code folding to a genuine START_C followed by a
switch to charset A or B, so the data value 99 is preserved while the
legitimate optimization (e.g. "Wikipedia" starting in charset B) still
applies. Fixes WhyNotHugo#251.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Code128 issue, codes starting with 99 will have that stripped from finished barcode

1 participant