1
0
mirror of https://github.com/exaloop/codon.git synced 2025-06-03 15:03:52 +08:00
codon/stdlib/os/path.codon

22 lines
659 B
Python
Raw Normal View History

2022-01-24 11:02:49 +01:00
# (c) 2022 Exaloop Inc. All rights reserved.
2021-09-27 14:02:44 -04:00
def splitext(p: str) -> Tuple[str, str]:
"""
Split the extension from a pathname.
Extension is everything from the last dot to the end, ignoring
leading dots. Returns "(root, ext)"; ext may be empty."""
2022-01-24 11:02:49 +01:00
sep = "/"
extsep = "."
2021-09-27 14:02:44 -04:00
sepIndex = p.rfind(sep)
dotIndex = p.rfind(extsep)
if dotIndex > sepIndex:
# skip all leading dots
filenameIndex = sepIndex + 1
while filenameIndex < dotIndex:
2022-01-24 11:02:49 +01:00
if p[filenameIndex : filenameIndex + 1] != extsep:
2021-09-27 14:02:44 -04:00
return p[:dotIndex], p[dotIndex:]
filenameIndex += 1
return p, p[:0]