Added support for start-of-input (\A) and end-of-input (\Z) assertions

This commit is contained in:
2025-01-30 13:56:56 -05:00
parent db7c884b83
commit ee51e39d59
3 changed files with 24 additions and 2 deletions

View File

@@ -11,8 +11,10 @@ type assertType int
const (
noneAssert assertType = iota
sosAssert
eosAssert
sosAssert // Start of string (^)
soiAssert // Start of input (\A)
eosAssert // End of string ($)
eoiAssert // End of input (\Z)
wboundAssert
nonwboundAssert
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
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 {
return isWordBoundary(str, idx)
}