Skip to content

Commit

Permalink
chore(_ext_link_shadow): do not create memoryview of whole string
Browse files Browse the repository at this point in the history
To same memory in rare cases that a lot of external_links are
created.
  • Loading branch information
5j9 committed Jul 19, 2024
1 parent b3bd6e3 commit 9d78b2c
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions wikitextparser/_wikitext.py
Original file line number Diff line number Diff line change
Expand Up @@ -1190,23 +1190,25 @@ def get_italics(self, recursive=True) -> List['Italic']:
)

@property
def _ext_link_shadow(self) -> memoryview:
def _ext_link_shadow(self) -> bytearray:
"""Replace the invalid chars of SPAN_PARSER_TYPES with b'_'.
For comments, all characters are replaced, but for ('Template',
'ParserFunction', 'Parameter') only invalid characters are replaced.
"""
ss, se, _, _ = self._span_data
byte_array = bytearray(self._lststr[0], 'ascii', 'replace')
byte_array = bytearray(self._lststr[0][ss:se], 'ascii', 'replace')
subspans = self._subspans
for s, e, _, _ in subspans('Comment'):
byte_array[s:e] = (e - s) * b'_'
byte_array[s - ss : e - ss] = (e - s) * b'_'
for s, e, _, _ in subspans('WikiLink'):
byte_array[s:e] = (e - s) * b' '
byte_array[s - ss : e - ss] = (e - s) * b' '
for type_ in 'Template', 'ParserFunction', 'Parameter':
for s, e, _, _ in subspans(type_):
byte_array[s:e] = INVALID_EXT_CHARS_SUB(b' ', byte_array[s:e])
return memoryview(byte_array)[ss:se]
byte_array[s - ss : e - ss] = INVALID_EXT_CHARS_SUB(
b' ', byte_array[s:e]
)
return byte_array

@property
def external_links(self) -> List['ExternalLink']:
Expand Down

0 comments on commit 9d78b2c

Please sign in to comment.