Skip to content

Commit d363685

Browse files
committed
Check for duplicate patterns
1 parent 2b2117f commit d363685

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

search_offsets/patterns.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from pathlib import Path
44
from typing import NamedTuple
55

6+
from loguru import logger
67
from more_itertools import chunked
78

89

@@ -20,6 +21,14 @@ def __str__(self) -> str:
2021
def __repr__(self) -> str:
2122
return f"{self.__class__.__name__}(name={self.name!r}, pattern={self.pattern!r})"
2223

24+
def __hash__(self) -> int:
25+
return hash(tuple(self.pattern))
26+
27+
def __eq__(self, other: object) -> bool:
28+
if not isinstance(other, Pattern):
29+
return False
30+
return self.pattern == other.pattern
31+
2332

2433
def hex_to_bytes(s: str) -> bytes:
2534
"""
@@ -35,6 +44,22 @@ def convert_to_pattern(s: list[str]) -> list[int | None]:
3544
return [None if item == "??" else int(item, 16) for item in s]
3645

3746

47+
def check_duplicates(patterns: list[Pattern]) -> None:
48+
"""
49+
Check for duplicate patterns.
50+
"""
51+
duplicates = defaultdict(list)
52+
53+
for pattern in patterns:
54+
duplicates[pattern].append(pattern.name)
55+
56+
print(list(duplicates.values()))
57+
58+
for names in duplicates.values():
59+
if len(names) > 1:
60+
logger.warning(f"Duplicate patterns: {names}")
61+
62+
3863
def load_patterns(pattern_path: Path) -> list[Pattern]:
3964
"""
4065
Load patterns to a list from ffsess file.
@@ -52,6 +77,8 @@ def load_patterns(pattern_path: Path) -> list[Pattern]:
5277

5378
patterns.append(pattern_object)
5479

80+
check_duplicates(patterns)
81+
5582
return patterns
5683

5784

0 commit comments

Comments
 (0)