Skip to content

Commit

Permalink
Fixes pkg#188; eliminates duplicated core code
Browse files Browse the repository at this point in the history
match contained lots of cut-n-pasted code from filepath/match.go in the
core libraries. Particularly Match() and Join() were changed in such a
way that made them functionally equivalent to the versions in
path/match.go. This removes the duplicate code and just calls the path
versions.
  • Loading branch information
eikenb committed Jul 20, 2017
1 parent c325f9a commit 4f3e725
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 57 deletions.
6 changes: 3 additions & 3 deletions client_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1609,9 +1609,9 @@ func contains(vector []string, s string) bool {
var globTests = []struct {
pattern, result string
}{
{"match.go", "./match.go"},
{"mat?h.go", "./match.go"},
{"ma*ch.go", "./match.go"},
{"match.go", "match.go"},
{"mat?h.go", "match.go"},
{"ma*ch.go", "match.go"},
{"../*/match.go", "../sftp/match.go"},
}

Expand Down
58 changes: 4 additions & 54 deletions match.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package sftp

import (
"errors"
"path"
"strings"
"unicode/utf8"
)

// ErrBadPattern indicates a globbing pattern was malformed.
var ErrBadPattern = errors.New("syntax error in pattern")
var ErrBadPattern = path.ErrBadPattern

// Unix separator
const separator = "/"
Expand Down Expand Up @@ -36,48 +36,7 @@ const separator = "/"
//
//
func Match(pattern, name string) (matched bool, err error) {
Pattern:
for len(pattern) > 0 {
var star bool
var chunk string
star, chunk, pattern = scanChunk(pattern)
if star && chunk == "" {
// Trailing * matches rest of string unless it has a /.
return !strings.Contains(name, separator), nil
}
// Look for match at current position.
t, ok, err := matchChunk(chunk, name)
// if we're the last chunk, make sure we've exhausted the name
// otherwise we'll give a false result even if we could still match
// using the star
if ok && (len(t) == 0 || len(pattern) > 0) {
name = t
continue
}
if err != nil {
return false, err
}
if star {
// Look for match skipping i+1 bytes.
// Cannot skip /.
for i := 0; i < len(name) && !isPathSeparator(name[i]); i++ {
t, ok, err := matchChunk(chunk, name[i+1:])
if ok {
// if we're the last chunk, make sure we exhausted the name
if len(pattern) == 0 && len(t) > 0 {
continue
}
name = t
continue Pattern
}
if err != nil {
return false, err
}
}
}
return false, nil
}
return len(name) == 0, nil
return path.Match(pattern, name)
}

// detect if byte(char) is path separator
Expand Down Expand Up @@ -325,16 +284,7 @@ func (c *Client) glob(dir, pattern string, matches []string) (m []string, e erro
// a Separator if necessary.
// all empty strings are ignored.
func Join(elem ...string) string {
return join(elem)
}
func join(elem []string) string {
// If there's a bug here, fix the logic in ./path_plan9.go too.
for i, e := range elem {
if e != "" {
return strings.Join(elem[i:], string(separator))
}
}
return ""
return path.Join(elem...)
}

// hasMeta reports whether path contains any of the magic characters
Expand Down

0 comments on commit 4f3e725

Please sign in to comment.