Skip to content

Commit

Permalink
Added glob patterns to search file input. There are some improvements…
Browse files Browse the repository at this point in the history
… that are still needed though
  • Loading branch information
jmeaster30 committed Jan 20, 2024
1 parent 6bba46d commit 99e2600
Show file tree
Hide file tree
Showing 5 changed files with 278 additions and 9 deletions.
49 changes: 49 additions & 0 deletions libvore/algorithm.go
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
}
70 changes: 70 additions & 0 deletions libvore/algorithm_test.go
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, ", "))
}
}
141 changes: 141 additions & 0 deletions libvore/path.go
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
}
1 change: 1 addition & 0 deletions libvore/vore.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ func (v *Vore) RunFiles(filenames []string, mode ReplaceMode, processFilenames b
actualFiles = append(actualFiles, fixedFilename)
}
for _, actualFilename := range actualFiles {
fmt.Printf("Processing file '%s'...\n", actualFilename)
var reader *VReader
if processFilenames {
reader = VReaderFromString(actualFilename)
Expand Down
26 changes: 17 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/jmeaster30/vore/libvore"
)

var replaceModeArg libvore.ReplaceMode = libvore.NEW
var replaceModeArg = libvore.NEW

func replaceMode(value string) error {
switch value {
Expand Down Expand Up @@ -59,7 +59,10 @@ func main() {
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
err = pprof.StartCPUProfile(f)
if err != nil {
log.Fatal(err)
}
defer pprof.StopCPUProfile()
}

Expand Down Expand Up @@ -88,21 +91,26 @@ func main() {
}

var vore *libvore.Vore
var comp_error error
var compError error
if len(source) != 0 {
vore, comp_error = libvore.CompileFile(source)
vore, compError = libvore.CompileFile(source)
} else {
vore, comp_error = libvore.Compile(command)
vore, compError = libvore.Compile(command)
}

if comp_error != nil {
fmt.Printf("%s\n", comp_error.Error())
os.Exit(1)
if compError != nil {
log.Fatal(compError)
}

//vore.PrintAST()
currentDir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}

searchFiles := libvore.ParsePath(*files_arg).GetFileList(currentDir)

results := vore.RunFiles([]string{*files_arg}, replaceModeArg, process_filenames)
results := vore.RunFiles(searchFiles, replaceModeArg, process_filenames)

if no_output { // skip all output
return
Expand Down

0 comments on commit 99e2600

Please sign in to comment.