mirror of
https://github.com/wader/fq.git
synced 2024-11-29 23:27:12 +03:00
e2eb667091
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.
31 lines
519 B
Go
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
|
|
}
|