Skip to main content

pathlib_normalize

Function pathlib_normalize 

Source
pub fn pathlib_normalize(path: &str, fmt: PathFormat) -> String
Expand description

Canonicalize a filesystem path the same way Python’s pathlib does on construction.

Mirrors pathlib.PurePath’s _format_parsed_parts / _load_parts machinery for filesystem inputs (POSIX and Windows). The transformation is intentionally distinct from normalize (the private trailing-separator stripper above) — pathlib_normalize is what the public path() constructor runs on its argument so that downstream operations see a canonical form, exactly as pathlib does.

Rules (POSIX):

  • empty input → "."
  • exactly two leading / → preserved (POSIX double-slash root)
  • 1 or 3+ leading / → collapsed to a single /
  • drop interior . segments (a/./b → a/b)
  • drop empty segments from runs of separators (a//b → a/b)
  • strip trailing separators (a/b/ → a/b)
  • .. segments are kept verbatim (a/../b is NOT simplified; pathlib does not resolve .. because it cannot do so safely without filesystem access)
  • dot path . and empty input both render as "."

Rules (Windows):

  • same dot/empty rules as POSIX
  • all / are converted to \
  • drive-relative anchors C: (no separator) are preserved as C:, drive-absolute anchors C:\ keep their trailing \
  • UNC anchors \\server\share always end with \
  • a single leading \ (rooted-no-drive) is preserved

URIs are NOT handled here — the caller routes URI inputs (uri_path::is_uri) to the URI codepath which preserves every byte verbatim. URIs are opaque per the OpenJD spec (see specifications wiki §1.2.1 path types).