Skip to content

Commit

Permalink
fix minor bug in alternatives
Browse files Browse the repository at this point in the history
  • Loading branch information
rhaeguard committed Sep 23, 2023
1 parent 668d7ac commit 6d0ec5b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ func TestCheck(t *testing.T) {
{`(?<anim>cat)-\k<anim>`, "nonsensedog-cat-cat-dognonsense", true},
{`(?<letter>[cxv])-[a-z]+-\k<letter>`, "c-abcd-c", true},
{`(?<letter>[cxv])-[a-z]+-\k<letter>`, "c-abcd-d", false},
// capturing groups, numeric groups and named groups: example with a phone number format
{`[0-9]{3}(-| )?[0-9]{3}\1[0-9]{2}\1[0-9]{2}`, `123-678-99-32`, true},
{`[0-9]{3}(-| )?[0-9]{3}\1[0-9]{2}\1?[0-9]{2}`, `123 678 99 32`, true},
{`[0-9]{3}(-| )?[0-9]{3}\1[0-9]{2}\1?[0-9]{2}`, `123 678 9932`, true},
{`[0-9]{3}(-| |)?[0-9]{3}\1[0-9]{2}\1?[0-9]{2}`, `1236789932`, true},
{`[0-9]{3}(|-| )?[0-9]{3}\1[0-9]{2}\1?[0-9]{2}`, `1236789932`, true},
// quantifiers
{"(hi){2,3}", "hi hihi hihi", true},
{`ab{0,}bc`, `abbbbc`, true},
Expand Down Expand Up @@ -217,7 +223,11 @@ func TestCheckForDev(t *testing.T) {
regexString, input string
expected bool
}{
{`\h+`, ``, false},
{`[0-9]{3}(-| )?[0-9]{3}\1[0-9]{2}\1[0-9]{2}`, `123-678-99-32`, true},
{`[0-9]{3}(-| )?[0-9]{3}\1[0-9]{2}\1?[0-9]{2}`, `123 678 99 32`, true},
{`[0-9]{3}(-| )?[0-9]{3}\1[0-9]{2}\1?[0-9]{2}`, `123 678 9932`, true},
{`[0-9]{3}(-| |)?[0-9]{3}\1[0-9]{2}\1?[0-9]{2}`, `1236789932`, true},
{`[0-9]{3}(|-| )?[0-9]{3}\1[0-9]{2}\1?[0-9]{2}`, `1236789932`, true},
}

for _, test := range data {
Expand Down
9 changes: 9 additions & 0 deletions nfa.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,15 @@ func tokenToNfa(token regexToken, memory *parsingContext, startFrom *State) (*St
case groupUncaptured:
values := token.value.([]regexToken)

if len(values) == 0 {
end := &State{
transitions: map[uint8][]*State{},
}

startFrom.transitions[epsilonChar] = append(startFrom.transitions[epsilonChar], end)
return startFrom, end, nil
}

start, end, err := tokenToNfa(values[0], memory, &State{
transitions: map[uint8][]*State{},
})
Expand Down

0 comments on commit 6d0ec5b

Please sign in to comment.