mirror of
https://github.com/rclone/rclone.git
synced 2024-11-22 16:34:30 +01: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 == "/" {
|
if dirGlob == "/" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
dirRe, err := globToRegexp(dirGlob, f.Opt.IgnoreCase)
|
dirRe, err := GlobToRegexp(dirGlob, f.Opt.IgnoreCase)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -274,7 +274,7 @@ func (f *Filter) Add(Include bool, glob string) error {
|
|||||||
if strings.Contains(glob, "**") {
|
if strings.Contains(glob, "**") {
|
||||||
isDirRule, isFileRule = true, true
|
isDirRule, isFileRule = true, true
|
||||||
}
|
}
|
||||||
re, err := globToRegexp(glob, f.Opt.IgnoreCase)
|
re, err := GlobToRegexp(glob, f.Opt.IgnoreCase)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -10,10 +10,10 @@ import (
|
|||||||
"github.com/pkg/errors"
|
"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
|
// 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
|
var re bytes.Buffer
|
||||||
if ignoreCase {
|
if ignoreCase {
|
||||||
_, _ = re.WriteString("(?i)")
|
_, _ = re.WriteString("(?i)")
|
||||||
|
@ -42,7 +42,7 @@ func TestGlobToRegexp(t *testing.T) {
|
|||||||
{`a\\b`, `(^|/)a\\b$`, ``},
|
{`a\\b`, `(^|/)a\\b$`, ``},
|
||||||
} {
|
} {
|
||||||
for _, ignoreCase := range []bool{false, true} {
|
for _, ignoreCase := range []bool{false, true} {
|
||||||
gotRe, err := globToRegexp(test.in, ignoreCase)
|
gotRe, err := GlobToRegexp(test.in, ignoreCase)
|
||||||
if test.error == "" {
|
if test.error == "" {
|
||||||
prefix := ""
|
prefix := ""
|
||||||
if ignoreCase {
|
if ignoreCase {
|
||||||
@ -102,7 +102,7 @@ func TestGlobToDirGlobs(t *testing.T) {
|
|||||||
{"/sausage3**", []string{`/sausage3**/`, "/"}},
|
{"/sausage3**", []string{`/sausage3**/`, "/"}},
|
||||||
{"/a/*.jpg", []string{`/a/`, "/"}},
|
{"/a/*.jpg", []string{`/a/`, "/"}},
|
||||||
} {
|
} {
|
||||||
_, err := globToRegexp(test.in, false)
|
_, err := GlobToRegexp(test.in, false)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
got := globToDirGlobs(test.in)
|
got := globToDirGlobs(test.in)
|
||||||
assert.Equal(t, test.want, got, test.in)
|
assert.Equal(t, test.want, got, test.in)
|
||||||
|
Loading…
Reference in New Issue
Block a user