File tree Expand file tree Collapse file tree 1 file changed +7
-19
lines changed Expand file tree Collapse file tree 1 file changed +7
-19
lines changed Original file line number Diff line number Diff line change 11from __future__ import annotations
22
3+ import re
34from pathlib import Path
45from typing import TYPE_CHECKING
56
89
910
1011def normalize_path (path : str | bytes | Path | None ) -> str :
11- # handle bytes
1212 if path is None :
1313 result = ""
1414 elif isinstance (path , bytes ):
1515 result = str (path , "ascii" )
16- # ensure str
17- # handle pathlib.Path and upath.Path
16+
17+ # handle pathlib.Path
1818 elif isinstance (path , Path ):
1919 result = str (path )
2020
@@ -27,24 +27,12 @@ def normalize_path(path: str | bytes | Path | None) -> str:
2727 # convert backslash to forward slash
2828 result = result .replace ("\\ " , "/" )
2929
30- # ensure no leading slash
31- while len (result ) > 0 and result [0 ] == "/" :
32- result = result [1 :]
33-
34- # ensure no trailing slash
35- while len (result ) > 0 and result [- 1 ] == "/" :
36- result = result [:- 1 ]
30+ # remove leading and trailing slashes
31+ result = result .strip ("/" )
3732
3833 # collapse any repeated slashes
39- previous_char = None
40- collapsed = ""
41- for char in result :
42- if char == "/" and previous_char == "/" :
43- pass
44- else :
45- collapsed += char
46- previous_char = char
47- result = collapsed
34+ pat = re .compile (r"//+" )
35+ result = pat .sub ("/" , result )
4836
4937 # disallow path segments with just '.' or '..'
5038 segments = result .split ("/" )
You can’t perform that action at this time.
0 commit comments