Skip to content

Commit

Permalink
Speed up is_template_string by avoiding regex engine (#118076)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed May 25, 2024
1 parent 4b0f58e commit f026083
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions homeassistant/helpers/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@
)
_HASS_LOADER = "template.hass_loader"

_RE_JINJA_DELIMITERS = re.compile(r"\{%|\{\{|\{#")
# Match "simple" ints and floats. -1.0, 1, +5, 5.0
_IS_NUMERIC = re.compile(r"^[+-]?(?!0\d)\d*(?:\.\d*)?$")

Expand Down Expand Up @@ -261,7 +260,9 @@ def is_complex(value: Any) -> bool:

def is_template_string(maybe_template: str) -> bool:
"""Check if the input is a Jinja2 template."""
return _RE_JINJA_DELIMITERS.search(maybe_template) is not None
return "{" in maybe_template and (
"{%" in maybe_template or "{{" in maybe_template or "{#" in maybe_template
)


class ResultWrapper:
Expand Down

0 comments on commit f026083

Please sign in to comment.