From 9d78b2ce50f96d6055375aae4471a7f662ba83f9 Mon Sep 17 00:00:00 2001 From: 5j9 <5j9@users.noreply.github.com> Date: Fri, 19 Jul 2024 12:15:10 +0330 Subject: [PATCH] chore(_ext_link_shadow): do not create memoryview of whole string To same memory in rare cases that a lot of external_links are created. --- wikitextparser/_wikitext.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/wikitextparser/_wikitext.py b/wikitextparser/_wikitext.py index 843ae42..ba3059e 100644 --- a/wikitextparser/_wikitext.py +++ b/wikitextparser/_wikitext.py @@ -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']: