Skip to content

Commit

Permalink
Slightly speed up statistics calculation using regular expressions
Browse files Browse the repository at this point in the history
Refs #360.
  • Loading branch information
mitya57 committed Oct 15, 2018
1 parent db20aaa commit daad167
Showing 1 changed file with 5 additions and 12 deletions.
17 changes: 5 additions & 12 deletions ReText/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ class ReTextEdit(QTextEdit):
scrollLimitReached = pyqtSignal(QWheelEvent)
returnBlockPattern = re.compile("^[\\s]*([*>-]|\\d+\\.) ")
orderedListPattern = re.compile("^([\\s]*)(\\d+)\\. $")
wordPattern = re.compile(r"\w+")
nonAlphaNumPattern = re.compile(r"\W")

def __init__(self, parent):
QTextEdit.__init__(self)
Expand Down Expand Up @@ -432,19 +434,10 @@ def updateTextStatistics(self):
if not globalSettings.documentStatsEnabled:
return
text = self.toPlainText()
wasWordCharacter = False
wordCount = 0
alphaNumCount = 0
wordCount = len(self.wordPattern.findall(text))
alphaNums = self.nonAlphaNumPattern.sub('', text)
alphaNumCount = len(alphaNums)
characterCount = len(text)
for c in text:
isWordCharacter = c.isalnum()
if isWordCharacter:
alphaNumCount += 1
if wasWordCharacter and not isWordCharacter:
wordCount += 1
wasWordCharacter = isWordCharacter
if wasWordCharacter:
wordCount += 1
self.statistics = (wordCount, alphaNumCount, characterCount)


Expand Down

0 comments on commit daad167

Please sign in to comment.