Skip to content

Commit

Permalink
expand: resolve the two remaining TODOs
Browse files Browse the repository at this point in the history
The main point of hasGlob in older v2 versions was to avoid doing
globbing work for many more directories than needed, since we'd perform
globbing on filepath.Join($PWD, field).

However, newer versions glob only on the field relative to PWD, so we
already skip that extra work without the need for this piece of code.

As for the binary search, it can be implemented in just a few lines
thanks to sort.SearchStrings.
  • Loading branch information
mvdan committed Dec 19, 2018
1 parent 4b0ffa9 commit fc5a791
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 17 deletions.
11 changes: 5 additions & 6 deletions expand/environ.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,14 @@ func listEnvironWithUpper(upper bool, pairs ...string) Environ {
return listEnviron(list)
}

// listEnviron is a sorted list of "name=value" strings.
type listEnviron []string

func (l listEnviron) Get(name string) Variable {
// TODO: binary search
prefix := name + "="
for _, pair := range l {
if val := strings.TrimPrefix(pair, prefix); val != pair {
return Variable{Exported: true, Value: val}
}
i := sort.SearchStrings(l, prefix)
if i < len(l) && strings.HasPrefix(l[i], prefix) {
return Variable{Exported: true, Value: strings.TrimPrefix(l[i], prefix)}
}
return Variable{}
}
Expand All @@ -187,7 +186,7 @@ func (l listEnviron) Each(fn func(name string, vr Variable) bool) {
for _, pair := range l {
i := strings.IndexByte(pair, '=')
if i < 0 {
// can't happen; see above
// should never happen; see listEnvironWithUpper
panic("expand.listEnviron: did not expect malformed name-value pair: " + pair)
}
name, value := pair[:i], pair[i+1:]
Expand Down
11 changes: 0 additions & 11 deletions expand/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"os/user"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"

Expand Down Expand Up @@ -583,16 +582,6 @@ func findAllIndex(pattern, name string, n int) [][]int {
return rx.FindAllStringIndex(name, n)
}

// TODO: use this again to optimize globbing; see
// https://github.com/mvdan/sh/issues/213
func hasGlob(path string) bool {
magicChars := `*?[`
if runtime.GOOS != "windows" {
magicChars = `*?[\`
}
return strings.ContainsAny(path, magicChars)
}

var rxGlobStar = regexp.MustCompile(".*")

// pathJoin2 is a simpler version of filepath.Join without cleaning the result,
Expand Down

0 comments on commit fc5a791

Please sign in to comment.