path

package module
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2024 License: MIT Imports: 4 Imported by: 9

README

path

GoDoc Build Status Coverage Status Go Report Card Issues

This enhances the standard path API with some extra functions. This API is intended to be a drop-in replacement; it merely calls through to the standard API where there is duplication.

There is also a type Path, which is a kind of string. Path provides a similar set of methods to the helper functions.

Please see the GoDoc for more.

Installation

go get -u github.com/rickb777/path

Status

This library has been in reliable production use for some time. Versioning follows the well-known semantic version pattern.

Licence

MIT

Documentation

Overview

Package path implements utility routines for manipulating slash-separated paths. This deliberately resembles the standard path API closely. The similarity is intentional and credit is due to the Go authors for their work.

This package may serve as a drop-in replacement for the standard path package. In addition to the functions available in the standard path package, there are extra functions for dividing paths.

There is also a type Path. This allows path strings to be manipulated using methods instead of helper functions. These methods follow a similar design, and also allow iteration through path segments.

This package should only be used for paths separated by forward slashes, such as the paths in URLs.

Index

Constants

This section is empty.

Variables

View Source
var ErrBadPattern = std.ErrBadPattern

ErrBadPattern indicates a globbing pattern was malformed.

Functions

func Base

func Base(path string) string

Base returns the last element of path. Trailing slashes are removed before extracting the last element. If the path is empty, Base returns ".". If the path consists entirely of slashes, Base returns "/".

func Clean

func Clean(path string) string

Clean returns the shortest path name equivalent to path by purely lexical processing. It applies the following rules iteratively until no further processing can be done:

  1. Replace multiple slashes with a single slash.
  2. Eliminate each . path name element (the current directory).
  3. Eliminate each inner .. path name element (the parent directory) along with the non-.. element that precedes it.
  4. Eliminate .. elements that begin a rooted path: that is, replace "/.." by "/" at the beginning of a path.

The returned path ends in a slash only if it is the root "/".

If the result of this process is an empty string, Clean returns the string ".".

See also Rob Pike, “Lexical File Names in Plan 9 or Getting Dot-Dot Right,” https://9p.io/sys/doc/lexnames.html

func Dir

func Dir(path string) string

Dir returns all but the last element of path, typically the path's directory. After dropping the final element using Split, the path is Cleaned and trailing slashes are removed. If the path is empty, Dir returns ".". If the path consists entirely of slashes followed by non-slash bytes, Dir returns a single slash. In any other case, the returned path does not end in a slash.

func Divide

func Divide(path string, nth int) (string, string)

Divide divides a path at the nth slash, not counting the leading slash if there is one.

The resulting pair (head, tail) always satisfy

head + tail = path

func Drop

func Drop(path string, unwanted int) string

Drop is a helper for Divide that returns the tail part only.

func Ext

func Ext(path string) string

Ext returns the file name extension used by path. The extension is the suffix beginning at the final dot in the final slash-separated element of path; it is empty if there is no dot.

func IsAbs

func IsAbs(path string) bool

IsAbs reports whether the path is absolute.

func Join

func Join(elem ...string) string

Join joins any number of path elements into a single path, adding a separating slash if necessary. The result is Cleaned; in particular, all empty strings are ignored.

func Match

func Match(pattern, name string) (matched bool, err error)

Match reports whether name matches the shell file name pattern. The pattern syntax is:

pattern:
	{ term }
term:
	'*'         matches any sequence of non-/ characters
	'?'         matches any single non-/ character
	'[' [ '^' ] { character-range } ']'
	            character class (must be non-empty)
	c           matches character c (c != '*', '?', '\\', '[')
	'\\' c      matches character c

character-range:
	c           matches character c (c != '\\', '-', ']')
	'\\' c      matches character c
	lo '-' hi   matches character c for lo <= c <= hi

Match requires pattern to match all of name, not just a substring. The only possible returned error is ErrBadPattern, when pattern is malformed.

func Split

func Split(path string) (dir, file string)

Split splits path immediately following the final slash, separating it into a directory and file name component. If there is no slash in path, Split returns an empty dir and file set to path. The returned values have the property that path = dir+file.

func SplitExt added in v1.1.0

func SplitExt(path string) (string, string)

SplitExt splits the file name from its extension. The extension is the suffix beginning at the final dot in the final slash-separated element of path; it is empty if there is no dot. The dot is included in the extension.

Everything prior to the last dot is returned as the first result.

func Take

func Take(path string, wanted int) string

Take is a helper for Divide that returns the head part only.

Types

type Path added in v0.8.0

type Path string

Path is just a string with specialised methods.

func Of added in v0.12.0

func Of(elem ...string) Path

Of joins any number of path elements to build a new path, adding a separating slashes as necessary. The result is Cleaned; in particular, all empty strings are ignored.

If the first non-blank elem has a leading slash, the path will also have a leading slash. It will not have a trailing slash.

func OfAny added in v0.14.0

func OfAny(elem ...interface{}) Path

OfAny joins any number of path elements to build a new path, adding a separating slashes as necessary. Each element is treated as either a string, or a Path, or some other type; in the latter case, it is formatted using fmt.Sprintf so the "%v" rules apply.

The result is Cleaned; in particular, all empty strings are ignored.

If the first non-blank elem has a leading slash, the path will also have a leading slash. It will not have a trailing slash.

func (Path) Append added in v0.8.0

func (path Path) Append(elem ...string) Path

Append joins some more segments to the end of the path. It adds separating slashes as necessary. The result is Cleaned; in particular, all empty strings are ignored.

func (Path) Base added in v0.8.0

func (path Path) Base() string

Base returns the last element of path. Trailing slashes are removed before extracting the last element. If the path is empty, Base returns ".". If the path consists entirely of slashes, Base returns "/".

func (Path) Clean added in v0.8.0

func (path Path) Clean() Path

Clean returns the shortest path name equivalent to path by purely lexical processing. It applies the following rules iteratively until no further processing can be done:

  1. Replace multiple slashes with a single slash.
  2. Eliminate each . path name element (the current directory).
  3. Eliminate each inner .. path name element (the parent directory) along with the non-.. element that precedes it.
  4. Eliminate .. elements that begin a rooted path: that is, replace "/.." by "/" at the beginning of a path.

The returned path ends in a slash only if it is the root "/".

If the result of this process is an empty string, Clean returns the string ".".

See also Rob Pike, “Lexical File Names in Plan 9 or Getting Dot-Dot Right,” https://9p.io/sys/doc/lexnames.html

func (Path) Dir added in v0.8.0

func (path Path) Dir() Path

Dir returns all but the last element of path, typically the path's directory. After dropping the final element using Split, the path is Cleaned and trailing slashes are removed. If the path is empty, Dir returns ".". If the path consists entirely of slashes followed by non-slash bytes, Dir returns a single slash. In any other case, the returned path does not end in a slash.

func (Path) Divide added in v0.8.0

func (path Path) Divide(nth int) (Path, Path)

Divide divides a path at the nth slash, not counting the leading slash if there is one.

The resulting pair (head, tail) always satisfy

head + tail = path

func (Path) Drop added in v0.8.0

func (path Path) Drop(unwanted int) Path

Drop is a helper for Divide that returns the tail part only.

func (Path) Ext added in v0.8.0

func (path Path) Ext() string

Ext returns the file name extension used by path. The extension is the suffix beginning at the final dot in the final slash-separated element of path; it is empty if there is no dot.

Unlike ExtOnly, the dot is included in the result.

func (Path) ExtOnly added in v0.15.0

func (path Path) ExtOnly() string

ExtOnly returns the file name extension used by path. The extension is the suffix beginning at the final dot in the final slash-separated element of path; it is empty if there is no dot.

Unlike Ext, the dot is not included in the result.

func (Path) HasPrefix added in v0.13.0

func (path Path) HasPrefix(other Path) bool

HasPrefix reports whether the path starts with a particular prefix.

func (Path) HasSuffix added in v0.13.0

func (path Path) HasSuffix(other Path) bool

HasSuffix reports whether the path ends with a particular suffix.

func (Path) IsAbs added in v0.8.0

func (path Path) IsAbs() bool

IsAbs reports whether the path is absolute.

func (Path) IsEmpty added in v0.8.0

func (path Path) IsEmpty() bool

IsEmpty returns true if the path is empty.

func (Path) Join added in v1.3.0

func (path Path) Join(p2 Path) Path

Join joins a segment to the end of the path. It adds separating slashes as necessary. The result is Cleaned; in particular, all empty strings are ignored.

func (Path) Next added in v0.8.0

func (path Path) Next() (string, Path)

Next returns the first segment (without any leading '/') and the rest. It can be used for iterating through the path segments; the end has been reached when the tail is empty (see IsEmpty).

func (Path) Prepend added in v0.11.0

func (path Path) Prepend(elem ...string) Path

Prepend joins some more segments to the beginning of the path. It adds separating slashes as necessary. The result is Cleaned; in particular, all empty strings are ignored.

func (*Path) Scan added in v0.10.0

func (path *Path) Scan(value interface{}) error

Scan parses some value. It implements sql.Scanner, https://golang.org/pkg/database/sql/#Scanner

func (Path) Segments added in v0.8.0

func (path Path) Segments() []string

Segments returns the path split into the parts between slashes. Any leading or trailing slash on the path is removed before the path is split, so there is no leading or trailing blank string in the result.

The root path "/" will return nil. A blank path will also return nil; this ensures that the Segments of the zero value of Path is a zero value of []string.

func (Path) Split added in v0.8.0

func (path Path) Split() (dir Path, file string)

Split splits path immediately following the final slash, separating it into a directory and file name component. If there is no slash in path, Split returns an empty dir and file set to path. The returned values have the property that path = dir+file.

func (Path) SplitExt added in v1.1.0

func (path Path) SplitExt() (Path, string)

SplitExt splits the file name from its extension. The extension is the suffix beginning at the final dot in the final slash-separated element of path; it is empty if there is no dot. The dot is included in the extension.

Everything prior to the last dot is returned as the first result.

func (Path) String added in v0.8.0

func (path Path) String() string

String simply converts the type to a string.

func (Path) Take added in v0.8.0

func (path Path) Take(wanted int) Path

Take is a helper for Divide that returns the head part only.

func (Path) Value added in v0.10.0

func (path Path) Value() (driver.Value, error)

Value converts the value to a string. It implements driver.Valuer, https://golang.org/pkg/database/sql/driver/#Valuer

Jump to

Keyboard shortcuts

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