Skip to content

Commit 9627a88

Browse files
authored
perf: dual replace (#1064)
Signed-off-by: Henry Schreiner <henryfs@princeton.edu>
1 parent d5398b8 commit 9627a88

1 file changed

Lines changed: 2 additions & 6 deletions

File tree

src/packaging/utils.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ class InvalidSdistFilename(ValueError):
3434

3535
# Core metadata spec for `Name`
3636
_validate_regex = re.compile(r"[A-Z0-9]|[A-Z0-9][A-Z0-9._-]*[A-Z0-9]", re.IGNORECASE)
37-
_canonicalize_table = str.maketrans(
38-
"ABCDEFGHIJKLMNOPQRSTUVWXYZ_.",
39-
"abcdefghijklmnopqrstuvwxyz--",
40-
)
4137
_normalized_regex = re.compile(r"[a-z0-9]|[a-z0-9]([a-z0-9-](?!--))*[a-z0-9]")
4238
# PEP 427: The build number must start with a digit.
4339
_build_tag_regex = re.compile(r"(\d+)(.*)")
@@ -48,8 +44,8 @@ def canonicalize_name(name: str, *, validate: bool = False) -> NormalizedName:
4844
raise InvalidName(f"name is invalid: {name!r}")
4945
# Ensure all ``.`` and ``_`` are ``-``
5046
# Emulates ``re.sub(r"[-_.]+", "-", name).lower()`` from PEP 503
51-
# About 2x faster, safe since packages only support alphanumeric characters
52-
value = name.translate(_canonicalize_table)
47+
# Much faster than re, and even faster than str.translate
48+
value = name.lower().replace("_", "-").replace(".", "-")
5349
# Condense repeats (faster than regex)
5450
while "--" in value:
5551
value = value.replace("--", "-")

0 commit comments

Comments
 (0)