Skip to content

Commit

Permalink
perf: using dictionaries instead of match case when it makes sense
Browse files Browse the repository at this point in the history
  • Loading branch information
AlphaJack committed Nov 1, 2024
1 parent 74ea4d6 commit 1430d28
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions toc/toc.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,18 +616,18 @@ def _process_latex(self, lines: list) -> list:
_pattern = re.compile(
r"\\(chapter|(?:sub){0,}section|(?:sub){0,}paragraph){(.*?)}"
)
_levels = {
"chapter": 1,
"section": 2,
"subsection": 3,
"subsubsection": 4,
"paragraph": 5,
"subparagraph": 6,
}
for n, line in enumerate(lines):
_match = _pattern.match(line)
if _match:
_heading_levels = {
"chapter": 1,
"section": 2,
"subsection": 3,
"subsubsection": 4,
"paragraph": 5,
"subparagraph": 6,
}
_heading_level = _heading_levels.get(_match.group(1), 1)
_heading_level = _levels.get(_match.group(1), 1)
_heading_text = (
f"{_match.group(2)} {n+1}" if self.lineNumbers else _match.group(2)
)
Expand All @@ -650,7 +650,7 @@ def _process_restructuredtext(self, data: str) -> list:
for _match in _pattern.finditer(data):
_heading_text = _match.group(1)
_symbol = _match.group(2)[:1]
_heading_level = _levels[_symbol]
_heading_level = _levels.get(_symbol, 1)
if self.lineNumbers:
# start counting from _heading_text, not optional overline
_untilCurrentMatch = _match.start(1)
Expand All @@ -667,17 +667,11 @@ def _process_man(self, lines: list) -> list:
# parse perl files, reusing headings
_newtoc = []
_pattern = re.compile(r'^\.(T[Hh]|S[HhSs]) "?(\w+?(?:\s\w+?)*)"?(\s|$)')
_levels = {"TH": 1, "Th": 1, "SH": 2, "Sh": 2, "SS": 3, "Ss": 3}
for n, line in enumerate(lines):
_match = _pattern.match(line)
if _match:
_heading_level = 1
match _match.group(1):
case "TH" | "Th":
_heading_level = 1
case "SH" | "Sh":
_heading_level = 2
case "SS" | "Ss":
_heading_level = 3
_heading_level = _levels.get(_match.group(1), 1)
_heading_text = (
f"{_match.group(2)} {n+1}" if self.lineNumbers else _match.group(2)
)
Expand Down

0 comments on commit 1430d28

Please sign in to comment.