Documentation
¶
Overview ¶
Package pathname is a pure-Go (no cgo) reimplementation of the pure path-manipulation surface of Ruby's pathname standard library, as embedded in go-embedded-ruby's prelude and matching MRI 4.0.5.
A Pathname wraps a path string and exposes the lexical operations Ruby's Pathname offers — basename, dirname, extname, cleanpath, relative_path_from, join/+, split, each_filename, ascend/descend, sub_ext, sub, comparison — none of which touch the filesystem. The filesystem-touching delegations of Ruby's Pathname (read, write, exist?, …) are deliberately out of scope: in go-embedded-ruby they remain host-side, forwarding to the File class, while this library is the standalone, interpreter-independent path algebra.
Ruby's Pathname is "/"-based for these lexical operations on every platform (it never consults the OS path separator for join/split/cleanpath), so this package uses "/" unconditionally. The behaviour is therefore identical on Windows, Linux and macOS; the package builds and runs the same on GOOS=windows.
Index ¶
- Constants
- type ArgumentError
- type Pathname
- func (p *Pathname) Absolute() bool
- func (p *Pathname) Ascend(yield func(*Pathname))
- func (p *Pathname) Basename() *Pathname
- func (p *Pathname) BasenameSuffix(suffix string) *Pathname
- func (p *Pathname) Cleanpath() *Pathname
- func (p *Pathname) Cmp(other *Pathname) int
- func (p *Pathname) Descend(yield func(*Pathname))
- func (p *Pathname) Dirname() *Pathname
- func (p *Pathname) EachFilename(yield func(string))
- func (p *Pathname) Eql(other *Pathname) bool
- func (p *Pathname) Extname() string
- func (p *Pathname) Filenames() []string
- func (p *Pathname) Hash() uint64
- func (p *Pathname) Inspect() string
- func (p *Pathname) Join(args ...*Pathname) *Pathname
- func (p *Pathname) JoinStrings(args ...string) *Pathname
- func (p *Pathname) Parent() *Pathname
- func (p *Pathname) Plus(other *Pathname) *Pathname
- func (p *Pathname) PlusString(other string) *Pathname
- func (p *Pathname) Relative() bool
- func (p *Pathname) RelativePathFrom(baseDirectory *Pathname) (*Pathname, error)
- func (p *Pathname) Root() bool
- func (p *Pathname) Split() []*Pathname
- func (p *Pathname) String() string
- func (p *Pathname) Sub(pattern, repl string) *Pathname
- func (p *Pathname) SubExt(repl string) *Pathname
- func (p *Pathname) ToS() string
Constants ¶
const Separator = "/"
Separator is the path component separator. Ruby's Pathname hardcodes "/" for the lexical operations on every platform, and so does this package.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ArgumentError ¶
type ArgumentError struct {
Message string
}
ArgumentError mirrors the error Ruby's relative_path_from raises for an incompatible base directory. Its message matches MRI's wording.
func (*ArgumentError) Error ¶
func (e *ArgumentError) Error() string
type Pathname ¶
type Pathname struct {
// contains filtered or unexported fields
}
Pathname wraps a path string and exposes Ruby's pure path-manipulation methods. The zero value is a Pathname for the empty string. It is immutable: every transforming method returns a fresh *Pathname.
func (*Pathname) Absolute ¶
Absolute reports whether the path is absolute (begins with "/"), like Ruby's Pathname#absolute?.
func (*Pathname) Ascend ¶
Ascend yields the path then each parent up to the root (or the first relative component), matching Ruby's Pathname#ascend.
func (*Pathname) Basename ¶
Basename returns the last path component (Ruby's Pathname#basename with an empty suffix). For an all-separator or empty path it returns "/" as MRI does for a path whose only non-empty component is the root.
func (*Pathname) BasenameSuffix ¶
BasenameSuffix returns the last path component with an optional trailing suffix stripped, matching Ruby's Pathname#basename(suffix). A suffix of ".*" strips any file extension; any other non-empty suffix is removed only when the component ends with it.
func (*Pathname) Cleanpath ¶
Cleanpath collapses ".", ".." and redundant separators, matching Ruby's Pathname#cleanpath (the default, aggressive form). A ".." that would escape the root of an absolute path is dropped (as MRI does); a ".." that escapes a relative path is preserved.
func (*Pathname) Cmp ¶
Cmp compares two paths lexically by their wrapped strings, matching Ruby's Pathname#<=>: -1, 0 or 1.
func (*Pathname) Descend ¶
Descend yields the shortest prefix first down to the full path — the ascend sequence reversed — matching Ruby's Pathname#descend.
func (*Pathname) Dirname ¶
Dirname returns all but the last path component, matching Ruby's Pathname#dirname (and its alias #parent): "." when there is no separator and "/" when the only separator is the leading one.
func (*Pathname) EachFilename ¶
EachFilename calls yield once per non-empty path component (leading, trailing and doubled separators contribute no component), matching Ruby's Pathname#each_filename.
func (*Pathname) Eql ¶
Eql reports whether two paths are equal, matching Ruby's Pathname#== / #eql? (string equality of the wrapped paths).
func (*Pathname) Extname ¶
Extname returns the file extension of the last path component (".txt", or "" when there is none), matching Ruby's Pathname#extname.
func (*Pathname) Filenames ¶
Filenames returns the non-empty path components as a slice, matching the result of Ruby's each_filename.to_a.
func (*Pathname) Hash ¶
Hash returns the FNV-1a hash of the wrapped path. Ruby's Pathname#hash delegates to String#hash; this is a stable Go-side hash for use as a map key, equal exactly when the paths are equal.
func (*Pathname) Join ¶
Join appends one or more components left to right, matching Ruby's Pathname#join: each component follows the +/join append rule, so an absolute component resets the accumulated path to the root.
func (*Pathname) JoinStrings ¶
JoinStrings is Join accepting raw string components.
func (*Pathname) Plus ¶
Plus appends a path component, matching Ruby's Pathname#+ (and #/): an absolute argument resets to the root, otherwise a single "/" separates them.
func (*Pathname) PlusString ¶
PlusString is Plus accepting a raw string component, matching Ruby's Pathname#+ when handed a String (it wraps it in a Pathname first).
func (*Pathname) Relative ¶
Relative reports whether the path is relative, like Ruby's Pathname#relative?.
func (*Pathname) RelativePathFrom ¶
RelativePathFrom returns this path expressed relative to baseDirectory, using only the lexical components (no filesystem access), matching Ruby's Pathname#relative_path_from. Both paths are cleaned and split, a shared prefix is dropped, each remaining base component contributes a ".." and the remaining self components follow. Mixing an absolute path with a relative one — or a ".." that escapes a relative base — returns an *ArgumentError, as MRI raises.
func (*Pathname) Root ¶
Root reports whether the path is a root, i.e. one or more "/" and nothing else, like Ruby's Pathname#root?.
func (*Pathname) String ¶
String implements fmt.Stringer, returning the same value as ToS so a *Pathname formats as its path. (Ruby's #to_s.)
func (*Pathname) Sub ¶
Sub returns a Pathname whose path has the first occurrence of pattern replaced by repl, matching the lexical use of Ruby's Pathname#sub (a plain String#sub on the wrapped path, no filesystem access).
