Browse Source

cluster, dns, repl, tls, util: fix RegExp nits

* Take RegExp creation out of cycles.
* Use test(), not match() in boolean context.
* Remove redundant RegExp parts.

Backport-PR-URL: https://github.com/nodejs/node/pull/14348
PR-URL: https://github.com/nodejs/node/pull/13536
Reviewed-By: Anna Henningsen <anna@addaleax.net>
v6.x
Vse Mozhet Byt 8 years ago
committed by Myles Borins
parent
commit
edbe442938
No known key found for this signature in database GPG Key ID: 933B01F40B5CA946
  1. 2
      lib/_tls_wrap.js
  2. 5
      lib/cluster.js
  3. 6
      lib/dns.js
  4. 7
      lib/repl.js
  5. 10
      lib/util.js

2
lib/_tls_wrap.js

@ -954,7 +954,7 @@ function SNICallback(servername, callback) {
var ctx; var ctx;
this.server._contexts.some(function(elem) { this.server._contexts.some(function(elem) {
if (servername.match(elem[0]) !== null) { if (elem[0].test(servername)) {
ctx = elem[1]; ctx = elem[1];
return true; return true;
} }

5
lib/cluster.js

@ -298,14 +298,13 @@ function masterInit() {
var workerEnv = util._extend({}, process.env); var workerEnv = util._extend({}, process.env);
var execArgv = cluster.settings.execArgv.slice(); var execArgv = cluster.settings.execArgv.slice();
var debugPort = 0; var debugPort = 0;
var debugArgvRE = /^(--inspect|--debug|--debug-(brk|port))(=\d+)?$/;
workerEnv = util._extend(workerEnv, env); workerEnv = util._extend(workerEnv, env);
workerEnv.NODE_UNIQUE_ID = '' + id; workerEnv.NODE_UNIQUE_ID = '' + id;
for (var i = 0; i < execArgv.length; i++) { for (var i = 0; i < execArgv.length; i++) {
var match = execArgv[i].match( var match = execArgv[i].match(debugArgvRE);
/^(--inspect|--debug|--debug-(brk|port))(=\d+)?$/
);
if (match) { if (match) {
if (debugPort === 0) { if (debugPort === 0) {

6
lib/dns.js

@ -297,13 +297,15 @@ exports.setServers = function(servers) {
// servers cares won't have any servers available for resolution // servers cares won't have any servers available for resolution
const orig = cares.getServers(); const orig = cares.getServers();
const newSet = []; const newSet = [];
const IPv6RE = /\[(.*)\]/;
const addrSplitRE = /:\d+$/;
servers.forEach((serv) => { servers.forEach((serv) => {
var ipVersion = isIP(serv); var ipVersion = isIP(serv);
if (ipVersion !== 0) if (ipVersion !== 0)
return newSet.push([ipVersion, serv]); return newSet.push([ipVersion, serv]);
const match = serv.match(/\[(.*)\](?::\d+)?/); const match = serv.match(IPv6RE);
// we have an IPv6 in brackets // we have an IPv6 in brackets
if (match) { if (match) {
ipVersion = isIP(match[1]); ipVersion = isIP(match[1]);
@ -311,7 +313,7 @@ exports.setServers = function(servers) {
return newSet.push([ipVersion, match[1]]); return newSet.push([ipVersion, match[1]]);
} }
const s = serv.split(/:\d+$/)[0]; const s = serv.split(addrSplitRE)[0];
ipVersion = isIP(s); ipVersion = isIP(s);
if (ipVersion !== 0) if (ipVersion !== 0)

7
lib/repl.js

@ -861,6 +861,7 @@ function complete(line, callback) {
const exts = Object.keys(this.context.require.extensions); const exts = Object.keys(this.context.require.extensions);
var indexRe = new RegExp('^index(' + exts.map(regexpEscape).join('|') + var indexRe = new RegExp('^index(' + exts.map(regexpEscape).join('|') +
')$'); ')$');
var versionedFileNamesRe = /-\d+\.\d+/;
completeOn = match[1]; completeOn = match[1];
var subdir = match[2] || ''; var subdir = match[2] || '';
@ -879,7 +880,7 @@ function complete(line, callback) {
name = files[f]; name = files[f];
ext = path.extname(name); ext = path.extname(name);
base = name.slice(0, -ext.length); base = name.slice(0, -ext.length);
if (base.match(/-\d+\.\d+(\.\d+)?/) || name === '.npm') { if (versionedFileNamesRe.test(base) || name === '.npm') {
// Exclude versioned names that 'npm' installs. // Exclude versioned names that 'npm' installs.
continue; continue;
} }
@ -923,7 +924,7 @@ function complete(line, callback) {
// spam.eggs.<|> # completions for 'spam.eggs' with filter '' // spam.eggs.<|> # completions for 'spam.eggs' with filter ''
// foo<|> # all scope vars with filter 'foo' // foo<|> # all scope vars with filter 'foo'
// foo.<|> # completions for 'foo' with filter '' // foo.<|> # completions for 'foo' with filter ''
} else if (line.length === 0 || line[line.length - 1].match(/\w|\.|\$/)) { } else if (line.length === 0 || /\w|\.|\$/.test(line[line.length - 1])) {
match = simpleExpressionRE.exec(line); match = simpleExpressionRE.exec(line);
if (line.length === 0 || match) { if (line.length === 0 || match) {
var expr; var expr;
@ -1175,7 +1176,7 @@ REPLServer.prototype.memory = function memory(cmd) {
self.lines.level.push({ self.lines.level.push({
line: self.lines.length - 1, line: self.lines.length - 1,
depth: depth, depth: depth,
isFunction: /\s*function\s*/.test(cmd) isFunction: /\bfunction\b/.test(cmd)
}); });
} else if (depth < 0) { } else if (depth < 0) {
// going... up. // going... up.

10
lib/util.js

@ -17,6 +17,8 @@ const inspectDefaultOptions = Object.seal({
breakLength: 60 breakLength: 60
}); });
const numbersOnlyRE = /^\d+$/;
var Debug; var Debug;
var simdFormatters; var simdFormatters;
@ -668,7 +670,7 @@ function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
output.push(`... ${remaining} more item${remaining > 1 ? 's' : ''}`); output.push(`... ${remaining} more item${remaining > 1 ? 's' : ''}`);
} }
keys.forEach(function(key) { keys.forEach(function(key) {
if (typeof key === 'symbol' || !key.match(/^\d+$/)) { if (typeof key === 'symbol' || !numbersOnlyRE.test(key)) {
output.push(formatProperty(ctx, value, recurseTimes, visibleKeys, output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
key, true)); key, true));
} }
@ -687,7 +689,7 @@ function formatTypedArray(ctx, value, recurseTimes, visibleKeys, keys) {
output.push(`... ${remaining} more item${remaining > 1 ? 's' : ''}`); output.push(`... ${remaining} more item${remaining > 1 ? 's' : ''}`);
} }
for (const key of keys) { for (const key of keys) {
if (typeof key === 'symbol' || !key.match(/^\d+$/)) { if (typeof key === 'symbol' || !numbersOnlyRE.test(key)) {
output.push( output.push(
formatProperty(ctx, value, recurseTimes, visibleKeys, key, true)); formatProperty(ctx, value, recurseTimes, visibleKeys, key, true));
} }
@ -801,11 +803,11 @@ function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
} }
} }
if (name === undefined) { if (name === undefined) {
if (array && key.match(/^\d+$/)) { if (array && numbersOnlyRE.test(key)) {
return str; return str;
} }
name = JSON.stringify('' + key); name = JSON.stringify('' + key);
if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) { if (/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/.test(name)) {
name = name.substr(1, name.length - 2); name = name.substr(1, name.length - 2);
name = ctx.stylize(name, 'name'); name = ctx.stylize(name, 'name');
} else { } else {

Loading…
Cancel
Save