pathhelper

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2022 License: Apache-2.0 Imports: 9 Imported by: 2

Documentation

Overview

Package pathhelper

Index

Constants

View Source
const (
	WindowsSlash = '\\'
	UnixSlash    = '/'
)
View Source
const (
	WindowsPathListSeparator = ';'
	UnixPathListSeparator    = ':'
)

Variables

This section is empty.

Functions

func AbsWindowsPath

func AbsWindowsPath(
	cwd, path string,
	getFHSPath func(p string) (string, error),
) (string, error)

AbsWindowsPath returns absolute path of path with custom cwd cwd SHOULD be a windows absolute path path SHOULD be a relative path (not starting with `[a-zA-Z]:` or `\\`)

It tries to handle three different styles all at once:

  • windows native style:
  • `foo\bar` and `foo/bar` as path relative
  • `\foo` and `/foo` as driver relative
  • cygpath style absolute path (`/cygdrive/c`)
  • golang io/fs style absolute path for windows (`/[a-zA-Z]/`, e.g. /c/foo)

func Clean

func Clean(path string, sep byte) 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 Separator elements with a single one.
  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, assuming Separator is '/'.

The returned path ends in a slash only if it represents a root directory, such as "/" on Unix or `C:\` on Windows.

Finally, any occurrences of slash are replaced by Separator.

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 CleanUnixPath

func CleanUnixPath(p string) string

func CleanWindowsPath

func CleanWindowsPath(p string) string

func ConvertFSPathToWindowsPath

func ConvertFSPathToWindowsPath(defaultVolumeName, path string) string

ConvertFSPathToWindowsPath converts paths like `/c/foo` to absolute windows path `c:/foo` defaultVolumeName is used to resolve path as driver relative path, it's usually obtained by calling filepath.VolumeName, and its value is like `c:`, `\\some-host\path`

func EvalLink(linkFile, linkTo string) string

EvalLink returns lintTo's actual path from the point view of linkFile usually used in archive file or non actual os filesystem

For unix style path only

func FromSlash

func FromSlash(path string, sep byte) string

FromSlash returns the result of replacing each slash ('/') character in path with a separator character. Multiple slashes are replaced by multiple separators.

func IsReservedWindowsName

func IsReservedWindowsName(name string) bool

IsReservedWindowsName returns truen if the name is reserved in windows ref: https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file#naming-conventions

func IsReservedWindowsPathChar

func IsReservedWindowsPathChar(c rune) bool

IsReservedWindowsPathChar checks whether c is a reserved character for windows path ref: https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file#naming-conventions

func IsUnixPathListSeparator

func IsUnixPathListSeparator(c byte) bool

IsUnixPathListSeparator returns true when c is colon

func IsUnixSlash

func IsUnixSlash(c byte) bool

IsUnixSlash returns true when c is slash

func IsWindowsAbs

func IsWindowsAbs(path string) (b bool)

IsWindowsAbs reports whether the path is absolute on windows the path is considered absolute as following cases - reserved windows names (e.g. COM1) - starts with `\\<some-host>\<share>` (UNC path) - starts with `C:\` (driver absolute path) - starts with `\` or `/`

func IsWindowsDiskDesignatorChar

func IsWindowsDiskDesignatorChar(c byte) bool

IsWindowsDiskDesignatorChar returns true when c matches [a-zA-Z]

func IsWindowsPathListSeparator

func IsWindowsPathListSeparator(c byte) bool

IsWindowsPathListSeparator returns true when c is colon or semi-colon

func IsWindowsSlash

func IsWindowsSlash(c byte) bool

IsWindowsSlash returns true when c is slash or back slash

func JoinUnixPath

func JoinUnixPath(elem ...string) string

func JoinWindowsPath

func JoinWindowsPath(elem ...string) string

func LCPP added in v0.12.0

func LCPP(paths []string) string

LCPP returns the longest common path prefix among the slash paths.

func SplitList

func SplitList(
	path string,
	colonSep bool,
	semiColonSep bool,
	isSlash CharMatchFunc,
) []string

SplitList is like filepath.SplitList, but caller controls whether take colon and/or semi-colon as path separators

it also handles double-quoted path segments

NOTE for windows path lists:

behavior differences from win32:
- when colonIsPathSeparator is true, "C:tmp.txt" will be splited into `C` and `tmp.txt`
  instead of keeping it as like win32's behavior: a file named "tmp.txt" in the current directory on drive C

NOTE for unix style path list:

when colonIsPathSeparator is true (most of the case)
`[a-zA-Z]:/` in middle of the path list followd by an absolute path will be joined with next path segment
	e.g. `/root/bin:c:/foo` becomes `/root/bin`, `c:/foo`
so it's preferable to avoid single character relative path in the path list

func ToSlash added in v0.10.0

func ToSlash(buf []byte, s string) (ret string, newBuf []byte)

ToSlash replaces all backslashes to slashes in s as ret

unlike ToSlash funcs from std path/filepath package, this function always does the replacement no matter what platform currently running on

the returned newBuf should be used for following calls if both "len(s) > len(buf)" and "s contains backslash" are true

Types

type CharMatchFunc

type CharMatchFunc func(byte) bool

Jump to

Keyboard shortcuts

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