Skip to content

Commit f4d2aa1

Browse files
committed
apply suggestions from code review
1 parent 54642e8 commit f4d2aa1

File tree

1 file changed

+7
-19
lines changed

1 file changed

+7
-19
lines changed

src/zarr/storage/_utils.py

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import re
34
from pathlib import Path
45
from typing import TYPE_CHECKING
56

@@ -8,13 +9,12 @@
89

910

1011
def 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("/")

0 commit comments

Comments
 (0)