mirror of
https://github.com/rclone/rclone.git
synced 2024-11-26 10:13:52 +08:00
filter: export GlobToRegexp #5164
This commit is contained in:
parent
5c646dff9a
commit
c142e3edcc
|
@ -254,7 +254,7 @@ func (f *Filter) addDirGlobs(Include bool, glob string) error {
|
|||
if dirGlob == "/" {
|
||||
continue
|
||||
}
|
||||
dirRe, err := globToRegexp(dirGlob, f.Opt.IgnoreCase)
|
||||
dirRe, err := GlobToRegexp(dirGlob, f.Opt.IgnoreCase)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -274,7 +274,7 @@ func (f *Filter) Add(Include bool, glob string) error {
|
|||
if strings.Contains(glob, "**") {
|
||||
isDirRule, isFileRule = true, true
|
||||
}
|
||||
re, err := globToRegexp(glob, f.Opt.IgnoreCase)
|
||||
re, err := GlobToRegexp(glob, f.Opt.IgnoreCase)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -10,10 +10,10 @@ import (
|
|||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// globToRegexp converts an rsync style glob to a regexp
|
||||
// GlobToRegexp converts an rsync style glob to a regexp
|
||||
//
|
||||
// documented in filtering.md
|
||||
func globToRegexp(glob string, ignoreCase bool) (*regexp.Regexp, error) {
|
||||
func GlobToRegexp(glob string, ignoreCase bool) (*regexp.Regexp, error) {
|
||||
var re bytes.Buffer
|
||||
if ignoreCase {
|
||||
_, _ = re.WriteString("(?i)")
|
||||
|
|
|
@ -42,7 +42,7 @@ func TestGlobToRegexp(t *testing.T) {
|
|||
{`a\\b`, `(^|/)a\\b$`, ``},
|
||||
} {
|
||||
for _, ignoreCase := range []bool{false, true} {
|
||||
gotRe, err := globToRegexp(test.in, ignoreCase)
|
||||
gotRe, err := GlobToRegexp(test.in, ignoreCase)
|
||||
if test.error == "" {
|
||||
prefix := ""
|
||||
if ignoreCase {
|
||||
|
@ -102,7 +102,7 @@ func TestGlobToDirGlobs(t *testing.T) {
|
|||
{"/sausage3**", []string{`/sausage3**/`, "/"}},
|
||||
{"/a/*.jpg", []string{`/a/`, "/"}},
|
||||
} {
|
||||
_, err := globToRegexp(test.in, false)
|
||||
_, err := GlobToRegexp(test.in, false)
|
||||
assert.NoError(t, err)
|
||||
got := globToDirGlobs(test.in)
|
||||
assert.Equal(t, test.want, got, test.in)
|
||||
|
|
Loading…
Reference in New Issue
Block a user