118 lines
2 KiB
Go
118 lines
2 KiB
Go
package fs
|
|
|
|
import (
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/spf13/cast"
|
|
)
|
|
|
|
func (f Files) FilterByWildcard() Files {
|
|
var result Files
|
|
|
|
for i := len(f) - 1; i >= 0; i-- {
|
|
result = append(result, f[i])
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
func (f Files) FilterByRange1(from, to int) Files {
|
|
var result Files
|
|
size := len(f)
|
|
|
|
if from > to {
|
|
for i := from; i >= to; i-- {
|
|
result = append(result, f[size-i])
|
|
}
|
|
} else {
|
|
for i := from; i <= to; i++ {
|
|
result = append(result, f[size-i])
|
|
}
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
func (f Files) FilterByRange2(from int) Files {
|
|
var result Files
|
|
size := len(f)
|
|
|
|
for i := from; i >= 1; i-- {
|
|
result = append(result, f[size-i])
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
func (f Files) FilterByRange3(from int) Files {
|
|
var result Files
|
|
size := len(f)
|
|
|
|
for i := from; i <= size; i++ {
|
|
result = append(result, f[size-i])
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
func (f Files) FilterByWords(words []string) Files {
|
|
var result Files
|
|
size := len(f)
|
|
|
|
for _, word := range words {
|
|
isInt, _ := regexp.MatchString("^[0-9]+$", word)
|
|
|
|
if isInt {
|
|
value, _ := strconv.Atoi(word)
|
|
result = append(result, f[size-value])
|
|
}
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
func (f Files) Select(input string) Files {
|
|
switch input {
|
|
case "*":
|
|
case "*+":
|
|
return f.FilterByWildcard()
|
|
case "*-":
|
|
return f
|
|
}
|
|
|
|
range1Regex := "^([0-9]+)-([0-9]+)$"
|
|
range2Regex := "^([0-9]+)-$"
|
|
range3Regex := "^([0-9]+)\\+$"
|
|
|
|
isRange1, _ := regexp.MatchString(range1Regex, input)
|
|
isRange2, _ := regexp.MatchString(range2Regex, input)
|
|
isRange3, _ := regexp.MatchString(range3Regex, input)
|
|
|
|
if isRange1 {
|
|
regex, _ := regexp.Compile(range1Regex)
|
|
data := regex.FindStringSubmatch(input)
|
|
|
|
return f.FilterByRange1(
|
|
cast.ToInt(data[1]),
|
|
cast.ToInt(data[2]),
|
|
)
|
|
}
|
|
|
|
if isRange2 {
|
|
regex, _ := regexp.Compile(range2Regex)
|
|
data := regex.FindStringSubmatch(input)
|
|
|
|
return f.FilterByRange2(cast.ToInt(data[1]))
|
|
}
|
|
|
|
if isRange3 {
|
|
regex, _ := regexp.Compile(range3Regex)
|
|
data := regex.FindStringSubmatch(input)
|
|
|
|
return f.FilterByRange3(cast.ToInt(data[1]))
|
|
}
|
|
|
|
return f.FilterByWords(strings.Fields(input))
|
|
}
|