Skip to content

Commit

Permalink
Improve the name _buffer → _digit_buffer for Code128 charset C
Browse files Browse the repository at this point in the history
  • Loading branch information
maresb committed Jul 9, 2024
1 parent 0ebabf5 commit 0f9c80f
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions barcode/codex.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def __init__(self, code: str, writer=None) -> None:
self.code = code
self.writer = writer or self.default_writer()
self._charset = "B"
self._buffer = ""
self._digit_buffer = "" # Accumulate pairs of digits for charset C
check_code(self.code, self.name, code128.ALL)

def __str__(self) -> str:
Expand Down Expand Up @@ -196,9 +196,9 @@ def look_next() -> bool:
codes = self._new_charset("B")
elif char in code128.A:
codes = self._new_charset("A")
if len(self._buffer) == 1:
codes.append(self._convert(self._buffer[0]))
self._buffer = ""
if len(self._digit_buffer) == 1:
codes.append(self._convert(self._digit_buffer[0]))
self._digit_buffer = ""
elif self._charset == "B":
if look_next():
codes = self._new_charset("C")
Expand Down Expand Up @@ -240,13 +240,13 @@ def _convert_or_buffer(self, char: str) -> int | None:
if char in code128.C:
return code128.C[char]
if char.isdigit():
self._buffer += char
if len(self._buffer) == 1:
self._digit_buffer += char
if len(self._digit_buffer) == 1:
# Wait for the second digit to group in pairs
return None
assert len(self._buffer) == 2
value = int(self._buffer)
self._buffer = ""
assert len(self._digit_buffer) == 2
value = int(self._digit_buffer)
self._digit_buffer = ""
return value
raise RuntimeError(f"Character {char} could not be converted in charset C.")

Expand All @@ -269,10 +269,10 @@ def _build(self) -> list[int]:
if code_num is not None:
encoded.append(code_num)
# Finally look in the buffer
if len(self._buffer) == 1:
if len(self._digit_buffer) == 1:
encoded.extend(self._new_charset("B"))
encoded.append(self._convert(self._buffer[0]))
self._buffer = ""
encoded.append(self._convert(self._digit_buffer[0]))
self._digit_buffer = ""
return self._try_to_optimize(encoded)

def build(self) -> list[str]:
Expand Down

0 comments on commit 0f9c80f

Please sign in to comment.