/
id.go
197 lines (183 loc) · 4 KB
/
id.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package soymsg
import (
"bytes"
"fmt"
"strconv"
"github.com/robfig/soy/ast"
)
// calcID calculates the message ID for the given message node.
// The ID changes if the text content or meaning attribute changes.
// It is invariant to changes in description.
func calcID(n *ast.MsgNode) uint64 {
var buf bytes.Buffer
writeFingerprint(&buf, n, false)
var fp = fingerprint(buf.Bytes())
if n.Meaning != "" {
var topbit uint64
if fp&(1<<63) > 0 {
topbit = 1
}
fp = (fp << 1) + topbit + fingerprint([]byte(n.Meaning))
}
return fp & 0x7fffffffffffffff
}
// writeFingerprint writes the string used to fingerprint a message to the buffer.
// if braces is true, the string written has placeholders surrounded by braces.
// Plural messages always have braced placeholders.
func writeFingerprint(buf *bytes.Buffer, part ast.Node, braces bool) {
switch part := part.(type) {
case *ast.MsgNode:
for _, part := range part.Body.Children() {
writeFingerprint(buf, part, braces)
}
case *ast.RawTextNode:
buf.Write(part.Text)
case *ast.MsgPlaceholderNode:
if braces {
buf.WriteString("{" + part.Name + "}")
} else {
buf.WriteString(part.Name)
}
case *ast.MsgPluralNode:
buf.WriteString("{" + part.VarName + ",plural,")
for _, plCase := range part.Cases {
buf.WriteString("=" + strconv.Itoa(plCase.Value) + "{")
for _, child := range plCase.Body.Children() {
writeFingerprint(buf, child, true)
}
buf.WriteString("}")
}
buf.WriteString("other{")
for _, child := range part.Default.Children() {
writeFingerprint(buf, child, true)
}
buf.WriteString("}}")
default:
panic(fmt.Sprintf("unrecognized type %T", part))
}
}
// fingerprinting functions ported from official Soy, so that we end up with the
// same message ids.
func fingerprint(str []byte) uint64 {
var hi = hash32(str, 0, len(str), 0)
var lo = hash32(str, 0, len(str), 102072)
if (hi == 0) && (lo == 0 || lo == 1) {
// Turn 0/1 into another fingerprint
hi ^= 0x130f9bef
lo ^= 0x94a0a928
}
return (uint64(hi) << 32) | uint64(lo&0xffffffff)
}
func hash32(str []byte, start, limit int, c uint32) uint32 {
var a uint32 = 0x9e3779b9
var b uint32 = 0x9e3779b9
var i int
for i = start; i+12 <= limit; i += 12 {
a += (uint32(str[i+0]&0xff) << 0) |
(uint32(str[i+1]&0xff) << 8) |
(uint32(str[i+2]&0xff) << 16) |
(uint32(str[i+3]&0xff) << 24)
b += (uint32(str[i+4]&0xff) << 0) |
(uint32(str[i+5]&0xff) << 8) |
(uint32(str[i+6]&0xff) << 16) |
(uint32(str[i+7]&0xff) << 24)
c += (uint32(str[i+8]&0xff) << 0) |
(uint32(str[i+9]&0xff) << 8) |
(uint32(str[i+10]&0xff) << 16) |
(uint32(str[i+11]&0xff) << 24)
// Mix.
a -= b
a -= c
a ^= (c >> 13)
b -= c
b -= a
b ^= (a << 8)
c -= a
c -= b
c ^= (b >> 13)
a -= b
a -= c
a ^= (c >> 12)
b -= c
b -= a
b ^= (a << 16)
c -= a
c -= b
c ^= (b >> 5)
a -= b
a -= c
a ^= (c >> 3)
b -= c
b -= a
b ^= (a << 10)
c -= a
c -= b
c ^= (b >> 15)
}
c += uint32(limit - start)
switch limit - i { // Deal with rest. Cases fall through.
case 11:
c += uint32(str[i+10]&0xff) << 24
fallthrough
case 10:
c += uint32(str[i+9]&0xff) << 16
fallthrough
case 9:
c += uint32(str[i+8]&0xff) << 8
// the first byte of c is reserved for the length
fallthrough
case 8:
b += uint32(str[i+7]&0xff) << 24
fallthrough
case 7:
b += uint32(str[i+6]&0xff) << 16
fallthrough
case 6:
b += uint32(str[i+5]&0xff) << 8
fallthrough
case 5:
b += uint32(str[i+4] & 0xff)
fallthrough
case 4:
a += uint32(str[i+3]&0xff) << 24
fallthrough
case 3:
a += uint32(str[i+2]&0xff) << 16
fallthrough
case 2:
a += uint32(str[i+1]&0xff) << 8
fallthrough
case 1:
a += uint32(str[i+0] & 0xff)
// case 0 : nothing left to add
}
// Mix.
a -= b
a -= c
a ^= (c >> 13)
b -= c
b -= a
b ^= (a << 8)
c -= a
c -= b
c ^= (b >> 13)
a -= b
a -= c
a ^= (c >> 12)
b -= c
b -= a
b ^= (a << 16)
c -= a
c -= b
c ^= (b >> 5)
a -= b
a -= c
a ^= (c >> 3)
b -= c
b -= a
b ^= (a << 10)
c -= a
c -= b
c ^= (b >> 15)
return c
}