pathmatch

package
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: May 11, 2023 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

* @Descripttion: * @version: * @Author: lfzxs@qq.com * @Date: 2023-04-18 15:04:23 * @LastEditors: lfzxs@qq.com * @LastEditTime: 2023-04-18 15:28:04

* @Descripttion: * @version: * @Author: lfzxs@qq.com * @Date: 2023-04-18 15:01:47 * @LastEditors: lfzxs@qq.com * @LastEditTime: 2023-04-18 15:33:24

Index

Constants

This section is empty.

Variables

View Source
var ErrBadPattern = path.ErrBadPattern

ErrBadPattern indicates a pattern was malformed.

Functions

func Match

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

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

pattern:
  { term }
term:
  '*'         matches any sequence of non-path-separators
  '/**/'      matches zero or more directories
  '?'         matches any single non-path-separator character
  '[' [ '^' '!' ] { character-range } ']'
              character class (must be non-empty)
              starting with `^` or `!` negates the class
  '{' { term } [ ',' { term } ... ] '}'
              alternatives
  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 returns true if `name` matches the file name `pattern`. `name` and `pattern` are split on forward slash (`/`) characters and may be relative or absolute.

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

A doublestar (`**`) should appear surrounded by path separators such as `/**/`. A mid-pattern doublestar (`**`) behaves like bash's globstar option: a pattern such as `path/to/**.txt` would return the same results as `path/to/*.txt`. The pattern you're looking for is `path/to/**/*.txt`.

Note: this is meant as a drop-in replacement for path.Match() which always uses '/' as the path separator. If you want to support systems which use a different path separator (such as Windows), what you want is PathMatch(). Alternatively, you can run filepath.ToSlash() on both pattern and name and then use this function.

func PathMatch

func PathMatch(pattern, name string) (bool, error)

PathMatch returns true if `name` matches the file name `pattern`. The difference between Match and PathMatch is that PathMatch will automatically use your system's path separator to split `name` and `pattern`. On systems where the path separator is `'\'`, escaping will be disabled.

Note: this is meant as a drop-in replacement for filepath.Match(). It assumes that both `pattern` and `name` are using the system's path separator. If you can't be sure of that, use filepath.ToSlash() on both `pattern` and `name`, and then use the Match() function instead.

func SplitPattern

func SplitPattern(p string) (base, pattern string)

SplitPattern is a utility function. Given a pattern, SplitPattern will return two strings: the first string is everything up to the last slash (`/`) that appears _before_ any unescaped "meta" characters (ie, `*?[{`). The second string is everything after that slash. For example, given the pattern:

../../path/to/meta*/**
             ^----------- split here

SplitPattern returns "../../path/to" and "meta*/**". This is useful for initializing os.DirFS() to call Glob() because Glob() will silently fail if your pattern includes `/./` or `/../`. For example:

base, pattern := SplitPattern("../../path/to/meta*/**")
fsys := os.DirFS(base)
matches, err := Glob(fsys, pattern)

If SplitPattern cannot find somewhere to split the pattern (for example, `meta*/**`), it will return "." and the unaltered pattern (`meta*/**` in this example).

Of course, it is your responsibility to decide if the returned base path is "safe" in the context of your application. Perhaps you could use Match() to validate against a list of approved base directories?

func ValidatePathPattern

func ValidatePathPattern(s string) bool

func ValidatePattern

func ValidatePattern(s string) bool

ValidatePattern Validate a pattern. Patterns are validated while they run in Match(), PathMatch(), and Glob(), so, you normally wouldn't need to call this. However, there are cases where this might be useful: for example, if your program allows a user to enter a pattern that you'll run at a later time, you might want to validate it.

ValidatePattern assumes your pattern uses '/' as the path separator.

Types

This section is empty.

Jump to

Keyboard shortcuts

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