From a58de3b1f6363a8ec5d663ef6bcdd1b615711517 Mon Sep 17 00:00:00 2001 From: TheJackiMonster Date: Tue, 1 Jun 2021 15:17:54 +0200 Subject: [PATCH] Optimized counting words and characters via regex Signed-off-by: TheJackiMonster --- manuskript/functions/__init__.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/manuskript/functions/__init__.py b/manuskript/functions/__init__.py index 4bce00b4..58a07b6d 100644 --- a/manuskript/functions/__init__.py +++ b/manuskript/functions/__init__.py @@ -24,17 +24,14 @@ MW = None def wordCount(text): - t = text.strip().replace(" ", "\n").split("\n") - t = [l for l in t if l] - return len(t) + return len(re.findall(r"\S+", text)) + def charCount(text, use_spaces = True): - t = text.strip() - - if not use_spaces: - t = t.replace(" ", "") - - return len(t) + if use_spaces: + return len(re.findall(r"[\S ]", text)) + else: + return len(re.findall(r"\S", text)) validate_ok = lambda *args, **kwargs: True def uiParse(input, default, converter, validator=validate_ok):