Debian Codegraph @

html/hash.go

raw
1 package html
2
3 // uses github.com/tdewolff/hasher
4 //go:generate hasher -type=Hash -file=hash.go
5
6 // Hash defines perfect hashes for a predefined list of strings
7 type Hash uint32
8
9 // Identifiers for the hashes associated with the text in the comments.
10 const (
11 Iframe Hash = 0x6 // iframe
12 Math Hash = 0x604 // math
13 Plaintext Hash = 0x2109 // plaintext
14 Script Hash = 0xa06 // script
15 Style Hash = 0x1405 // style
16 Svg Hash = 0x1903 // svg
17 Textarea Hash = 0x2608 // textarea
18 Title Hash = 0xf05 // title
19 Xml Hash = 0x1c03 // xml
20 Xmp Hash = 0x1f03 // xmp
21 )
22
23 //var HashMap = map[string]Hash{
24 // "iframe": Iframe,
25 // "math": Math,
26 // "plaintext": Plaintext,
27 // "script": Script,
28 // "style": Style,
29 // "svg": Svg,
30 // "textarea": Textarea,
31 // "title": Title,
32 // "xml": Xml,
33 // "xmp": Xmp,
34 //}
35
36 // String returns the text associated with the hash.
37 func (i Hash) String() string {
38 return string(i.Bytes())
39 }
40
41 // Bytes returns the text associated with the hash.
42 func (i Hash) Bytes() []byte {
43 start := uint32(i >> 8)
44 n := uint32(i & 0xff)
45 if start+n > uint32(len(_Hash_text)) {
46 return []byte{}
47 }
48 return _Hash_text[start : start+n]
49 }
50
51 // ToHash returns a hash Hash for a given []byte. Hash is a uint32 that is associated with the text in []byte. It returns zero if no match found.
52 func ToHash(s []byte) Hash {
53 if len(s) == 0 || len(s) > _Hash_maxLen {
54 return 0
55 }
56 //if 3 < len(s) {
57 // return HashMap[string(s)]
58 //}
59 h := uint32(_Hash_hash0)
60 for i := 0; i < len(s); i++ {
61 h ^= uint32(s[i])
62 h *= 16777619
63 }
64 if i := _Hash_table[h&uint32(len(_Hash_table)-1)]; int(i&0xff) == len(s) {
65 t := _Hash_text[i>>8 : i>>8+i&0xff]
66 for i := 0; i < len(s); i++ {
67 if t[i] != s[i] {
68 goto NEXT
69 }
70 }
71 return i
72 }
73 NEXT:
74 if i := _Hash_table[(h>>16)&uint32(len(_Hash_table)-1)]; int(i&0xff) == len(s) {
75 t := _Hash_text[i>>8 : i>>8+i&0xff]
76 for i := 0; i < len(s); i++ {
77 if t[i] != s[i] {
78 return 0
79 }
80 }
81 return i
82 }
83 return 0
84 }
85
86 const _Hash_hash0 = 0xb4b790b3
87 const _Hash_maxLen = 9
88
89 var _Hash_text = []byte("" +
90 "iframemathscriptitlestylesvgxmlxmplaintextarea")
91
92 var _Hash_table = [1 << 4]Hash{
93 0x2: 0xa06, // script
94 0x3: 0xf05, // title
95 0x4: 0x1405, // style
96 0x5: 0x604, // math
97 0x6: 0x6, // iframe
98 0x8: 0x1c03, // xml
99 0x9: 0x2608, // textarea
100 0xc: 0x1f03, // xmp
101 0xe: 0x2109, // plaintext
102 0xf: 0x1903, // svg
103 }