Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2005 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.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 * A simple class for generating unique JavaScript variable/property names.
28 *
29 * <p>This class is not thread safe.
30 *
31 */
32 final class NameGenerator {
33 /** Generate short name with this first character */
34 static final char[] FIRST_CHAR =
35 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$".toCharArray();
36
37 /** These appear after after the first character */
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 * Creates a NameGenerator.
51 *
52 * @param reservedNames set of names that are reserved; generated names will
53 * not include these names. This set is referenced rather than copied,
54 * so changes to the set will be reflected in how names are generated.
55 * @param prefix all generated names begin with this prefix.
56 * @param reservedCharacters If specified these characters won't be used in
57 * generated names
58 */
59 NameGenerator(Set<String> reservedNames, String prefix,
60 @Nullable char[] reservedCharacters) {
61 this.reservedNames = reservedNames;
62 this.prefix = prefix;
63
64 // build the character arrays to use
65 this.firstChars = reserveCharacters(FIRST_CHAR, reservedCharacters);
66 this.nonFirstChars = reserveCharacters(NONFIRST_CHAR, reservedCharacters);
67
68 checkPrefix(prefix);
69 }
70
71 /**
72 * Provides the array of available characters based on the specified arrays.
73 * @param chars The list of characters that are legal
74 * @param reservedCharacters The characters that should not be used
75 * @return An array of characters to use. Will return the chars array if
76 * reservedCharacters is null or empty, otherwise creates a new array.
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 /** Validates a name prefix. */
90 private void checkPrefix(String prefix) {
91 if (prefix.length() > 0) {
92 // Make sure that prefix starts with a legal character.
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 * Generates the next short name.
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 // Make sure it's not a JS keyword or reserved name.
141 if (TokenStream.isKeyword(name) || reservedNames.contains(name)) {
142 continue;
143 }
144
145 return name;
146 }
147 }
148 }