From 68db3c3de8edf15816f8dad3da106b32b229ae5c Mon Sep 17 00:00:00 2001 From: Grzegorz Orczykowski Date: Sun, 29 Oct 2023 01:25:48 +0200 Subject: [PATCH] Compile the regex at module level --- pyxform/validators/pyxform/android_package_name.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pyxform/validators/pyxform/android_package_name.py b/pyxform/validators/pyxform/android_package_name.py index 7db9af2f..1c7da8da 100644 --- a/pyxform/validators/pyxform/android_package_name.py +++ b/pyxform/validators/pyxform/android_package_name.py @@ -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 - " @@ -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