-
Notifications
You must be signed in to change notification settings - Fork 0
/
hasprefix.go
46 lines (40 loc) · 973 Bytes
/
hasprefix.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
package triegun
import (
"io"
"text/template"
)
func (p *Plant) genHasPrefix(w io.Writer, st *state) error {
return templPrefix.Execute(w, map[string]interface{}{
"TagName": p.TagName,
"States": allStatesUpToGoal(st),
})
}
var templPrefix = template.Must(template.New("prefix").Parse(`
func HasPrefix{{.TagName}}String(str string) bool {
return HasPrefix{{.TagName}}(*(*[]byte)(unsafe.Pointer(&str)))
}
func HasPrefix{{.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 }}
return true
{{ else }}
switch bytes[i] {
{{ range $key, $next := .Nexts }}
case {{ printf "%q" $key }}:
i++
goto STATE_{{ $next.Id }}
{{ end }}
default:
return false
}
{{ end }}
{{ end }}
}
`))