1
1
mirror of https://github.com/wader/fq.git synced 2024-11-25 23:13:19 +03:00
fq/internal/lazyre/lazyre.go
Mattias Wadman e2eb667091 html: Add to probe group
As decoder now can know they are decoding as part of probing we can now
use some heuristics to see if we should decode as html.
The reason heuristics is needed is that x/html parser will alwaus succeed.

Add lazyre package to help delay compile of RE and make it concurrency safe.
2023-05-11 19:07:18 +02:00

31 lines
519 B
Go

// lazyre lazily compiles a *regexp.Regexp in concurrency safe way
// Use &lazyre.RE{S: `...`} or call New
package lazyre
import (
"regexp"
"sync"
)
type RE struct {
S string
m sync.RWMutex
re *regexp.Regexp
}
// New creates a new *lazyRE
func New(s string) *RE {
return &RE{S: s}
}
// Must compiles regexp, returned *regexp.Regexp can be stored away and reused
func (lr *RE) Must() *regexp.Regexp {
lr.m.Lock()
defer lr.m.Unlock()
if lr.re == nil {
lr.re = regexp.MustCompile(lr.S)
}
return lr.re
}