Debian Codegraph @

src/com/google/javascript/jscomp/GoogleJsMessageIdGenerator.java

raw symbols SCIP
1 /*
2 * Copyright 2008 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.javascript.jscomp;
18
19 import com.google.common.base.CaseFormat;
20 import com.google.common.base.Charsets;
21 import com.google.common.base.Preconditions;
22 import com.google.javascript.jscomp.JsMessage.IdGenerator;
23 import com.google.javascript.jscomp.JsMessage.PlaceholderReference;
24
25 import java.util.List;
26
27 /**
28 * An {@link IdGenerator} designed to play nicely with Google's Translation
29 * systems. Each message is scoped to a project id, so that it does
30 * not conflict with other messages at Google.
31 * <p>
32 * Just as reminder what key type used in different formats:
33 * <ol>
34 * <li>XMB - id. We export using this format.
35 * <li>XTB - id. Internal, result of translation.
36 * <li>XLB - name. External, use it if we need to share translation with third
37 * part.
38 * <li>PROPERTIES - name.
39 * </ol>
40 *
41 * @see <a href="http://cldr.unicode.org/development/development-process/design-proposals/xmb">xmb</a>
42 */
43 public class GoogleJsMessageIdGenerator implements IdGenerator {
44
45 private final String projectId;
46
47 /**
48 * Creates an instance.
49 *
50 * @param projectId A TC project name (e.g. "MyProject")
51 */
52 public GoogleJsMessageIdGenerator(String projectId) {
53 this.projectId = projectId;
54 }
55
56 @Override
57 public String generateId(String meaning, List<CharSequence> messageParts) {
58 Preconditions.checkState(meaning != null);
59
60 StringBuilder sb = new StringBuilder();
61 for (CharSequence part : messageParts) {
62 if (part instanceof PlaceholderReference) {
63 sb.append(CaseFormat.LOWER_CAMEL.to(
64 CaseFormat.UPPER_UNDERSCORE,
65 ((PlaceholderReference) part).getName()));
66 } else {
67 sb.append(part);
68 }
69 }
70 String tcValue = sb.toString();
71
72 String projectScopedMeaning =
73 (projectId != null ? (projectId + ": ") : "") + meaning;
74 return String.valueOf(
75 MessageId.GenerateId(tcValue, projectScopedMeaning));
76 }
77
78
79 /**
80 * 64-bit fingerprint support.
81 *
82 * Forked from the guava-internal library.
83 */
84 private static final class FP {
85 private FP() {}
86
87 /** Generate fingerprint of "byte[start,limit-1]". */
88 private static long fingerprint(byte[] str, int start, int limit) {
89 int hi = hash32(str, start, limit, 0);
90 int lo = hash32(str, start, limit, 102072);
91 if ((hi == 0) && (lo == 0 || lo == 1)) {
92 // Turn 0/1 into another fingerprint
93 hi ^= 0x130f9bef;
94 lo ^= 0x94a0a928;
95 }
96 return (((long) hi) << 32) | (lo & 0xffffffffl);
97 }
98
99 /**
100 * Generate fingerprint of "str". Equivalent to UTF-encoding "str" into
101 * bytes and then fingerprinting those bytes.
102 */
103 private static long fingerprint(String str) {
104 byte[] tmp = str.getBytes(Charsets.UTF_8);
105 return FP.fingerprint(tmp, 0, tmp.length);
106 }
107
108 @SuppressWarnings("fallthrough")
109 private static int hash32(byte[] str, int start, int limit, int c) {
110 int a = 0x9e3779b9;
111 int b = 0x9e3779b9;
112 int i;
113 for (i = start; i + 12 <= limit; i += 12) {
114 a += (((str[i + 0] & 0xff) << 0)
115 | ((str[i + 1] & 0xff) << 8)
116 | ((str[i + 2] & 0xff) << 16)
117 | ((str[i + 3] & 0xff) << 24));
118 b += (((str[i + 4] & 0xff) << 0)
119 | ((str[i + 5] & 0xff) << 8)
120 | ((str[i + 6] & 0xff) << 16)
121 | ((str[i + 7] & 0xff) << 24));
122 c += (((str[i + 8] & 0xff) << 0)
123 | ((str[i + 9] & 0xff) << 8) | ((str[i + 10] & 0xff) << 16)
124 | ((str[i + 11] & 0xff) << 24));
125
126 // Mix
127 a -= b;
128 a -= c;
129 a ^= (c >>> 13);
130 b -= c;
131 b -= a;
132 b ^= (a << 8);
133 c -= a;
134 c -= b;
135 c ^= (b >>> 13);
136 a -= b;
137 a -= c;
138 a ^= (c >>> 12);
139 b -= c;
140 b -= a;
141 b ^= (a << 16);
142 c -= a;
143 c -= b;
144 c ^= (b >>> 5);
145 a -= b;
146 a -= c;
147 a ^= (c >>> 3);
148 b -= c;
149 b -= a;
150 b ^= (a << 10);
151 c -= a;
152 c -= b;
153 c ^= (b >>> 15);
154 }
155
156 c += limit - start;
157 switch (limit - i) { // deal with rest. Cases fall through
158 case 11:
159 c += (str[i + 10] & 0xff) << 24;
160 case 10:
161 c += (str[i + 9] & 0xff) << 16;
162 case 9:
163 c += (str[i + 8] & 0xff) << 8;
164 // the first byte of c is reserved for the length
165 case 8:
166 b += (str[i + 7] & 0xff) << 24;
167 case 7:
168 b += (str[i + 6] & 0xff) << 16;
169 case 6:
170 b += (str[i + 5] & 0xff) << 8;
171 case 5:
172 b += (str[i + 4] & 0xff);
173 case 4:
174 a += (str[i + 3] & 0xff) << 24;
175 case 3:
176 a += (str[i + 2] & 0xff) << 16;
177 case 2:
178 a += (str[i + 1] & 0xff) << 8;
179 case 1:
180 a += (str[i + 0] & 0xff);
181 // case 0 : nothing left to add
182 }
183
184 // Mix
185 a -= b;
186 a -= c;
187 a ^= (c >>> 13);
188 b -= c;
189 b -= a;
190 b ^= (a << 8);
191 c -= a;
192 c -= b;
193 c ^= (b >>> 13);
194 a -= b;
195 a -= c;
196 a ^= (c >>> 12);
197 b -= c;
198 b -= a;
199 b ^= (a << 16);
200 c -= a;
201 c -= b;
202 c ^= (b >>> 5);
203 a -= b;
204 a -= c;
205 a ^= (c >>> 3);
206 b -= c;
207 b -= a;
208 b ^= (a << 10);
209 c -= a;
210 c -= b;
211 c ^= (b >>> 15);
212 return c;
213 }
214 }
215
216 /**
217 * Generates fingerprint for an English message using the FP package.
218 * This supersedes the message id generation using C fingerprint
219 * functions and JNI. This is slower than the C implementation (
220 * we're talking about microseconds here) but it avoids using JNI and
221 * shared libraries.<p>
222 *
223 * Forked from the i18n library.
224 */
225 private static class MessageId {
226 private final static long GenerateId(String message, String meaning) {
227 long fp = FP.fingerprint(message);
228 if (null != meaning && meaning.length() > 0) {
229 // combine the fingerprints of message and meaning
230 long fp2 = FP.fingerprint(meaning);
231 fp = fp2 + (fp << 1) + (fp < 0 ? 1 : 0);
232 }
233 // To avoid negative ids we strip the high-order bit
234 return fp & 0x7fffffffffffffffL;
235 }
236 }
237 }