jenkins-bot has submitted this change. (
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1340528?usp=email )
Change subject: Reuse the match start position in replaceExcept
......................................................................
Reuse the match start position in replaceExcept
Read the start position once per match and reuse it for exception
checking, text slicing, search advancement and marker placement.
Avoid three or four start() calls per successful replacement.
Change-Id: I22598cb155eceee0ad1d49e7c41d1de4b0beabba
---
M pywikibot/textlib.py
1 file changed, 7 insertions(+), 5 deletions(-)
Approvals:
Mahveotm: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/textlib.py b/pywikibot/textlib.py
index 14ac844..c4f9343 100644
--- a/pywikibot/textlib.py
+++ b/pywikibot/textlib.py
@@ -431,6 +431,8 @@
# nothing left to replace
break
+ match_start = match.start()
+
# check which exception will occur next.
nextExceptionMatch = None
for dontTouchR in dontTouchRegexes:
@@ -441,7 +443,7 @@
nextExceptionMatch = excMatch
if nextExceptionMatch is not None \
- and nextExceptionMatch.start() <= match.start():
+ and nextExceptionMatch.start() <= match_start:
# an HTML comment or text in nowiki tags stands before the next
# valid match. Skip.
index = nextExceptionMatch.end()
@@ -482,19 +484,19 @@
last = group_match.end()
replacement += new[last:]
- text = text[:match.start()] + replacement + text[match.end():]
+ text = text[:match_start] + replacement + text[match.end():]
# continue the search on the remaining text
if allowoverlap:
- index = match.start() + 1
+ index = match_start + 1
else:
- index = match.start() + len(replacement)
+ index = match_start + len(replacement)
if not match.group():
# When the regex allows to match nothing, shift by one char
index += 1
- markerpos = match.start() + len(replacement)
+ markerpos = match_start + len(replacement)
replaced += 1
return text[:markerpos] + marker + text[markerpos:]
--
To view, visit
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1340528?usp=email
To unsubscribe, or for help writing mail filters, visit
https://gerrit.wikimedia.org/r/settings?usp=email
Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I22598cb155eceee0ad1d49e7c41d1de4b0beabba
Gerrit-Change-Number: 1340528
Gerrit-PatchSet: 2
Gerrit-Owner: Mahveotm <[email protected]>
Gerrit-Reviewer: Mahveotm <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]