-
Notifications
You must be signed in to change notification settings - Fork 0
/
is-in.go
47 lines (41 loc) · 957 Bytes
/
is-in.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package triegun
import (
"io"
"text/template"
)
func (p *Plant) genIsIn(w io.Writer, st *state) error {
return templIsIn.Execute(w, map[string]interface{}{
"TagName": p.TagName,
"States": allStates(st),
})
}
var templIsIn = template.Must(template.New("isin").Parse(`
func IsIn{{.TagName}}String(str string) bool {
return IsIn{{.TagName}}(*(*[]byte)(unsafe.Pointer(&str)))
}
func IsIn{{.TagName}}(bytes []byte) bool {
defer func(){
recover() // Must be "index out of range" error for string.
// Ignore and return false.
}()
i := 0
goto STATE_{{ with index .States 0 }}{{ print .Id }}{{ end }}
{{ range .States }}
STATE_{{ .Id }}:
{{ if .IsGoal }}
if i == len(bytes) {
return true
}
{{ end }}
switch bytes[i] {
{{ range $key, $next := .Nexts }}
case {{ printf "%q" $key }}:
i++
goto STATE_{{ $next.Id }}
{{ end }}
default:
return false
}
{{ end }}
}
`))