Renamed "base54" to "nameGenerator" (#3436).

This is because its character set will soon be configurable, and it may therefore return
non-base-54 strings.
This commit is contained in:
jameswilddev 2019-07-20 22:55:25 +01:00
parent 70bb304a0a
commit a8fc48eea6
2 changed files with 19 additions and 19 deletions

View File

@ -210,7 +210,7 @@ function mangle_properties(ast, options) {
} }
// either debug mode is off, or it is on and we could not use the mangled name // either debug mode is off, or it is on and we could not use the mangled name
if (!mangled) do { if (!mangled) do {
mangled = base54(++cname); mangled = nameGenerator(++cname);
} while (!can_mangle(mangled)); } while (!can_mangle(mangled));
cache.set(name, mangled); cache.set(name, mangled);
} }

View File

@ -332,14 +332,14 @@ function next_mangled_name(scope, options, def) {
}); });
var name; var name;
for (var i = 0; i < holes.length; i++) { for (var i = 0; i < holes.length; i++) {
name = base54(holes[i]); name = nameGenerator(holes[i]);
if (names[name]) continue; if (names[name]) continue;
holes.splice(i, 1); holes.splice(i, 1);
scope.names_in_use[name] = true; scope.names_in_use[name] = true;
return name; return name;
} }
while (true) { while (true) {
name = base54(++scope.cname); name = nameGenerator(++scope.cname);
if (in_use[name] || RESERVED_WORDS[name] || options.reserved.has[name]) continue; if (in_use[name] || RESERVED_WORDS[name] || options.reserved.has[name]) continue;
if (!names[name]) break; if (!names[name]) break;
holes.push(scope.cname); holes.push(scope.cname);
@ -419,7 +419,7 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options) {
if (node instanceof AST_Label) { if (node instanceof AST_Label) {
var name; var name;
do { do {
name = base54(++lname); name = nameGenerator(++lname);
} while (RESERVED_WORDS[name]); } while (RESERVED_WORDS[name]);
node.mangled_name = name; node.mangled_name = name;
return true; return true;
@ -476,8 +476,8 @@ AST_Toplevel.DEFMETHOD("find_colliding_names", function(options) {
}); });
AST_Toplevel.DEFMETHOD("expand_names", function(options) { AST_Toplevel.DEFMETHOD("expand_names", function(options) {
base54.reset(); nameGenerator.reset();
base54.sort(); nameGenerator.sort();
options = _default_mangler_options(options); options = _default_mangler_options(options);
var avoid = this.find_colliding_names(options); var avoid = this.find_colliding_names(options);
var cname = 0; var cname = 0;
@ -490,7 +490,7 @@ AST_Toplevel.DEFMETHOD("expand_names", function(options) {
function next_name() { function next_name() {
var name; var name;
do { do {
name = base54(cname++); name = nameGenerator(cname++);
} while (avoid[name] || RESERVED_WORDS[name]); } while (avoid[name] || RESERVED_WORDS[name]);
return name; return name;
} }
@ -517,29 +517,29 @@ AST_Sequence.DEFMETHOD("tail_node", function() {
AST_Toplevel.DEFMETHOD("compute_char_frequency", function(options) { AST_Toplevel.DEFMETHOD("compute_char_frequency", function(options) {
options = _default_mangler_options(options); options = _default_mangler_options(options);
base54.reset(); nameGenerator.reset();
try { try {
AST_Node.prototype.print = function(stream, force_parens) { AST_Node.prototype.print = function(stream, force_parens) {
this._print(stream, force_parens); this._print(stream, force_parens);
if (this instanceof AST_Symbol && !this.unmangleable(options)) { if (this instanceof AST_Symbol && !this.unmangleable(options)) {
base54.consider(this.name, -1); nameGenerator.consider(this.name, -1);
} else if (options.properties) { } else if (options.properties) {
if (this instanceof AST_Dot) { if (this instanceof AST_Dot) {
base54.consider(this.property, -1); nameGenerator.consider(this.property, -1);
} else if (this instanceof AST_Sub) { } else if (this instanceof AST_Sub) {
skip_string(this.property); skip_string(this.property);
} }
} }
}; };
base54.consider(this.print_to_string(), 1); nameGenerator.consider(this.print_to_string(), 1);
} finally { } finally {
AST_Node.prototype.print = AST_Node.prototype._print; AST_Node.prototype.print = AST_Node.prototype._print;
} }
base54.sort(); nameGenerator.sort();
function skip_string(node) { function skip_string(node) {
if (node instanceof AST_String) { if (node instanceof AST_String) {
base54.consider(node.value, -1); nameGenerator.consider(node.value, -1);
} else if (node instanceof AST_Conditional) { } else if (node instanceof AST_Conditional) {
skip_string(node.consequent); skip_string(node.consequent);
skip_string(node.alternative); skip_string(node.alternative);
@ -549,7 +549,7 @@ AST_Toplevel.DEFMETHOD("compute_char_frequency", function(options) {
} }
}); });
var base54 = (function() { var nameGenerator = (function() {
var freq = Object.create(null); var freq = Object.create(null);
function init(chars) { function init(chars) {
var array = []; var array = [];
@ -566,7 +566,7 @@ var base54 = (function() {
function reset() { function reset() {
frequency = Object.create(freq); frequency = Object.create(freq);
} }
base54.consider = function(str, delta) { nameGenerator.consider = function(str, delta) {
for (var i = str.length; --i >= 0;) { for (var i = str.length; --i >= 0;) {
frequency[str[i]] += delta; frequency[str[i]] += delta;
} }
@ -574,12 +574,12 @@ var base54 = (function() {
function compare(a, b) { function compare(a, b) {
return frequency[b] - frequency[a]; return frequency[b] - frequency[a];
} }
base54.sort = function() { nameGenerator.sort = function() {
chars = leading.sort(compare).concat(digits.sort(compare)); chars = leading.sort(compare).concat(digits.sort(compare));
}; };
base54.reset = reset; nameGenerator.reset = reset;
reset(); reset();
function base54(num) { function nameGenerator(num) {
var ret = "", base = 54; var ret = "", base = 54;
num++; num++;
do { do {
@ -590,5 +590,5 @@ var base54 = (function() {
} while (num > 0); } while (num > 0);
return ret; return ret;
} }
return base54; return nameGenerator;
})(); })();