| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public class GoogleJsMessageIdGenerator implements IdGenerator { |
| 44 |
|
| 45 |
private final String projectId; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 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 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
private static final class FP { |
| 85 |
private FP() {} |
| 86 |
|
| 87 |
|
| 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 |
|
| 93 |
hi ^= 0x130f9bef; |
| 94 |
lo ^= 0x94a0a928; |
| 95 |
} |
| 96 |
return (((long) hi) << 32) | (lo & 0xffffffffl); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 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 |
|
| 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) { |
| 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 |
|
| 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 |
|
| 182 |
} |
| 183 |
|
| 184 |
|
| 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 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 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 |
|
| 230 |
long fp2 = FP.fingerprint(meaning); |
| 231 |
fp = fp2 + (fp << 1) + (fp < 0 ? 1 : 0); |
| 232 |
} |
| 233 |
|
| 234 |
return fp & 0x7fffffffffffffffL; |
| 235 |
} |
| 236 |
} |
| 237 |
} |