Skip to content

Commit

Permalink
performance: faster line wrap pt. 2
Browse files Browse the repository at this point in the history
  • Loading branch information
hukkin authored Nov 13, 2024
1 parent 6da0ca6 commit dee9917
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions src/mdformat/renderer/_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@
PRESERVE_CHAR = "\x00"
RE_PRESERVE_CHAR = re.compile(re.escape(PRESERVE_CHAR))

RE_UNICODE_WS_OR_WRAP_POINT = re.compile(
rf"[{re.escape(''.join(codepoints.UNICODE_WHITESPACE))}]"
"|"
rf"{re.escape(WRAP_POINT)}+"
)


def make_render_children(separator: str) -> Render:
def render_children(
Expand Down Expand Up @@ -350,29 +356,27 @@ def _wrap(text: str, *, width: int | Literal["no"]) -> str:
return wrapped


def _prepare_wrap(text: str) -> tuple[str, str]:
def _prepare_wrap(text: str) -> tuple[str, list[str]]:
"""Prepare text for wrap.
Convert `WRAP_POINT`s to spaces. Convert whitespace to
`PRESERVE_CHAR`s. Return a tuple with the prepared string, and
another string consisting of replacement characters for
`PRESERVE_CHAR`s.
`PRESERVE_CHAR`s. Return a tuple with the prepared string, and a
list consisting of replacement characters for `PRESERVE_CHAR`s.
"""
result = ""
replacements = ""
for c in text:
if c == WRAP_POINT:
if not result or result[-1] != " ":
result += " "
elif c in codepoints.UNICODE_WHITESPACE:
result += PRESERVE_CHAR
replacements += c
else:
result += c
replacements = []

def replacer(match: re.Match[str]) -> str:
first_char = match.group()[0]
if first_char == WRAP_POINT:
return " "
replacements.append(first_char)
return PRESERVE_CHAR

result = RE_UNICODE_WS_OR_WRAP_POINT.sub(replacer, text)
return result, replacements


def _recover_preserve_chars(text: str, replacements: str) -> str:
def _recover_preserve_chars(text: str, replacements: Iterable[str]) -> str:
iter_replacements = iter(replacements)
return RE_PRESERVE_CHAR.sub(lambda _: next(iter_replacements), text)

Expand Down

0 comments on commit dee9917

Please sign in to comment.