tile38/internal/glob/glob.go

97 lines
1.9 KiB
Go
Raw Permalink Normal View History

2016-07-12 21:48:40 +03:00
package glob
2016-11-02 19:15:11 +03:00
import "strings"
// Glob structure for simple string matching
2016-07-12 21:48:40 +03:00
type Glob struct {
Pattern string
Desc bool
Limits []string
IsGlob bool
}
// Match returns true when string matches pattern. Returns an error when the
// pattern is invalid.
func Match(pattern, str string) (matched bool, err error) {
return wildcardMatch(pattern, str)
2016-07-12 21:48:40 +03:00
}
// IsGlob returns true when the pattern is a valid glob
2016-07-12 21:48:40 +03:00
func IsGlob(pattern string) bool {
for i := 0; i < len(pattern); i++ {
switch pattern[i] {
case '[', '*', '?':
_, err := Match(pattern, "whatever")
return err == nil
}
}
return false
}
// Parse returns a glob structure from the pattern.
2016-07-12 21:48:40 +03:00
func Parse(pattern string, desc bool) *Glob {
g := &Glob{Pattern: pattern, Desc: desc, Limits: []string{"", ""}}
2016-11-02 19:15:11 +03:00
if strings.HasPrefix(pattern, "*") {
2016-07-12 21:48:40 +03:00
g.IsGlob = true
return g
}
if pattern == "" {
g.IsGlob = false
return g
}
n := 0
isGlob := false
outer:
for i := 0; i < len(pattern); i++ {
switch pattern[i] {
case '[', '*', '?':
_, err := Match(pattern, "whatever")
if err == nil {
isGlob = true
}
break outer
}
n++
}
if n == 0 {
g.Limits = []string{pattern, pattern}
g.IsGlob = false
return g
}
var a, b string
if desc {
a = pattern[:n]
b = a
if b[n-1] == 0x00 {
for len(b) > 0 && b[len(b)-1] == 0x00 {
if len(b) > 1 {
if b[len(b)-2] == 0x00 {
b = b[:len(b)-1]
} else {
b = string(append([]byte(b[:len(b)-2]), b[len(b)-2]-1, 0xFF))
}
} else {
b = ""
}
}
} else {
b = string(append([]byte(b[:n-1]), b[n-1]-1))
}
if a[n-1] == 0xFF {
a = string(append([]byte(a), 0x00))
} else {
a = string(append([]byte(a[:n-1]), a[n-1]+1))
}
} else {
a = pattern[:n]
if a[n-1] == 0xFF {
b = string(append([]byte(a), 0x00))
} else {
b = string(append([]byte(a[:n-1]), a[n-1]+1))
}
}
g.Limits = []string{a, b}
g.IsGlob = isGlob
return g
}