Skip to content

Commit

Permalink
Optimize regex
Browse files Browse the repository at this point in the history
  • Loading branch information
jotonedev committed Oct 13, 2024
1 parent b1a77e0 commit 6400b7e
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pyown/messages/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class BaseMessage(abc.ABC):
suffix: Final[str] = "##" # Suffix of the message
separator: Final[str] = "*" # Separator of the tags

_regex: Pattern[str] = re.compile(r"^\*(?:([0-9#]*)\*?)+##$") # Regex pattern used to match the message
_regex: Pattern[str] = re.compile(r"^\*(?:([0-9]*)(?:#[0-9]*)*\*?)+##$") # Regex pattern used to match the message

@abc.abstractmethod
def __init__(self, *args, **kwargs) -> None:
Expand Down
6 changes: 3 additions & 3 deletions pyown/messages/dimension.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class DimensionRequest(BaseMessage):
_type: MessageType = MessageType.DIMENSION_REQUEST
_tags: tuple[Who, Where, Dimension]

_regex: Pattern[str] = re.compile(r"^\*#[0-9#]+\*[0-9#]*\*[0-9]*##$")
_regex: Pattern[str] = re.compile(r"^\*#[0-9#]+\*[0-9]*(?:#[0-9]*)*\*[0-9]*##$")

def __init__(self, tags: tuple[Who, Where, Dimension]):
self._tags = tags
Expand Down Expand Up @@ -67,7 +67,7 @@ class DimensionWriting(BaseMessage):
_type: MessageType = MessageType.DIMENSION_WRITING
_tags: tuple[Who, Where, Dimension, Value, ...] # type: ignore[misc]

_regex: Pattern[str] = re.compile(r"^\*#[0-9#]+\*[0-9#]*\*#[0-9]*(?:\*[0-9#]*)*##$")
_regex: Pattern[str] = re.compile(r"^\*#[0-9#]+\*[0-9]*(?:#[0-9]*)*\*#[0-9]*(?:\*[0-9]*(?:#[0-9]*)*)*##$")

def __init__(self, tags: tuple[Who, Where, Dimension, Value, ...]): # type: ignore[misc]
self._tags = tags
Expand Down Expand Up @@ -118,7 +118,7 @@ class DimensionResponse(DimensionWriting, BaseMessage):
This is sent by the server to the client
"""
_type: MessageType = MessageType.DIMENSION_RESPONSE
_regex: Pattern[str] = re.compile(r"^\*#[0-9#]+\*[0-9#]*\*[0-9#]*(?:\*[0-9#]*)*##$")
_regex: Pattern[str] = re.compile(r"^\*#[0-9#]+\*[0-9]*(?:#[0-9]*)*\*[0-9]*(?:#[0-9]*)*(?:\*[0-9]*(?:#[0-9]*)*)*##$")

@property
def message(self) -> str:
Expand Down
2 changes: 1 addition & 1 deletion pyown/messages/normal.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class NormalMessage(BaseMessage):
_type: MessageType = MessageType.NORMAL
_tags: tuple[Who, What, Where]

_regex: Pattern[str] = re.compile(r"^\*[0-9#]+\*[0-9#]*\*[0-9#]*##$")
_regex: Pattern[str] = re.compile(r"^\*[0-9#]+\*[0-9]*(?:#[0-9]*)*\*[0-9]*(?:#[0-9]*)*##$")

def __init__(self, tags: tuple[Who, What, Where]):
self._tags = tags
Expand Down
2 changes: 1 addition & 1 deletion pyown/messages/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class StatusRequest(BaseMessage):
_type: MessageType = MessageType.STATUS_REQUEST
_tags: tuple[Who, Where]

_regex: Pattern[str] = re.compile(r"^\*#[0-9#]+\*[0-9#]*##$")
_regex: Pattern[str] = re.compile(r"^\*#[0-9#]+\*[0-9]*(?:#[0-9]*)*##$")

def __init__(self, tags: tuple[Who, Where]):
self._tags = tags
Expand Down

0 comments on commit 6400b7e

Please sign in to comment.