Skip to content

Commit

Permalink
Fix regexes to split only prefixes/suffixes
Browse files Browse the repository at this point in the history
In theory this shouldn't be necessary, but it's better to be safe.

Signed-off-by: Nikola Forró <[email protected]>
  • Loading branch information
nforro committed Jul 2, 2024
1 parent ea9a85b commit cac33a0
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
4 changes: 2 additions & 2 deletions specfile/prep.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class PatchMacro(PrepMacro):
@property
def number(self) -> int:
"""Number of the %patch macro."""
tokens = re.split(r"(\d+)", self.name, maxsplit=1)
tokens = re.split(r"(\d+)$", self.name, maxsplit=1)
if len(tokens) > 1:
return int(tokens[1])
if self.options.P is not None:
Expand All @@ -123,7 +123,7 @@ def number(self) -> int:

@number.setter
def number(self, value: int) -> None:
tokens = re.split(r"(\d+)", self.name, maxsplit=1)
tokens = re.split(r"(\d+)$", self.name, maxsplit=1)
if len(tokens) > 1:
self.name = f"{tokens[0]}{value}"
elif self.options.P is not None:
Expand Down
2 changes: 1 addition & 1 deletion specfile/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def _extract_number(self) -> Optional[str]:
Returns:
Extracted number or `None` if there isn't one.
"""
tokens = re.split(r"(\d+)", self._tag.name, maxsplit=1)
tokens = re.split(r"(\d+)$", self._tag.name, maxsplit=1)
if len(tokens) > 1:
return tokens[1]
return None
Expand Down
8 changes: 4 additions & 4 deletions specfile/value_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class MacroSubstitution(Node):
"""Node representing macro substitution, e.g. _%version_."""

def __init__(self, body: str) -> None:
tokens = re.split(r"([?!]*)", body, maxsplit=1)
tokens = re.split(r"^([?!]*)", body, maxsplit=1)
if len(tokens) == 1:
self.prefix, self.name = "", tokens[0]
else:
Expand All @@ -97,7 +97,7 @@ class EnclosedMacroSubstitution(Node):
"""Node representing macro substitution enclosed in brackets, e.g. _%{?dist}_."""

def __init__(self, body: str) -> None:
tokens = re.split(r"([?!]*)", body, maxsplit=1)
tokens = re.split(r"^([?!]*)", body, maxsplit=1)
if len(tokens) == 1:
self.prefix, rest = "", tokens[0]
else:
Expand Down Expand Up @@ -129,7 +129,7 @@ class ConditionalMacroExpansion(Node):
"""Node representing conditional macro expansion, e.g. _%{?prerel:0.}_."""

def __init__(self, condition: str, body: List[Node]) -> None:
tokens = re.split(r"([?!]*)", condition, maxsplit=1)
tokens = re.split(r"^([?!]*)", condition, maxsplit=1)
if len(tokens) == 1:
self.prefix, self.name = "", tokens[0]
else:
Expand Down Expand Up @@ -271,7 +271,7 @@ def find_macro_end(index):
elif value[start + 1] == "{":
if ":" in value[start:end]:
condition, body = value[start + 2 : end - 1].split(":", maxsplit=1)
tokens = re.split(r"([?!]*)", condition, maxsplit=1)
tokens = re.split(r"^([?!]*)", condition, maxsplit=1)
prefix = tokens[0 if len(tokens) == 1 else 1]
if "?" in prefix:
result.append(
Expand Down

0 comments on commit cac33a0

Please sign in to comment.