| 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.javascript.rhino.TokenStream; |
| 20 |
import javax.annotation.Nullable; |
| 21 |
import com.google.common.collect.Sets; |
| 22 |
import com.google.common.primitives.Chars; |
| 23 |
|
| 24 |
import java.util.*; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
final class NameGenerator { |
| 33 |
|
| 34 |
static final char[] FIRST_CHAR = |
| 35 |
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$".toCharArray(); |
| 36 |
|
| 37 |
|
| 38 |
static final char[] NONFIRST_CHAR = |
| 39 |
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789$" |
| 40 |
.toCharArray(); |
| 41 |
|
| 42 |
private final Set<String> reservedNames; |
| 43 |
private final String prefix; |
| 44 |
private int nameCount; |
| 45 |
|
| 46 |
private final char[] firstChars; |
| 47 |
private final char[] nonFirstChars; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
NameGenerator(Set<String> reservedNames, String prefix, |
| 60 |
@Nullable char[] reservedCharacters) { |
| 61 |
this.reservedNames = reservedNames; |
| 62 |
this.prefix = prefix; |
| 63 |
|
| 64 |
|
| 65 |
this.firstChars = reserveCharacters(FIRST_CHAR, reservedCharacters); |
| 66 |
this.nonFirstChars = reserveCharacters(NONFIRST_CHAR, reservedCharacters); |
| 67 |
|
| 68 |
checkPrefix(prefix); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
static char[] reserveCharacters(char[] chars, char[] reservedCharacters) { |
| 79 |
if (reservedCharacters == null || reservedCharacters.length == 0) { |
| 80 |
return chars; |
| 81 |
} |
| 82 |
Set<Character> charSet = Sets.newLinkedHashSet(Chars.asList(chars)); |
| 83 |
for (char reservedCharacter : reservedCharacters) { |
| 84 |
charSet.remove(reservedCharacter); |
| 85 |
} |
| 86 |
return Chars.toArray(charSet); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
private void checkPrefix(String prefix) { |
| 91 |
if (prefix.length() > 0) { |
| 92 |
|
| 93 |
if (!contains(firstChars, prefix.charAt(0))) { |
| 94 |
throw new IllegalArgumentException("prefix must start with one of: " + |
| 95 |
Arrays.toString(firstChars)); |
| 96 |
} |
| 97 |
for (int pos = 1; pos < prefix.length(); ++pos) { |
| 98 |
if (!contains(nonFirstChars, prefix.charAt(pos))) { |
| 99 |
throw new IllegalArgumentException("prefix has invalid characters, " + |
| 100 |
"must be one of: " + |
| 101 |
Arrays.toString(nonFirstChars)); |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
private boolean contains(char[] arr, char c) { |
| 108 |
for (int i = 0; i < arr.length; i++) { |
| 109 |
if (arr[i] == c) { |
| 110 |
return true; |
| 111 |
} |
| 112 |
} |
| 113 |
return false; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
String generateNextName() { |
| 120 |
while (true) { |
| 121 |
String name = prefix; |
| 122 |
|
| 123 |
int i = nameCount; |
| 124 |
|
| 125 |
if (name.isEmpty()) { |
| 126 |
int pos = i % firstChars.length; |
| 127 |
name += firstChars[pos]; |
| 128 |
i /= firstChars.length; |
| 129 |
} |
| 130 |
|
| 131 |
while (i > 0) { |
| 132 |
i--; |
| 133 |
int pos = i % nonFirstChars.length; |
| 134 |
name += nonFirstChars[pos]; |
| 135 |
i /= nonFirstChars.length; |
| 136 |
} |
| 137 |
|
| 138 |
nameCount++; |
| 139 |
|
| 140 |
|
| 141 |
if (TokenStream.isKeyword(name) || reservedNames.contains(name)) { |
| 142 |
continue; |
| 143 |
} |
| 144 |
|
| 145 |
return name; |
| 146 |
} |
| 147 |
} |
| 148 |
} |