mirror of https://github.com/home-assistant/core
Cache template regex compiles (#78529)
This commit is contained in:
parent
59aef20e99
commit
8dbbd0ded0
|
@ -1719,7 +1719,13 @@ def regex_match(value, find="", ignorecase=False):
|
|||
if not isinstance(value, str):
|
||||
value = str(value)
|
||||
flags = re.I if ignorecase else 0
|
||||
return bool(re.match(find, value, flags))
|
||||
return bool(_regex_cache(find, flags).match(value))
|
||||
|
||||
|
||||
@lru_cache(maxsize=128)
|
||||
def _regex_cache(find: str, flags: int) -> re.Pattern:
|
||||
"""Cache compiled regex."""
|
||||
return re.compile(find, flags)
|
||||
|
||||
|
||||
def regex_replace(value="", find="", replace="", ignorecase=False):
|
||||
|
@ -1727,8 +1733,7 @@ def regex_replace(value="", find="", replace="", ignorecase=False):
|
|||
if not isinstance(value, str):
|
||||
value = str(value)
|
||||
flags = re.I if ignorecase else 0
|
||||
regex = re.compile(find, flags)
|
||||
return regex.sub(replace, value)
|
||||
return _regex_cache(find, flags).sub(replace, value)
|
||||
|
||||
|
||||
def regex_search(value, find="", ignorecase=False):
|
||||
|
@ -1736,7 +1741,7 @@ def regex_search(value, find="", ignorecase=False):
|
|||
if not isinstance(value, str):
|
||||
value = str(value)
|
||||
flags = re.I if ignorecase else 0
|
||||
return bool(re.search(find, value, flags))
|
||||
return bool(_regex_cache(find, flags).search(value))
|
||||
|
||||
|
||||
def regex_findall_index(value, find="", index=0, ignorecase=False):
|
||||
|
@ -1749,7 +1754,7 @@ def regex_findall(value, find="", ignorecase=False):
|
|||
if not isinstance(value, str):
|
||||
value = str(value)
|
||||
flags = re.I if ignorecase else 0
|
||||
return re.findall(find, value, flags)
|
||||
return _regex_cache(find, flags).findall(value)
|
||||
|
||||
|
||||
def bitwise_and(first_value, second_value):
|
||||
|
|
Loading…
Reference in New Issue