mirror of
https://github.com/olivierkes/manuskript.git
synced 2024-06-01 10:39:34 +12:00
Compile regex patterns once
Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>
This commit is contained in:
parent
eac2964330
commit
e368cc7f73
|
@ -5,6 +5,10 @@ import re
|
||||||
|
|
||||||
from enum import Enum, unique
|
from enum import Enum, unique
|
||||||
|
|
||||||
|
_char_pattern_with_spaces = re.compile(r"[\S ]")
|
||||||
|
_char_pattern_without_spaces = re.compile(r"\S")
|
||||||
|
_word_pattern = re.compile(r"\S+")
|
||||||
|
|
||||||
|
|
||||||
@unique
|
@unique
|
||||||
class CounterKind(Enum):
|
class CounterKind(Enum):
|
||||||
|
@ -18,16 +22,16 @@ class CharCounter:
|
||||||
@classmethod
|
@classmethod
|
||||||
def count(cls, text: str, use_spaces: bool = False):
|
def count(cls, text: str, use_spaces: bool = False):
|
||||||
if use_spaces:
|
if use_spaces:
|
||||||
return len(re.findall(r"[\S ]", text))
|
return len(_char_pattern_with_spaces.findall(text))
|
||||||
else:
|
else:
|
||||||
return len(re.findall(r"\S", text))
|
return len(_char_pattern_without_spaces.findall(text))
|
||||||
|
|
||||||
|
|
||||||
class WordCounter:
|
class WordCounter:
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def count(cls, text: str):
|
def count(cls, text: str):
|
||||||
return len(re.findall(r"\S+", text))
|
return len(_word_pattern.findall(text))
|
||||||
|
|
||||||
|
|
||||||
class PageCounter:
|
class PageCounter:
|
||||||
|
|
Loading…
Reference in a new issue