@ -1,4 +1,4 @@
package main
package greg
import (
import (
"fmt"
"fmt"
@ -10,15 +10,15 @@ type Match []Group
// a Group represents a group. It contains the start index and end index of the match
// a Group represents a group. It contains the start index and end index of the match
type Group struct {
type Group struct {
s tartIdx int
S tartIdx int
e ndIdx int
E ndIdx int
}
}
func newMatch ( size int ) Match {
func newMatch ( size int ) Match {
toRet := make ( [ ] Group , size )
toRet := make ( [ ] Group , size )
for i := range toRet {
for i := range toRet {
toRet [ i ] . s tartIdx = - 1
toRet [ i ] . S tartIdx = - 1
toRet [ i ] . e ndIdx = - 1
toRet [ i ] . E ndIdx = - 1
}
}
return toRet
return toRet
}
}
@ -27,7 +27,7 @@ func newMatch(size int) Match {
func ( m Match ) numValidGroups ( ) int {
func ( m Match ) numValidGroups ( ) int {
numValid := 0
numValid := 0
for _ , g := range m {
for _ , g := range m {
if g . startIdx >= 0 && g . e ndIdx >= 0 {
if g . StartIdx >= 0 && g . E ndIdx >= 0 {
numValid ++
numValid ++
}
}
}
}
@ -35,7 +35,7 @@ func (m Match) numValidGroups() int {
}
}
// Returns a string containing the indices of all (valid) groups in the match
// Returns a string containing the indices of all (valid) groups in the match
func ( m Match ) t oString( ) string {
func ( m Match ) T oString( ) string {
var toRet string
var toRet string
for i , g := range m {
for i , g := range m {
if g . isValid ( ) {
if g . isValid ( ) {
@ -49,12 +49,12 @@ func (m Match) toString() string {
// Converts the Group into a string representation:
// Converts the Group into a string representation:
func ( idx Group ) toString ( ) string {
func ( idx Group ) toString ( ) string {
return fmt . Sprintf ( "%d\t%d" , idx . startIdx, idx . e ndIdx)
return fmt . Sprintf ( "%d\t%d" , idx . StartIdx, idx . E ndIdx)
}
}
// Returns whether a group contains valid indices
// Returns whether a group contains valid indices
func ( g Group ) isValid ( ) bool {
func ( g Group ) isValid ( ) bool {
return g . startIdx >= 0 && g . e ndIdx >= 0
return g . StartIdx >= 0 && g . E ndIdx >= 0
}
}
// takeZeroState takes the 0-state (if such a transition exists) for all states in the
// 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 )
copy ( s . threadGroups , state . threadGroups )
if s . groupBegin {
if s . groupBegin {
s . threadGroups [ s . groupNum ] . s tartIdx = idx
s . threadGroups [ s . groupNum ] . S tartIdx = idx
// openParenGroups = append(openParenGroups, s.groupNum)
// openParenGroups = append(openParenGroups, s.groupNum)
}
}
if s . groupEnd {
if s . groupEnd {
s . threadGroups [ s . groupNum ] . e ndIdx = idx
s . threadGroups [ s . groupNum ] . E ndIdx = idx
// closeParenGroups = append(closeParenGroups, s.groupNum)
// 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 {
func pruneIndices ( indices [ ] Match ) [ ] Match {
// First, sort the slice by the start indices
// First, sort the slice by the start indices
sort . Slice ( indices , func ( i , j int ) bool {
sort . Slice ( indices , func ( i , j int ) bool {
return indices [ i ] [ 0 ] . s tartIdx < indices [ j ] [ 0 ] . s tartIdx
return indices [ i ] [ 0 ] . S tartIdx < indices [ j ] [ 0 ] . S tartIdx
} )
} )
toRet := make ( [ ] Match , 0 , len ( indices ) )
toRet := make ( [ ] Match , 0 , len ( indices ) )
current := indices [ 0 ]
current := indices [ 0 ]
for _ , idx := range indices [ 1 : ] {
for _ , idx := range indices [ 1 : ] {
// idx doesn't overlap with current (starts after current ends), so add current to result
// idx doesn't overlap with current (starts after current ends), so add current to result
// and update the current.
// and update the current.
if idx [ 0 ] . startIdx >= current [ 0 ] . e ndIdx {
if idx [ 0 ] . StartIdx >= current [ 0 ] . E ndIdx {
toRet = append ( toRet , current )
toRet = append ( toRet , current )
current = idx
current = idx
} else if idx [ 0 ] . endIdx > current [ 0 ] . e ndIdx {
} else if idx [ 0 ] . EndIdx > current [ 0 ] . E ndIdx {
// idx overlaps, but it is longer, so update current
// idx overlaps, but it is longer, so update current
current = idx
current = idx
}
}
@ -147,7 +147,7 @@ func FindString(regex Reg, str string) string {
if err != nil {
if err != nil {
return ""
return ""
}
}
return str [ match [ 0 ] . startIdx: match [ 0 ] . e ndIdx]
return str [ match [ 0 ] . StartIdx: match [ 0 ] . E ndIdx]
}
}
// FindAllString is the 'all' version of FindString.
// 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 )
start . threadGroups = newMatch ( numGroups + 1 )
// Check if the start state begins a group - if so, add the start index to our list
// Check if the start state begins a group - if so, add the start index to our list
if start . groupBegin {
if start . groupBegin {
start . threadGroups [ start . groupNum ] . s tartIdx = i
start . threadGroups [ start . groupNum ] . S tartIdx = i
// tempIndices[start.groupNum].startIdx = i
// tempIndices[start.groupNum].startIdx = i
}
}
@ -356,10 +356,10 @@ func findAllMatchesHelper(start *State, str []rune, offset int, numGroups int) (
// i++
// i++
// }
// }
if tempIndices . numValidGroups ( ) > 0 && tempIndices [ 0 ] . isValid ( ) {
if tempIndices . numValidGroups ( ) > 0 && tempIndices [ 0 ] . isValid ( ) {
if tempIndices [ 0 ] . startIdx == tempIndices [ 0 ] . e ndIdx { // 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.
if tempIndices [ 0 ] . StartIdx == tempIndices [ 0 ] . E ndIdx { // 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 ] . e ndIdx + 1
return true , tempIndices , tempIndices [ 0 ] . E ndIdx + 1
} else {
} else {
return true , tempIndices , tempIndices [ 0 ] . e ndIdx
return true , tempIndices , tempIndices [ 0 ] . E ndIdx
}
}
}
}
return false , [ ] Group { } , startIdx
return false , [ ] Group { } , startIdx
@ -402,10 +402,10 @@ func findAllMatchesHelper(start *State, str []rune, offset int, numGroups int) (
}
}
if tempIndices . numValidGroups ( ) > 0 {
if tempIndices . numValidGroups ( ) > 0 {
if tempIndices [ 0 ] . startIdx == tempIndices [ 0 ] . e ndIdx { // 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.
if tempIndices [ 0 ] . StartIdx == tempIndices [ 0 ] . E ndIdx { // 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 ] . e ndIdx + 1
return true , tempIndices , tempIndices [ 0 ] . E ndIdx + 1
} else {
} else {
return true , tempIndices , tempIndices [ 0 ] . e ndIdx
return true , tempIndices , tempIndices [ 0 ] . E ndIdx
}
}
}
}
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.
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.