Skip to content

Commit

Permalink
Compile the regex at module level
Browse files Browse the repository at this point in the history
  • Loading branch information
grzesiek2010 committed Oct 28, 2023
1 parent 1051a5f commit 68db3c3
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions pyxform/validators/pyxform/android_package_name.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import re
from typing import Optional

PACKAGE_NAME_REGEX = re.compile(r"[^a-zA-Z0-9._]")


def validate_android_package_name(name: str) -> Optional[str]:
prefix = "Parameter 'app' has an invalid Android package name - "
Expand All @@ -24,9 +26,8 @@ def validate_android_package_name(name: str) -> Optional[str]:
if any(segment[0].isdigit() for segment in segments):
return f"{prefix}a digit cannot be the first character in a package name segment."

pattern = re.compile(r"[^a-zA-Z0-9._]")
for segment in segments:
if pattern.search(segment):
if PACKAGE_NAME_REGEX.search(segment):
return f"{prefix}the package name contains characters that are not allowed. Package names can only include letters (a-z, A-Z), numbers (0-9), dots (.), and underscores (_)."

return None

0 comments on commit 68db3c3

Please sign in to comment.