perf: Speed up clean functions

This commit is contained in:
Karim shoair
2025-07-30 02:03:27 +03:00
parent 9415acccce
commit 3c5de8e0f2
2 changed files with 9 additions and 7 deletions
+5 -3
View File
@@ -14,6 +14,9 @@ html_forbidden = {
html.HtmlComment,
}
__CLEANING_TABLE__ = str.maketrans({"\t": " ", "\n": None, "\r": None})
__CONSECUTIVE_SPACES_REGEX__ = re.compile(r" +")
@lru_cache(1, typed=True)
def setup_logger():
@@ -135,6 +138,5 @@ class _StorageTools:
@lru_cache(128, typed=True)
def clean_spaces(string):
string = string.replace("\t", " ")
string = re.sub("[\n|\r]", "", string)
return re.sub(" +", " ", string)
string = string.translate(__CLEANING_TABLE__)
return __CONSECUTIVE_SPACES_REGEX__.sub(" ", string)