From 1a2f1b7ca9970295e2fe141b1065bfd4cf916bd5 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Thu, 30 Jan 2025 08:56:03 -0500 Subject: [PATCH] Restructured code into 'cmd' module with CLI and 'greg' module with regex library; export necessary struct fields and methods --- cmd/helpers.go | 0 main.go => cmd/main.go | 8 ++-- unique_array.go => cmd/unique_array.go | 0 go.mod | 2 +- compile.go => greg/compile.go | 2 +- matching.go => greg/matching.go | 44 +++++++++---------- misc.go => greg/misc.go | 2 +- nfa.go => greg/nfa.go | 2 +- .../noteOnPCREBackreferences.txt | 0 postfixNode.go => greg/postfixNode.go | 2 +- range2regex.go => greg/range2regex.go | 2 +- re_test.go => greg/re_test.go | 4 +- re_tests.py => greg/re_tests.py | 0 re_tests_uniq.py => greg/re_tests_uniq.py | 0 sliceQueue.go => greg/sliceQueue.go | 2 +- stateContents.go => greg/stateContents.go | 2 +- todo.txt => greg/todo.txt | 0 17 files changed, 37 insertions(+), 35 deletions(-) create mode 100644 cmd/helpers.go rename main.go => cmd/main.go (96%) rename unique_array.go => cmd/unique_array.go (100%) rename compile.go => greg/compile.go (99%) rename matching.go => greg/matching.go (93%) rename misc.go => greg/misc.go (99%) rename nfa.go => greg/nfa.go (99%) rename noteOnPCREBackreferences.txt => greg/noteOnPCREBackreferences.txt (100%) rename postfixNode.go => greg/postfixNode.go (99%) rename range2regex.go => greg/range2regex.go (99%) rename re_test.go => greg/re_test.go (99%) rename re_tests.py => greg/re_tests.py (100%) rename re_tests_uniq.py => greg/re_tests_uniq.py (100%) rename sliceQueue.go => greg/sliceQueue.go (97%) rename stateContents.go => greg/stateContents.go (97%) rename todo.txt => greg/todo.txt (100%) diff --git a/cmd/helpers.go b/cmd/helpers.go new file mode 100644 index 0000000..e69de29 diff --git a/main.go b/cmd/main.go similarity index 96% rename from main.go rename to cmd/main.go index c8c5f8b..ed4058b 100644 --- a/main.go +++ b/cmd/main.go @@ -8,11 +8,13 @@ import ( "os" "github.com/fatih/color" + + "gitea.twomorecents.org/Rockingcool/kg/greg" ) func main() { // Flags for the regex Compile function - flagsToCompile := make([]ReFlag, 0) + flagsToCompile := make([]greg.ReFlag, 0) invertFlag := flag.Bool("v", false, "Invert match.") // This flag has two 'modes': @@ -29,10 +31,10 @@ func main() { // These flags have to be passed to the Compile function if *multiLineFlag { - flagsToCompile = append(flagsToCompile, RE_MULTILINE, RE_SINGLE_LINE) + flagsToCompile = append(flagsToCompile, greg.RE_MULTILINE, greg.RE_SINGLE_LINE) } if *caseInsensitiveFlag { - flagsToCompile = append(flagsToCompile, RE_CASE_INSENSITIVE) + flagsToCompile = append(flagsToCompile, greg.RE_CASE_INSENSITIVE) } // -l and -o are mutually exclusive: -o overrides -l diff --git a/unique_array.go b/cmd/unique_array.go similarity index 100% rename from unique_array.go rename to cmd/unique_array.go diff --git a/go.mod b/go.mod index 2a5ad00..194bce2 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module re +module gitea.twomorecents.org/Rockingcool/kg go 1.23.1 diff --git a/compile.go b/greg/compile.go similarity index 99% rename from compile.go rename to greg/compile.go index 9663f6d..4bc19ca 100644 --- a/compile.go +++ b/greg/compile.go @@ -1,4 +1,4 @@ -package main +package greg import ( "fmt" diff --git a/matching.go b/greg/matching.go similarity index 93% rename from matching.go rename to greg/matching.go index 42da160..d73108c 100644 --- a/matching.go +++ b/greg/matching.go @@ -1,4 +1,4 @@ -package main +package greg import ( "fmt" @@ -10,15 +10,15 @@ type Match []Group // a Group represents a group. It contains the start index and end index of the match type Group struct { - startIdx int - endIdx int + StartIdx int + EndIdx int } func newMatch(size int) Match { toRet := make([]Group, size) for i := range toRet { - toRet[i].startIdx = -1 - toRet[i].endIdx = -1 + toRet[i].StartIdx = -1 + toRet[i].EndIdx = -1 } return toRet } @@ -27,7 +27,7 @@ func newMatch(size int) Match { func (m Match) numValidGroups() int { numValid := 0 for _, g := range m { - if g.startIdx >= 0 && g.endIdx >= 0 { + if g.StartIdx >= 0 && g.EndIdx >= 0 { numValid++ } } @@ -35,7 +35,7 @@ func (m Match) numValidGroups() int { } // Returns a string containing the indices of all (valid) groups in the match -func (m Match) toString() string { +func (m Match) ToString() string { var toRet string for i, g := range m { if g.isValid() { @@ -49,12 +49,12 @@ func (m Match) toString() string { // Converts the Group into a string representation: func (idx Group) toString() string { - return fmt.Sprintf("%d\t%d", idx.startIdx, idx.endIdx) + return fmt.Sprintf("%d\t%d", idx.StartIdx, idx.EndIdx) } // Returns whether a group contains valid indices func (g Group) isValid() bool { - return g.startIdx >= 0 && g.endIdx >= 0 + return g.StartIdx >= 0 && g.EndIdx >= 0 } // takeZeroState takes the 0-state (if such a transition exists) for all states in the @@ -70,11 +70,11 @@ func takeZeroState(states []*State, numGroups int, idx int) (rtv []*State, isZer } copy(s.threadGroups, state.threadGroups) if s.groupBegin { - s.threadGroups[s.groupNum].startIdx = idx + s.threadGroups[s.groupNum].StartIdx = idx // openParenGroups = append(openParenGroups, s.groupNum) } if s.groupEnd { - s.threadGroups[s.groupNum].endIdx = idx + s.threadGroups[s.groupNum].EndIdx = idx // closeParenGroups = append(closeParenGroups, s.groupNum) } } @@ -118,17 +118,17 @@ func zeroMatchPossible(str []rune, idx int, numGroups int, states ...*State) boo func pruneIndices(indices []Match) []Match { // First, sort the slice by the start indices sort.Slice(indices, func(i, j int) bool { - return indices[i][0].startIdx < indices[j][0].startIdx + return indices[i][0].StartIdx < indices[j][0].StartIdx }) toRet := make([]Match, 0, len(indices)) current := indices[0] for _, idx := range indices[1:] { // idx doesn't overlap with current (starts after current ends), so add current to result // and update the current. - if idx[0].startIdx >= current[0].endIdx { + if idx[0].StartIdx >= current[0].EndIdx { toRet = append(toRet, current) current = idx - } else if idx[0].endIdx > current[0].endIdx { + } else if idx[0].EndIdx > current[0].EndIdx { // idx overlaps, but it is longer, so update current current = idx } @@ -147,7 +147,7 @@ func FindString(regex Reg, str string) string { if err != nil { return "" } - return str[match[0].startIdx:match[0].endIdx] + return str[match[0].StartIdx:match[0].EndIdx] } // FindAllString is the 'all' version of FindString. @@ -247,7 +247,7 @@ func findAllMatchesHelper(start *State, str []rune, offset int, numGroups int) ( start.threadGroups = newMatch(numGroups + 1) // Check if the start state begins a group - if so, add the start index to our list if start.groupBegin { - start.threadGroups[start.groupNum].startIdx = i + start.threadGroups[start.groupNum].StartIdx = i // tempIndices[start.groupNum].startIdx = i } @@ -356,10 +356,10 @@ func findAllMatchesHelper(start *State, str []rune, offset int, numGroups int) ( // i++ // } if tempIndices.numValidGroups() > 0 && tempIndices[0].isValid() { - if tempIndices[0].startIdx == tempIndices[0].endIdx { // If we have a zero-length match, we have to shift the index at which we start. Otherwise we keep looking at the same paert of the string over and over. - return true, tempIndices, tempIndices[0].endIdx + 1 + if tempIndices[0].StartIdx == tempIndices[0].EndIdx { // If we have a zero-length match, we have to shift the index at which we start. Otherwise we keep looking at the same paert of the string over and over. + return true, tempIndices, tempIndices[0].EndIdx + 1 } else { - return true, tempIndices, tempIndices[0].endIdx + return true, tempIndices, tempIndices[0].EndIdx } } return false, []Group{}, startIdx @@ -402,10 +402,10 @@ func findAllMatchesHelper(start *State, str []rune, offset int, numGroups int) ( } if tempIndices.numValidGroups() > 0 { - if tempIndices[0].startIdx == tempIndices[0].endIdx { // If we have a zero-length match, we have to shift the index at which we start. Otherwise we keep looking at the same paert of the string over and over. - return true, tempIndices, tempIndices[0].endIdx + 1 + if tempIndices[0].StartIdx == tempIndices[0].EndIdx { // If we have a zero-length match, we have to shift the index at which we start. Otherwise we keep looking at the same paert of the string over and over. + return true, tempIndices, tempIndices[0].EndIdx + 1 } else { - return true, tempIndices, tempIndices[0].endIdx + return true, tempIndices, tempIndices[0].EndIdx } } if startIdx == startingFrom { // Increment starting index if we haven't moved in the string. Prevents us from matching the same part of the string over and over. diff --git a/misc.go b/greg/misc.go similarity index 99% rename from misc.go rename to greg/misc.go index 96d43a1..464c062 100644 --- a/misc.go +++ b/greg/misc.go @@ -1,4 +1,4 @@ -package main +package greg import ( "slices" diff --git a/nfa.go b/greg/nfa.go similarity index 99% rename from nfa.go rename to greg/nfa.go index be4cd10..a3dd632 100644 --- a/nfa.go +++ b/greg/nfa.go @@ -1,4 +1,4 @@ -package main +package greg import ( "fmt" diff --git a/noteOnPCREBackreferences.txt b/greg/noteOnPCREBackreferences.txt similarity index 100% rename from noteOnPCREBackreferences.txt rename to greg/noteOnPCREBackreferences.txt diff --git a/postfixNode.go b/greg/postfixNode.go similarity index 99% rename from postfixNode.go rename to greg/postfixNode.go index 402cecf..9e715e8 100644 --- a/postfixNode.go +++ b/greg/postfixNode.go @@ -1,4 +1,4 @@ -package main +package greg import "fmt" diff --git a/range2regex.go b/greg/range2regex.go similarity index 99% rename from range2regex.go rename to greg/range2regex.go index 06b6dbf..39bda4d 100644 --- a/range2regex.go +++ b/greg/range2regex.go @@ -1,4 +1,4 @@ -package main +package greg import ( "fmt" diff --git a/re_test.go b/greg/re_test.go similarity index 99% rename from re_test.go rename to greg/re_test.go index d94c552..900482b 100644 --- a/re_test.go +++ b/greg/re_test.go @@ -1,4 +1,4 @@ -package main +package greg import ( "fmt" @@ -703,7 +703,7 @@ func TestFindString(t *testing.T) { t.Errorf("Expected no match got %v\n", foundString) } } else { - expectedString := test.str[test.result[0].startIdx:test.result[0].endIdx] + expectedString := test.str[test.result[0].StartIdx:test.result[0].EndIdx] if foundString != expectedString { t.Errorf("Wanted %v Got %v\n", expectedString, foundString) } diff --git a/re_tests.py b/greg/re_tests.py similarity index 100% rename from re_tests.py rename to greg/re_tests.py diff --git a/re_tests_uniq.py b/greg/re_tests_uniq.py similarity index 100% rename from re_tests_uniq.py rename to greg/re_tests_uniq.py diff --git a/sliceQueue.go b/greg/sliceQueue.go similarity index 97% rename from sliceQueue.go rename to greg/sliceQueue.go index ecb32f6..719fd7f 100644 --- a/sliceQueue.go +++ b/greg/sliceQueue.go @@ -1,4 +1,4 @@ -package main +package greg import "errors" diff --git a/stateContents.go b/greg/stateContents.go similarity index 97% rename from stateContents.go rename to greg/stateContents.go index ecf3887..371ba0a 100644 --- a/stateContents.go +++ b/greg/stateContents.go @@ -1,4 +1,4 @@ -package main +package greg type stateContents []int // Represents the contents of the current state - character classes can have multiple contents, which is why it is represented as a slice diff --git a/todo.txt b/greg/todo.txt similarity index 100% rename from todo.txt rename to greg/todo.txt