Skip to content
dream-path
guide

Inspecting paths

Parent, file name and extension of a normalized path, and exactly what each returns at the edges.

Three helpers split a normalized path at its separators. They are byte operations on / and ., nothing more: they do not resolve .., recognize drive letters or look at the file system. None of them allocate.

use dream_path::NormalizedPath;

fn main() {
    let path = NormalizedPath::new(r"Textures\Architecture\Wall.DDS");
    assert_eq!(path.parent().unwrap(), "textures/architecture");
    assert_eq!(path.file_name().unwrap(), "wall.dds");
    assert_eq!(path.extension().unwrap(), "dds");
}

Each has two forms: a method on NormalizedPath, and a free function over borrowed bytes, parent_normalized, file_name_normalized and extension_normalized, for bytes you know are normalized already. The free functions do not normalize; give them anything else and they split whatever they were given.

At the edges#

Pathparentfile_nameextension
textures/wall.ddstextureswall.ddsdds
wall.ddsnonewall.ddsdds
music/explore/musicexplorenone
archive.tar.gznonearchive.tar.gzgz
.hiddennone.hiddennone
notes.nonenotes.none
sound/fx/../door.wavsound/fx/..door.wavwav
the empty pathnonenonenone
  • A trailing separator is ignored when finding the parent and file name, so music/explore/ has the file name explore.
  • A path ending in / has no extension, so textures/wall.dds/ is not a DDS file.
  • A name that starts or ends with its only dot has no extension.
  • The extension is the text after the last dot, so archive.tar.gz is gz.

In Luau, fileName, parent and extension normalize their argument first and then apply the same rules.