-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added glob patterns to search file input. There are some improvements…
… that are still needed though
- Loading branch information
1 parent
6bba46d
commit 99e2600
Showing
5 changed files
with
278 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package libvore | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
func Max(x int, y int) int { | ||
if x < y { | ||
return y | ||
} | ||
return x | ||
} | ||
|
||
func Min(x int, y int) int { | ||
if x > y { | ||
return y | ||
} | ||
return x | ||
} | ||
|
||
func Window[T any](array []T, size int) [][]T { | ||
var results [][]T | ||
for idx, _ := range array { | ||
results = append(results, array[idx:Min(idx+size, len(array))]) | ||
} | ||
return results | ||
} | ||
|
||
func SplitKeep(target string, split string) []string { | ||
var results []string | ||
for { | ||
if len(target) == 0 { | ||
break | ||
} | ||
splitStart := strings.Index(target, split) | ||
if splitStart == 0 { | ||
target = target[len(split):] | ||
results = append(results, split) | ||
} else if splitStart == -1 { | ||
results = append(results, target) | ||
break | ||
} else { | ||
substr := target[0:splitStart] | ||
target = target[splitStart:] | ||
results = append(results, substr) | ||
} | ||
} | ||
return results | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package libvore | ||
|
||
import ( | ||
"reflect" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestMax1(t *testing.T) { | ||
max := Max(-1, 10) | ||
if max != 10 { | ||
t.Errorf("Max was supposed to be 10 but was %d", max) | ||
} | ||
} | ||
|
||
func TestMax2(t *testing.T) { | ||
max := Max(10, -3) | ||
if max != 10 { | ||
t.Errorf("Max was supposed to be 10 but was %d", max) | ||
} | ||
} | ||
|
||
func TestMax3(t *testing.T) { | ||
max := Max(-1, -5) | ||
if max != -1 { | ||
t.Errorf("Max was supposed to be -1 but was %d", max) | ||
} | ||
} | ||
|
||
func TestMax4(t *testing.T) { | ||
max := Max(20, 100) | ||
if max != 100 { | ||
t.Errorf("Max was supposed to be 100 but was %d", max) | ||
} | ||
} | ||
|
||
func TestMin1(t *testing.T) { | ||
max := Min(-1, 10) | ||
if max != -1 { | ||
t.Errorf("Min was supposed to be -1 but was %d", max) | ||
} | ||
} | ||
|
||
func TestMin2(t *testing.T) { | ||
max := Min(10, -3) | ||
if max != -3 { | ||
t.Errorf("Min was supposed to be -3 but was %d", max) | ||
} | ||
} | ||
|
||
func TestMin3(t *testing.T) { | ||
max := Min(-1, -5) | ||
if max != -5 { | ||
t.Errorf("Min was supposed to be -5 but was %d", max) | ||
} | ||
} | ||
|
||
func TestMin4(t *testing.T) { | ||
max := Min(20, 100) | ||
if max != 20 { | ||
t.Errorf("Min was supposed to be 20 but was %d", max) | ||
} | ||
} | ||
|
||
func TestSplitKeep(t *testing.T) { | ||
result := SplitKeep("abcaabc", "a") | ||
if !reflect.DeepEqual(result, []string{"a", "bc", "a", "a", "bc"}) { | ||
t.Errorf("SplitKeep was supposed to be [a, bc, a, a, bc] but was [%s]", strings.Join(result, ", ")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package libvore | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
) | ||
|
||
type PathEntryType int | ||
|
||
const ( | ||
Directory PathEntryType = iota | ||
WildcardDirectory | ||
File | ||
WildcardFile | ||
) | ||
|
||
type PathEntry struct { | ||
entryType PathEntryType | ||
value string | ||
} | ||
|
||
type Path struct { | ||
entries []PathEntry | ||
} | ||
|
||
func ParsePath(path string) *Path { | ||
var entries []PathEntry | ||
if path[0] == '/' { | ||
entries = append(entries, PathEntry{entryType: Directory, value: "/"}) | ||
path = path[1:] | ||
} | ||
splitPath := strings.Split(path, "/") | ||
for idx, pathPart := range splitPath { | ||
if idx == len(splitPath) { | ||
if strings.ContainsRune(pathPart, '*') { | ||
entries = append(entries, PathEntry{entryType: WildcardFile, value: pathPart}) | ||
} else { | ||
entries = append(entries, PathEntry{entryType: File, value: pathPart}) | ||
} | ||
} else { | ||
if strings.ContainsRune(pathPart, '*') { | ||
entries = append(entries, PathEntry{entryType: WildcardDirectory, value: pathPart}) | ||
} else { | ||
entries = append(entries, PathEntry{entryType: Directory, value: pathPart}) | ||
} | ||
} | ||
} | ||
return &Path{entries} | ||
} | ||
|
||
func pathMatches(target string, matches string) bool { | ||
if !strings.ContainsRune(matches, '*') { | ||
return target == matches | ||
} | ||
|
||
matchParts := Window(SplitKeep(matches, "*"), 2) | ||
|
||
result := true | ||
for _, part := range matchParts { | ||
if len(part) == 1 { | ||
if part[0] != "*" && target != part[0] { | ||
result = false | ||
} | ||
break | ||
} else if part[0] == "*" { | ||
splitStart := strings.Index(target, part[1]) | ||
if splitStart == -1 { | ||
target = "" | ||
} else { | ||
target = target[splitStart:] | ||
} | ||
} else if strings.HasPrefix(target, part[0]) { | ||
target = strings.TrimPrefix(target, part[0]) | ||
} else { | ||
result = false | ||
break | ||
} | ||
} | ||
return result | ||
} | ||
|
||
func directoryExists(entries []os.DirEntry, name string) bool { | ||
for _, e := range entries { | ||
if e.Name() == name { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func (path *Path) shrink() *Path { | ||
return &Path{entries: path.entries[1:]} | ||
} | ||
|
||
func (path *Path) GetFileList(currentDirectory string) []string { | ||
if len(path.entries) == 1 { | ||
entries, err := os.ReadDir(currentDirectory) | ||
if err != nil { | ||
return []string{} | ||
} | ||
|
||
var results []string | ||
for _, e := range entries { | ||
if !e.IsDir() && pathMatches(e.Name(), path.entries[0].value) { | ||
results = append(results, currentDirectory+"/"+e.Name()) | ||
} | ||
} | ||
return results | ||
} | ||
|
||
if path.entries[0].value == "/" { | ||
return path.shrink().GetFileList("/") | ||
} | ||
|
||
entries, err := os.ReadDir(currentDirectory) | ||
if err != nil { | ||
return []string{} | ||
} | ||
|
||
var results []string | ||
|
||
if strings.Trim(path.entries[0].value, "*") == "" { | ||
for _, e := range entries { | ||
if !e.IsDir() && pathMatches(e.Name(), path.entries[len(path.entries)-1].value) { | ||
results = append(results, currentDirectory+"/"+e.Name()) | ||
} | ||
} | ||
} | ||
|
||
if path.entries[0].entryType == WildcardDirectory { | ||
for _, e := range entries { | ||
if pathMatches(e.Name(), path.entries[0].value) { | ||
results = append(results, path.shrink().GetFileList(currentDirectory+"/"+e.Name())...) | ||
} | ||
} | ||
} else if path.entries[0].entryType == Directory && directoryExists(entries, path.entries[0].value) { | ||
results = append(results, path.shrink().GetFileList(currentDirectory+"/"+path.entries[0].value)...) | ||
} | ||
|
||
return results | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters