12 Commits

7 changed files with 57 additions and 7 deletions

11
LICENSE Normal file
View File

@@ -0,0 +1,11 @@
The MIT License (MIT)
Copyright (c) 2025 Aadhavan Srinivasan
All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@@ -1,9 +1,13 @@
.DEFAULT_GOAL := build .DEFAULT_GOAL := buildCmd
.PHONY: fmt vet build .PHONY: fmt vet buildLib buildCmd test
fmt: fmt:
go fmt ./... go fmt ./...
vet: fmt vet: fmt
go vet ./... go vet ./...
build: vet buildLib: vet
go build -gcflags="-N -l" ./... go build -gcflags="-N -l" ./...
buildCmd: buildLib
go build -C cmd/ -o re ./...
test: buildCmd
go test -v ./...

View File

@@ -407,7 +407,7 @@ func shuntingYard(re string, flags ...ReFlag) ([]postfixNode, error) {
} else { } else {
escapedNode, err := newEscapedNode(re_postfix[i], false) escapedNode, err := newEscapedNode(re_postfix[i], false)
if err != nil { if err != nil {
return nil, fmt.Errorf("invalid escape character in expression") return nil, err
} }
outQueue = append(outQueue, escapedNode) outQueue = append(outQueue, escapedNode)
} }
@@ -575,7 +575,7 @@ func shuntingYard(re string, flags ...ReFlag) ([]postfixNode, error) {
} else { } else {
escapedNode, err := newEscapedNode(re_postfix[i], true) escapedNode, err := newEscapedNode(re_postfix[i], true)
if err != nil { if err != nil {
return nil, fmt.Errorf("invalid escape character in character class") return nil, err
} }
chars = append(chars, escapedNode) chars = append(chars, escapedNode)
i++ i++
@@ -878,6 +878,10 @@ func thompson(re []postfixNode) (Reg, error) {
stateToAdd.assert = wboundAssert stateToAdd.assert = wboundAssert
case 'B': case 'B':
stateToAdd.assert = nonwboundAssert stateToAdd.assert = nonwboundAssert
case 'A':
stateToAdd.assert = soiAssert
case 'z':
stateToAdd.assert = eoiAssert
} }
} else { // Lookaround } else { // Lookaround
stateToAdd.lookaroundRegex = string(c.contents) stateToAdd.lookaroundRegex = string(c.contents)

7
regex/doc.go Normal file
View File

@@ -0,0 +1,7 @@
/*
Package regex implements an NFA-based engine to search for
regular expressions in strings. The engine does not use backtracking,
and is therefore not vulnerable to catastrophic backtracking. The regex
syntax supported (a variation of Golang's) is specified in the Syntax section.
*/
package regex

View File

@@ -11,8 +11,10 @@ type assertType int
const ( const (
noneAssert assertType = iota noneAssert assertType = iota
sosAssert sosAssert // Start of string (^)
eosAssert soiAssert // Start of input (\A)
eosAssert // End of string ($)
eoiAssert // End of input (\Z)
wboundAssert wboundAssert
nonwboundAssert nonwboundAssert
plaAssert // Positive lookahead plaAssert // Positive lookahead
@@ -119,6 +121,15 @@ func (s nfaState) checkAssertion(str []rune, idx int) bool {
// Index is at the end of the string, or it points to the last character which is a 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') return idx == len(str) || (multilineMode && str[idx] == '\n')
} }
if s.assert == soiAssert {
// Only true at the start of the input, regardless of mode
return idx == 0
}
if s.assert == eoiAssert {
// Only true at the end of the input, regardless of mode
return idx == len(str)
}
if s.assert == wboundAssert { if s.assert == wboundAssert {
return isWordBoundary(str, idx) return isWordBoundary(str, idx)
} }

View File

@@ -95,6 +95,16 @@ func newEscapedNode(c rune, inCharClass bool) (postfixNode, error) {
toReturn.nodetype = assertionNode toReturn.nodetype = assertionNode
toReturn.contents = append(toReturn.contents, c) toReturn.contents = append(toReturn.contents, c)
} }
if c == 'B' && inCharClass { // Invalid
return postfixNode{}, fmt.Errorf("word boundaries are not allowed in character class")
}
case 'A', 'z': // A is start of input, z is end of input (regardless of RE_MULTILINE)
if inCharClass {
return postfixNode{}, fmt.Errorf("input boundaries are not allowed in character class")
} else {
toReturn.nodetype = assertionNode
toReturn.contents = append(toReturn.contents, c)
}
case 'n': // Newline character case 'n': // Newline character
toReturn.nodetype = characterNode toReturn.nodetype = characterNode
toReturn.contents = append(toReturn.contents, '\n') toReturn.contents = append(toReturn.contents, '\n')

View File

@@ -443,8 +443,11 @@ var reTests = []struct {
{`abc$`, []ReFlag{RE_MULTILINE}, "jkl\n123abc\nxyz", []Group{{7, 10}}}, {`abc$`, []ReFlag{RE_MULTILINE}, "jkl\n123abc\nxyz", []Group{{7, 10}}},
{`^`, nil, "jkl\n123abc\nxyz", []Group{{0, 0}}}, {`^`, nil, "jkl\n123abc\nxyz", []Group{{0, 0}}},
{`^`, []ReFlag{RE_MULTILINE}, "jkl\n123abc\nxyz", []Group{{0, 0}, {4, 4}, {11, 11}}}, {`^`, []ReFlag{RE_MULTILINE}, "jkl\n123abc\nxyz", []Group{{0, 0}, {4, 4}, {11, 11}}},
{`\A`, []ReFlag{RE_MULTILINE}, "jkl\n123abc\nxyz", []Group{{0, 0}}},
{`$`, nil, "jkl\n123abc\nxyz", []Group{{14, 14}}}, {`$`, nil, "jkl\n123abc\nxyz", []Group{{14, 14}}},
{`$`, []ReFlag{RE_MULTILINE}, "jkl\n123abc\nxyz", []Group{{3, 3}, {10, 10}, {14, 14}}}, {`$`, []ReFlag{RE_MULTILINE}, "jkl\n123abc\nxyz", []Group{{3, 3}, {10, 10}, {14, 14}}},
{`\z`, []ReFlag{RE_MULTILINE}, "jkl\n123abc\nxyz", []Group{{14, 14}}},
{`^abc\z`, []ReFlag{RE_MULTILINE}, "abc\nabc\nabc", []Group{{8, 11}}},
{`a.b`, nil, "a\nb", []Group{}}, {`a.b`, nil, "a\nb", []Group{}},
{`a.b`, []ReFlag{RE_SINGLE_LINE}, "a\nb", []Group{{0, 3}}}, {`a.b`, []ReFlag{RE_SINGLE_LINE}, "a\nb", []Group{{0, 3}}},