Documentation
¶
Overview ¶
Package scan compiles many regular expressions into one block scanner.
Index ¶
Examples ¶
Constants ¶
const ( Caseless = engine.Caseless DotAll = engine.DotAll MultiLine = engine.MultiLine SingleMatch = engine.SingleMatch AllowEmpty = engine.AllowEmpty UTF8 = engine.UTF8 Prefilter = engine.Prefilter SomLeftMost = engine.SomLeftMost )
Variables ¶
var ( ErrInvalid = engine.ErrInvalid ErrScanTerminated = engine.ErrScanTerminated ErrScratchInUse = engine.ErrScratchInUse )
Functions ¶
This section is empty.
Types ¶
type CompileStats ¶
type CompileStats = engine.CompileStats
CompileStats describes the routing decisions made while compiling a database.
type Database ¶
Database is an immutable block-mode multi-pattern database.
func Compile ¶
Compile builds one block database from patterns. Patterns must be trusted; compilation does not enforce resource budgets.
func UnmarshalDatabase ¶
UnmarshalDatabase loads compiled tables produced by a trusted compiler.
func (*Database) Marshal ¶
Marshal encodes the compiled database for storage or go:embed.
Example ¶
package main
import (
"fmt"
"log"
"github.com/git-pkgs/scan"
)
func main() {
db, err := scan.Compile(&scan.Pattern{Expression: `token=[a-z0-9]{8}`})
if err != nil {
log.Fatal(err)
}
compiled, err := db.Marshal()
if err != nil {
log.Fatal(err)
}
loaded, err := scan.UnmarshalDatabase(compiled)
if err != nil {
log.Fatal(err)
}
matched, err := loaded.Match([]byte("token=abc12345"), scan.NewScratch(loaded))
if err != nil {
log.Fatal(err)
}
fmt.Println(matched)
}
Output: true
func (*Database) Match ¶
Match reports whether any expression matches data.
Example ¶
package main
import (
"fmt"
"log"
"github.com/git-pkgs/scan"
)
func main() {
db, err := scan.Compile(&scan.Pattern{Expression: `token=[a-z0-9]{8}`})
if err != nil {
log.Fatal(err)
}
scratch := scan.NewScratch(db)
matched, err := db.Match([]byte("token=abc12345"), scratch)
if err != nil {
log.Fatal(err)
}
fmt.Println(matched)
}
Output: true
func (*Database) Scan ¶
func (db *Database) Scan(data []byte, scratch *Scratch, handler MatchHandler) error
Scan searches data and reports matching end offsets in ascending order. Returning an error stops callbacks, after matching work has completed.
Example ¶
package main
import (
"fmt"
"log"
"github.com/git-pkgs/scan"
)
func main() {
db, err := scan.Compile(
&scan.Pattern{Expression: `token=[a-z0-9]{8}`, ID: 1, Flags: scan.SingleMatch | scan.SomLeftMost},
&scan.Pattern{Expression: `code=[0-9]{4}`, ID: 2, Flags: scan.SingleMatch | scan.SomLeftMost},
)
if err != nil {
log.Fatal(err)
}
scratch := scan.NewScratch(db)
err = db.Scan([]byte("token=abc12345 code=1234"), scratch, func(match scan.Match) error {
fmt.Printf("%d %d:%d\n", match.ID, match.From, match.To)
return nil
})
if err != nil {
log.Fatal(err)
}
}
Output: 1 0:14 2 15:24
func (*Database) Stats ¶
func (db *Database) Stats() CompileStats
Stats returns counts useful when tuning a pattern set.
type MatchHandler ¶
type MatchHandler = engine.MatchHandler
MatchHandler receives matches in engine order. Returning an error stops callbacks.
type Pattern ¶
Pattern associates an expression and compile flags with an application ID.
func NewPattern ¶
NewPattern returns a pattern with the supplied expression and flags.
type Scratch ¶
Scratch contains mutable scan state. Concurrent scans need separate scratches.
func (*Scratch) Clone ¶
Clone returns independent scan state with the same capacity.
Example ¶
package main
import (
"fmt"
"log"
"github.com/git-pkgs/scan"
)
func main() {
db, err := scan.Compile(&scan.Pattern{Expression: `token=[a-z0-9]{8}`})
if err != nil {
log.Fatal(err)
}
scratch := scan.NewScratch(db)
inputs := [][]byte{[]byte("token=abc12345"), []byte("no match")}
results := make(chan bool, len(inputs))
for _, input := range inputs {
workerScratch := scratch.Clone()
go func() {
matched, err := db.Match(input, workerScratch)
if err != nil {
log.Fatal(err)
}
results <- matched
}()
}
matchedInputs := 0
for range inputs {
if <-results {
matchedInputs++
}
}
fmt.Println(matchedInputs)
}
Output: 1