/* * Copyright 2006 The Closure Compiler Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.google.javascript.jscomp; import com.google.common.base.Preconditions; import com.google.common.collect.Lists; import com.google.common.collect.Sets; import java.util.Collections; import java.util.List; import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.annotation.Nullable; /** * A representation of a translatable message in JavaScript source code. * *
Instances are created using a {@link JsMessage.Builder}, * like this: *
* JsMessage m = new JsMessage.Builder(key)
* .appendPart("Hi ")
* .appendPlaceholderReference("firstName")
* .appendPart("!")
* .setDesc("A welcome message")
* .build();
*
*
*/
public class JsMessage {
/**
* Message style that could be used for JS code parsing.
* The enum order is from most relaxed to most restricted.
*/
public enum Style {
LEGACY, // All legacy code is completely OK
RELAX, // You allowed to use legacy code but it would be reported as warn
CLOSURE; // Any legacy code is prohibited
/**
* Calculates current messages {@link Style} based on the given arguments.
*
* @param useClosure if true then use closure style, otherwise not
* @param allowLegacyMessages if true then allow legacy messages otherwise
* not
* @return the message style based on the given arguments
*/
static Style getFromParams(boolean useClosure,
boolean allowLegacyMessages) {
if (useClosure) {
return allowLegacyMessages ? RELAX : CLOSURE;
} else {
return LEGACY;
}
}
}
private static final String MESSAGE_REPRESENTATION_FORMAT = "{$%s}";
private final String key;
private final String id;
private final ListThe hash takes a byte array representing arbitrary data (a * number, String, or Object) and turns it into a small, hopefully * unique, number. There are additional convenience functions which * hash int, long, and String types. * *
Note: this hash has weaknesses in the two * most-significant key bits and in the three least-significant seed * bits. The weaknesses are small and practically speaking, will not * affect the distribution of hash values. Still, it would be good * practice not to choose seeds 0, 1, 2, 3, ..., n to yield n, * independent hash functions. Use pseudo-random seeds instead. * *
This code is based on the work of Craig Silverstein and Sanjay * Ghemawat in, then forked from com.google.common. * *
The original code for the hash function is courtesy * Bob Jenkins. * *
TODO: Add stream hashing functionality.
*/
final static class Hash {
private Hash() {}
/** Default hash seed (64 bit) */
private static final long SEED64 =
0x2b992ddfa23249d6L; // part of pi, arbitrary
/** Hash constant (64 bit) */
private static final long CONSTANT64 =
0xe08c1d668b756f82L; // part of golden ratio, arbitrary
/******************
* STRING HASHING *
******************/
/**
* Hash a string to a 64 bit value. The digits of pi are used for
* the hash seed.
*
* @param value the string to hash
* @return 64 bit hash value
*/
static long hash64(@Nullable String value) {
return hash64(value, SEED64);
}
/**
* Hash a string to a 64 bit value using the supplied seed.
*
* @param value the string to hash
* @param seed the seed
* @return 64 bit hash value
*/
private static long hash64(@Nullable String value, long seed) {
if (value == null) {
return hash64(null, 0, 0, seed);
}
return hash64(value.getBytes(), seed);
}
/**
* Hash byte array to a 64 bit value using the supplied seed.
*
* @param value the bytes to hash
* @param seed the seed
* @return 64 bit hash value
*/
private static long hash64(byte[] value, long seed) {
return hash64(value, 0, value == null ? 0 : value.length, seed);
}
/**
* Hash byte array to a 64 bit value using the supplied seed.
*
* @param value the bytes to hash
* @param offset the starting position of value where bytes are
* used for the hash computation
* @param length number of bytes of value that are used for the
* hash computation
* @param seed the seed
* @return 64 bit hash value
*/
@SuppressWarnings("fallthrough")
private static long hash64(
byte[] value, int offset, int length, long seed) {
long a = CONSTANT64;
long b = a;
long c = seed;
int keylen;
for (keylen = length; keylen >= 24; keylen -= 24, offset += 24) {
a += word64At(value, offset);
b += word64At(value, offset + 8);
c += word64At(value, offset + 16);
// Mix
a -= b; a -= c; a ^= c >>> 43;
b -= c; b -= a; b ^= a << 9;
c -= a; c -= b; c ^= b >>> 8;
a -= b; a -= c; a ^= c >>> 38;
b -= c; b -= a; b ^= a << 23;
c -= a; c -= b; c ^= b >>> 5;
a -= b; a -= c; a ^= c >>> 35;
b -= c; b -= a; b ^= a << 49;
c -= a; c -= b; c ^= b >>> 11;
a -= b; a -= c; a ^= c >>> 12;
b -= c; b -= a; b ^= a << 18;
c -= a; c -= b; c ^= b >>> 22;
}
c += length;
switch (keylen) { // deal with rest. Cases fall through
case 23:
c += ((long) value[offset + 22]) << 56;
case 22:
c += (value[offset + 21] & 0xffL) << 48;
case 21:
c += (value[offset + 20] & 0xffL) << 40;
case 20:
c += (value[offset + 19] & 0xffL) << 32;
case 19:
c += (value[offset + 18] & 0xffL) << 24;
case 18:
c += (value[offset + 17] & 0xffL) << 16;
case 17:
c += (value[offset + 16] & 0xffL) << 8;
// the first byte of c is reserved for the length
case 16:
b += word64At(value, offset + 8);
a += word64At(value, offset);
break;
case 15:
b += (value[offset + 14] & 0xffL) << 48;
case 14:
b += (value[offset + 13] & 0xffL) << 40;
case 13:
b += (value[offset + 12] & 0xffL) << 32;
case 12:
b += (value[offset + 11] & 0xffL) << 24;
case 11:
b += (value[offset + 10] & 0xffL) << 16;
case 10:
b += (value[offset + 9] & 0xffL) << 8;
case 9:
b += (value[offset + 8] & 0xffL);
case 8:
a += word64At(value, offset);
break;
case 7:
a += (value[offset + 6] & 0xffL) << 48;
case 6:
a += (value[offset + 5] & 0xffL) << 40;
case 5:
a += (value[offset + 4] & 0xffL) << 32;
case 4:
a += (value[offset + 3] & 0xffL) << 24;
case 3:
a += (value[offset + 2] & 0xffL) << 16;
case 2:
a += (value[offset + 1] & 0xffL) << 8;
case 1:
a += (value[offset + 0] & 0xffL);
// case 0: nothing left to add
}
return mix64(a, b, c);
}
private static long word64At(byte[] bytes, int offset) {
return (bytes[offset + 0] & 0xffL)
+ ((bytes[offset + 1] & 0xffL) << 8)
+ ((bytes[offset + 2] & 0xffL) << 16)
+ ((bytes[offset + 3] & 0xffL) << 24)
+ ((bytes[offset + 4] & 0xffL) << 32)
+ ((bytes[offset + 5] & 0xffL) << 40)
+ ((bytes[offset + 6] & 0xffL) << 48)
+ ((bytes[offset + 7] & 0xffL) << 56);
}
/**
* Mixes longs a, b, and c, and returns the final value of c.
*/
private static long mix64(long a, long b, long c) {
a -= b; a -= c; a ^= c >>> 43;
b -= c; b -= a; b ^= a << 9;
c -= a; c -= b; c ^= b >>> 8;
a -= b; a -= c; a ^= c >>> 38;
b -= c; b -= a; b ^= a << 23;
c -= a; c -= b; c ^= b >>> 5;
a -= b; a -= c; a ^= c >>> 35;
b -= c; b -= a; b ^= a << 49;
c -= a; c -= b; c ^= b >>> 11;
a -= b; a -= c; a ^= c >>> 12;
b -= c; b -= a; b ^= a << 18;
c -= a; c -= b; c ^= b >>> 22;
return c;
}
}
public interface IdGenerator {
/**
* Generate the ID for the message. Messages with the same messageParts
* and meaning will get the same id. Messages with the same id
* will get the same translation.
*
* @param meaning The programmer-specified meaning. If no {@code @meaning}
* annotation appears, we will use the name of the variable it's
* assigned to. If the variable is unnamed, then we will just
* use a fingerprint of the message.
* @param messageParts The parts of the message, including the main
* message text.
*/
String generateId(String meaning, List