Skip to content

Commit

Permalink
Ok I lied, seperate out comment map func, use string IO
Browse files Browse the repository at this point in the history
  • Loading branch information
Lilaa3 committed Sep 27, 2024
1 parent 2d76dce commit cc2ea7f
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions fast64_internal/sm64/sm64_utility.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import NamedTuple, Optional
from pathlib import Path
from io import StringIO
import os
import re

Expand Down Expand Up @@ -161,6 +162,23 @@ def find_descriptor_in_text(value: ModifyFoundDescriptor, commentless: str, comm
return matches


def get_comment_map(text: str):
comment_map = []
commentless, last_pos, pos = StringIO(), 0, 0
for match in re.finditer(COMMENT_PATTERN, text):
pos += commentless.write(text[last_pos : match.start()]) # add text before comment
string = match.group(0)
if string.startswith("/"): # actual comment
comment_map.append((pos, len(string) - 1))
pos += commentless.write(" ")
else: # stuff like strings
pos += commentless.write(string)
last_pos = match.end()

commentless.write(text[last_pos:]) # add any remaining text after the last match
return commentless.getvalue(), comment_map


def find_descriptors(
text: str,
descriptors: list[ModifyFoundDescriptor],
Expand All @@ -171,24 +189,10 @@ def find_descriptors(
ignore_comments=True,
):
"""Returns: The found matches from descriptors, the footer pos (the end of the text if none)"""
comment_map = []
if ignore_comments:
commentless = ""
last_pos = 0

for match in re.finditer(COMMENT_PATTERN, text):
commentless += text[last_pos : match.start()] # add text before comment
string = match.group(0)
if string.startswith("/"):
comment_map.append((len(commentless), len(string) - 1))
commentless += " "
else:
commentless += string
last_pos = match.end()

commentless += text[last_pos:] # add any remaining text after the last match
commentless, comment_map = get_comment_map(text)
else:
commentless = text
commentless, comment_map = text, []

header_matches = find_descriptor_in_text(header, commentless, comment_map) if header is not None else []
footer_matches = find_descriptor_in_text(footer, commentless, comment_map) if footer is not None else []
Expand Down

0 comments on commit cc2ea7f

Please sign in to comment.