Fast path returns for normalize_path cases (#3189)

Co-authored-by: Kar Petrosyan <92274156+karpetrosyan@users.noreply.github.com>
This commit is contained in:
Tom Christie 2024-05-17 18:25:38 +01:00 committed by GitHub
parent 88a81c5d31
commit 37593c1952
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -392,8 +392,17 @@ def normalize_path(path: str) -> str:
normalize_path("/path/./to/somewhere/..") == "/path/to"
"""
# https://datatracker.ietf.org/doc/html/rfc3986#section-5.2.4
# Fast return when no '.' characters in the path.
if "." not in path:
return path
components = path.split("/")
# Fast return when no '.' or '..' components in the path.
if "." not in components and ".." not in components:
return path
# https://datatracker.ietf.org/doc/html/rfc3986#section-5.2.4
output: list[str] = []
for component in components:
if component == ".":