pathx

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 10, 2026 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package pathx contains tiny pure utilities for slash-separated logical paths — tree paths, projection paths, URL segments. It is NOT for OS filesystem paths; for those, use path/filepath from the standard library.

Why not stdlib path

path.Base / path.Dir use unix-basename semantics: empty path becomes ".", "foo/" becomes "foo", "/" stays "/". pathx uses literal last-segment / first-segment semantics where empty or trailing-slash inputs produce empty output, matching how Scrinium represents tree paths. Mixing the two would be a silent footgun. Callers that want unix-basename should reach for path.Base directly.

All functions are pure: no I/O, no allocations beyond the returned slice / string. Safe for concurrent use.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsStrictUnder

func IsStrictUnder(p, prefix string) bool

IsStrictUnder reports whether p is a strict descendant of prefix — equality is NOT a match. Use IsUnder to include equality.

IsStrictUnder("a/b/c", "a/b")  == true
IsStrictUnder("a/b",   "a/b")  == false
IsStrictUnder("a/bc",  "a/b")  == false

func IsUnder

func IsUnder(p, prefix string) bool

IsUnder reports whether p equals prefix or is a descendant of prefix (i.e. p == prefix || p starts with prefix+"/"). Empty prefix matches everything except empty p.

IsUnder("a/b/c", "a/b")  == true
IsUnder("a/b",   "a/b")  == true
IsUnder("a/bc",  "a/b")  == false
IsUnder("a",     "")     == true
IsUnder("",      "")     == false

func Join

func Join(parent, child string) string

Join glues parent and child with a single "/" between them. Empty parent returns child; empty child returns parent. Does not normalise existing slashes in either argument.

Join("a/b", "c")   == "a/b/c"
Join("", "c")      == "c"
Join("a", "")      == "a"
Join("", "")       == ""

func LastSegment

func LastSegment(p string) string

LastSegment returns everything after the last "/" in p, or p itself if p contains no slash. Empty input returns empty.

LastSegment("a/b/c")   == "c"
LastSegment("a")       == "a"
LastSegment("")        == ""
LastSegment("a/b/")    == ""
LastSegment("/")       == ""

func Parent

func Parent(p string) string

Parent returns everything before the last "/" in p, or empty if p contains no slash.

Parent("a/b/c")  == "a/b"
Parent("a")      == ""
Parent("/a")     == ""
Parent("")       == ""

func SplitFirst

func SplitFirst(p string) (first, rest string)

SplitFirst returns the first segment and the remainder of p. For paths with no slash the entire input is the first segment and rest is "".

SplitFirst("a/b/c")  == ("a", "b/c")
SplitFirst("a")      == ("a", "")
SplitFirst("")       == ("", "")
SplitFirst("/a")     == ("", "a")

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL