From 6e309be71c3e755686d08004f5e22e8ab894ee5d Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Tue, 28 Jan 2025 12:34:36 -0500 Subject: [PATCH] Moved case-insensitive stuff to thompson(); fixed case-insensitivity in character classes and ranges --- compile.go | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/compile.go b/compile.go index 5421a38..7d05781 100644 --- a/compile.go +++ b/compile.go @@ -812,18 +812,36 @@ func thompson(re []postfixNode) (Reg, error) { // and them to the state's _content_. As mentioned above, if the exception has exceptions, then we can match // those. nodeExceptChars := slices.Concat(Map(node.except, func(node postfixNode) []rune { - return node.contents + nodeContents := node.contents + if caseInsensitive { + nodeContents = slices.Concat(Map(nodeContents, func(r rune) []rune { + return allCases(r, caseInsensitive) + })...) + } + return nodeContents })...) state.content = rune2Contents(nodeExceptChars) } else { - state.except = append(state.except, node.contents...) + charsToAdd := node.contents + if caseInsensitive { + charsToAdd = slices.Concat(Map(charsToAdd, func(r rune) []rune { + return allCases(r, caseInsensitive) + })...) + } + state.except = append(state.except, charsToAdd...) } } } } // Convert the current contents to []int, convert the result of rune2contents to []int, append then // convert back to stateContents. - state.content = stateContents(append([]int(state.content), []int(rune2Contents(c.contents))...)) + runesToAdd := c.contents + if caseInsensitive { + runesToAdd = slices.Concat(Map(runesToAdd, func(r rune) []rune { + return allCases(r, caseInsensitive) + })...) + } + state.content = stateContents(append([]int(state.content), []int(rune2Contents(runesToAdd))...)) state.output = make([]*State, 0) state.output = append(state.output, &state) state.isEmpty = false @@ -935,7 +953,13 @@ func thompson(re []postfixNode) (Reg, error) { // Map the list of nodes to a list of states, each state containing the contents of a specific node states := Map(c.nodeContents, func(node postfixNode) *State { s := newState() - s.content = rune2Contents(node.contents) + nodeContents := node.contents + if caseInsensitive { + nodeContents = slices.Concat(Map(nodeContents, func(r rune) []rune { + return allCases(r, caseInsensitive) + })...) + } + s.content = rune2Contents(nodeContents) return &s }) // Reduce the list of states down to a single state by alternating them