From d3799bc30ac5fe68c7af2cc8ab09e4165dc1c1bf Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Sun, 22 Sep 2024 16:37:45 +0200 Subject: [PATCH] Update plain regex patterns to std lib re --- novelwriter/constants.py | 2 +- novelwriter/gui/dochighlight.py | 56 ++++++++++++++++++--------------- 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/novelwriter/constants.py b/novelwriter/constants.py index e16bfa03..1905c856 100644 --- a/novelwriter/constants.py +++ b/novelwriter/constants.py @@ -68,7 +68,7 @@ class nwRegEx: FMT_SC = r"(?i)(?{1,2}|<{1,2}$)") - rxRule.setPatternOptions(QRegExUnicode) + rxRule = re.compile(r"(^>{1,2}|<{1,2}$)", re.UNICODE) hlRule = { 1: self._hStyles["markup"], } @@ -240,8 +237,7 @@ class GuiDocHighlighter(QSyntaxHighlighter): self._txtRules.append((rxRule, hlRule)) # Auto-Replace Tags - rxRule = QRegularExpression(r"<(\S+?)>") - rxRule.setPatternOptions(QRegExUnicode) + rxRule = re.compile(r"<(\S+?)>", re.UNICODE) hlRule = { 0: self._hStyles["replace"], } @@ -403,17 +399,29 @@ class GuiDocHighlighter(QSyntaxHighlighter): if hRules: for rX, hRule in hRules: - rxItt = rX.globalMatch(text, xOff) - while rxItt.hasNext(): - rxMatch = rxItt.next() - for xM, hFmt in hRule.items(): - xPos = rxMatch.capturedStart(xM) - xEnd = rxMatch.capturedEnd(xM) - for x in range(xPos, xEnd): - cFmt = self.format(x) - if cFmt.fontStyleName() != "markup": - cFmt.merge(hFmt) - self.setFormat(x, 1, cFmt) + if isinstance(rX, QRegularExpression): + rxItt = rX.globalMatch(text, xOff) + while rxItt.hasNext(): + rxMatch = rxItt.next() + for xM, hFmt in hRule.items(): + xPos = rxMatch.capturedStart(xM) + xEnd = rxMatch.capturedEnd(xM) + for x in range(xPos, xEnd): + cFmt = self.format(x) + if cFmt.fontStyleName() != "markup": + cFmt.merge(hFmt) + self.setFormat(x, 1, cFmt) + else: + for match in re.finditer(rX, text[xOff:]): + for xM, hFmt in hRule.items(): + # print(f"'{match.group(xM)}'", match.start(xM), match.end(xM)) + xPos = match.start(xM) + xOff + xEnd = match.end(xM) + xOff + for x in range(xPos, xEnd): + cFmt = self.format(x) + if cFmt.fontStyleName() != "markup": + cFmt.merge(hFmt) + self.setFormat(x, 1, cFmt) data = self.currentBlockUserData() if not isinstance(data, TextBlockData): @@ -498,10 +506,8 @@ class TextBlockData(QTextBlockUserData): for match in re.finditer(nwRegEx.RX_WORDS, text[offset:].replace("_", " ")): if ( (word := match.group(0)) - and (iS := match.start(0)) >= 0 - and (iE := match.end(0)) >= 0 and not (word.isnumeric() or word.isupper() or checker.checkWord(word)) ): - self._spellErrors.append((iS + offset, iE + offset)) + self._spellErrors.append((match.start(0) + offset, match.end(0) + offset)) return self._spellErrors