Standard library · System and tools

filepath

Path functions that never touch the file system; the same work as the fs/path package.

import filepath

The filepath package works on strings alone and never touches the file system, apart from Abs, which needs the current directory. The fs.path package is built on it; use whichever reads better to you.

Constants and variables

Separator

const Separator: str = "/"
The separator between the parts of a path: /.
filepath-Join.salam
import filepath

func main:
    p := filepath.Join("projects", "salam/main.salam")
    println p, filepath.Separator
    println filepath.Dir(p), filepath.Base(p), filepath.Ext(p), filepath.Stem(p)
    println filepath.Normalize("a//b/./c/"), filepath.Clean("a/b/../c")
    println filepath.IsAbs("/usr/bin"), filepath.IsAbs(p), filepath.IsAbs(filepath.Abs(p))
    println filepath.Rel("/a/b", "/a/b/c/d")
    println filepath.IsSafe("/srv", "x/y"), filepath.IsSafe("/srv", "../etc")
    println filepath.Validate("a/b")
end
Outputprojects/salam/main.salam / projects/salam main.salam .salam main a//b/./c/ a/c true false true c/d true false true

Functions

Normalize

func Normalize(p: str): str
Turns \ separators into /.

The example for this item is shown with the ones above: filepath-Join.salam

IsAbs

func IsAbs(p: str): bool
True when the path is absolute; a Windows path such as C:/ counts as absolute.

The example for this item is shown with the ones above: filepath-Join.salam

Join

func Join(a: str, b: str): str
Joins two parts of a path.

The example for this item is shown with the ones above: filepath-Join.salam

Base

func Base(p: str): str
The name of the last part of a path.

The example for this item is shown with the ones above: filepath-Join.salam

Dir

func Dir(p: str): str
The path without its last part.

The example for this item is shown with the ones above: filepath-Join.salam

Ext

func Ext(p: str): str
The extension, with its dot.

The example for this item is shown with the ones above: filepath-Join.salam

Stem

func Stem(p: str): str
The last part's name without its extension.

The example for this item is shown with the ones above: filepath-Join.salam

Clean

func Clean(p: str): str
Simplifies a path.

The example for this item is shown with the ones above: filepath-Join.salam

Abs

func Abs(p: str): str
Makes a path absolute.

The example for this item is shown with the ones above: filepath-Join.salam

Rel

func Rel(base: str, target: str): str
The relative path from base to target.

The example for this item is shown with the ones above: filepath-Join.salam

Validate

func Validate(p: str): bool
True when the path is not empty.

The example for this item is shown with the ones above: filepath-Join.salam

IsSafe

func IsSafe(base: str, p: str): bool
True when the path stays inside base.

The example for this item is shown with the ones above: filepath-Join.salam