Skip to content

Commit

Permalink
Fix tokenized format parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
kg583 committed Nov 20, 2024
1 parent 3bc0261 commit 4bc96d8
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
1 change: 1 addition & 0 deletions tests/tivars.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ def test_load_from_string(self):

test_program.load_string(string := "setDate(1")
self.assertEqual(test_program.string(), string)
self.assertEqual(f"{test_program:02d: }", f"00: {string}")

# Version is wrong(?)
test_program.version = 0x04
Expand Down
10 changes: 4 additions & 6 deletions tivars/types/tokenized.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,9 @@ class TokenizedEntry(SizedEntry):

def __format__(self, format_spec: str) -> str:
try:
if "." not in format_spec:
format_spec += ".en"

lines, conv, sep, spec, lang = re.match(r"(.*?)(\w?)(\W*)?(\w?)\.(\w+)", format_spec).groups()
line_number = f"{{index:{lines}{conv or 'd'}}}{sep}" if conv or lines else sep
lines, sep, spec, lang = re.match(r"(.*?[a-z%#])?(\W*)(\w?)\.?(\w+)?", format_spec).groups()
line_number = f"{{index:{lines}}}{sep}" if lines else sep
lang = lang or "en"

match spec:
case "" | "d":
Expand All @@ -61,7 +59,7 @@ def __format__(self, format_spec: str) -> str:

return "\n".join(line_number.format(index=index) + line for index, line in enumerate(string.split("\n")))

except (AttributeError, KeyError, ValueError, TypeError):
except (AttributeError, KeyError, TypeError, ValueError):
return super().__format__(format_spec)

@staticmethod
Expand Down

0 comments on commit 4bc96d8

Please sign in to comment.