diff --git a/regex/compile.go b/regex/compile.go index f16457b..6f26bed 100644 --- a/regex/compile.go +++ b/regex/compile.go @@ -871,30 +871,30 @@ func thompson(re []postfixNode) (Reg, error) { if c.lookaroundDir == 0 || c.lookaroundSign == 0 { switch c.contents[0] { case '^': - state.assert = SOS + state.assert = sosAssert case '$': - state.assert = EOS + state.assert = eosAssert case 'b': - state.assert = WBOUND + state.assert = wboundAssert case 'B': - state.assert = NONWBOUND + state.assert = nonwboundAssert } } else { // Lookaround state.lookaroundRegex = string(c.contents) if c.lookaroundDir == lookahead { if c.lookaroundSign == positive { - state.assert = PLA + state.assert = plaAssert } if c.lookaroundSign == negative { - state.assert = NLA + state.assert = nlaAssert } } if c.lookaroundDir == lookbehind { if c.lookaroundSign == positive { - state.assert = PLB + state.assert = plbAssert } if c.lookaroundSign == negative { - state.assert = NLB + state.assert = nlbAssert } } tmpRe, err := shuntingYard(state.lookaroundRegex) @@ -919,7 +919,7 @@ func thompson(re []postfixNode) (Reg, error) { } if c.nodetype == lparenNode || c.nodetype == rparenNode { s := &State{} - s.assert = NONE + s.assert = noneAssert s.content = newContents(EPSILON) s.isEmpty = true s.output = make([]*State, 0) diff --git a/regex/matching.go b/regex/matching.go index 264610f..f7f83ca 100644 --- a/regex/matching.go +++ b/regex/matching.go @@ -107,7 +107,7 @@ func zeroMatchPossible(str []rune, idx int, numGroups int, states ...*State) boo } } for _, state := range tempstates { - if state.isEmpty && (state.assert == NONE || state.checkAssertion(str, idx)) && state.isLast { + if state.isEmpty && (state.assert == noneAssert || state.checkAssertion(str, idx)) && state.isLast { return true } } @@ -228,7 +228,7 @@ func findAllMatchesHelper(start *State, str []rune, offset int, numGroups int) ( // If the first state is an assertion, makes sure the assertion // is true before we do _anything_ else. - if start.assert != NONE { + if start.assert != noneAssert { if start.checkAssertion(str, offset) == false { i++ return false, []Group{}, i @@ -329,7 +329,7 @@ func findAllMatchesHelper(start *State, str []rune, offset int, numGroups int) ( // b. Empty // c. Doesn't assert anything for _, s := range currentStates { - if s.isLast && s.isEmpty && s.assert == NONE { + if s.isLast && s.isEmpty && s.assert == noneAssert { lastStatePtr = s lastStateInList = true } @@ -391,7 +391,7 @@ func findAllMatchesHelper(start *State, str []rune, offset int, numGroups int) ( // Only add the match if the start index is in bounds. If the state has an assertion, // make sure the assertion checks out. if state.isLast && i <= len(str) { - if state.assert == NONE || state.checkAssertion(str, i) { + if state.assert == noneAssert || state.checkAssertion(str, i) { for j := 1; j < numGroups+1; j++ { tempIndices[j] = state.threadGroups[j] } diff --git a/regex/nfa.go b/regex/nfa.go index 5981647..df37cdd 100644 --- a/regex/nfa.go +++ b/regex/nfa.go @@ -10,16 +10,16 @@ const EPSILON int = 0xF0000 type assertType int const ( - NONE assertType = iota - SOS - EOS - WBOUND - NONWBOUND - PLA // Positive lookahead - NLA // Negative lookahead - PLB // Positive lookbehind - NLB // Negative lookbehind - ALWAYS_TRUE // An assertion that is always true + noneAssert assertType = iota + sosAssert + eosAssert + wboundAssert + nonwboundAssert + plaAssert // Positive lookahead + nlaAssert // Negative lookahead + plbAssert // Positive lookbehind + nlbAssert // Negative lookbehind + alwaysTrueAssert // An assertion that is always true ) type State struct { @@ -105,24 +105,24 @@ func cloneStateHelper(state *State, cloneMap map[*State]*State) *State { // Checks if the given state's assertion is true. Returns true if the given // state doesn't have an assertion. func (s State) checkAssertion(str []rune, idx int) bool { - if s.assert == ALWAYS_TRUE { + if s.assert == alwaysTrueAssert { return true } - if s.assert == SOS { + if s.assert == sosAssert { // Single-line mode: Beginning of string // Multi-line mode: Previous character was newline return idx == 0 || (multilineMode && (idx > 0 && str[idx-1] == '\n')) } - if s.assert == EOS { + if s.assert == eosAssert { // Single-line mode: End of string // Multi-line mode: current character is newline // Index is at the end of the string, or it points to the last character which is a newline return idx == len(str) || (multilineMode && str[idx] == '\n') } - if s.assert == WBOUND { + if s.assert == wboundAssert { return isWordBoundary(str, idx) } - if s.assert == NONWBOUND { + if s.assert == nonwboundAssert { return !isWordBoundary(str, idx) } if s.isLookaround() { @@ -133,7 +133,7 @@ func (s State) checkAssertion(str []rune, idx int) bool { startState := s.lookaroundNFA var runesToMatch []rune var strToMatch string - if s.assert == PLA || s.assert == NLA { + if s.assert == plaAssert || s.assert == nlaAssert { runesToMatch = str[idx:] } else { runesToMatch = str[:idx] @@ -149,21 +149,21 @@ func (s State) checkAssertion(str []rune, idx int) bool { numMatchesFound := 0 for _, matchIdx := range matchIndices { - if s.assert == PLA || s.assert == NLA { // Lookahead - return true (or false) if at least one match starts at 0. Zero is used because the test-string _starts_ from idx. + if s.assert == plaAssert || s.assert == nlaAssert { // Lookahead - return true (or false) if at least one match starts at 0. Zero is used because the test-string _starts_ from idx. if matchIdx[0].StartIdx == 0 { numMatchesFound++ } } - if s.assert == PLB || s.assert == NLB { // Lookbehind - return true (or false) if at least one match _ends_ at the current index. + if s.assert == plbAssert || s.assert == nlbAssert { // Lookbehind - return true (or false) if at least one match _ends_ at the current index. if matchIdx[0].EndIdx == idx { numMatchesFound++ } } } - if s.assert == PLA || s.assert == PLB { // Positive assertions want at least one match + if s.assert == plaAssert || s.assert == plbAssert { // Positive assertions want at least one match return numMatchesFound > 0 } - if s.assert == NLA || s.assert == NLB { // Negative assertions only want zero matches + if s.assert == nlaAssert || s.assert == nlbAssert { // Negative assertions only want zero matches return numMatchesFound == 0 } } @@ -172,7 +172,7 @@ func (s State) checkAssertion(str []rune, idx int) bool { // Returns true if the contents of 's' contain the value at the given index of the given string func (s State) contentContains(str []rune, idx int) bool { - if s.assert != NONE { + if s.assert != noneAssert { return s.checkAssertion(str, idx) } if s.allChars { @@ -183,7 +183,7 @@ func (s State) contentContains(str []rune, idx int) bool { } func (s State) isLookaround() bool { - return s.assert == PLA || s.assert == PLB || s.assert == NLA || s.assert == NLB + return s.assert == plaAssert || s.assert == plbAssert || s.assert == nlaAssert || s.assert == nlbAssert } // Returns the matches for the character at the given index of the given string. @@ -192,7 +192,7 @@ func (s State) matchesFor(str []rune, idx int) ([]*State, int) { // Assertions can be viewed as 'checks'. If the check fails, we return // an empty array and 0. // If it passes, we treat it like any other state, and return all the transitions. - if s.assert != NONE { + if s.assert != noneAssert { if s.checkAssertion(str, idx) == false { return make([]*State, 0), -1 } @@ -270,7 +270,7 @@ func concatenate(s1 *State, s2 *State) *State { } func kleene(s1 State) (*State, error) { - if s1.isEmpty && s1.assert != NONE { + if s1.isEmpty && s1.assert != noneAssert { return nil, fmt.Errorf("previous token is not quantifiable") } @@ -328,7 +328,7 @@ func newState() State { ret := State{ output: make([]*State, 0), transitions: make(map[int][]*State), - assert: NONE, + assert: noneAssert, except: append([]rune{}, 0), lookaroundRegex: "", groupEnd: false, @@ -343,6 +343,6 @@ func zeroLengthMatchState() State { start := newState() start.content = newContents(EPSILON) start.isEmpty = true - start.assert = ALWAYS_TRUE + start.assert = alwaysTrueAssert return start }