Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libs: fix some RegExp nits #13536

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
libs: take RegExp creation out of cycles
  • Loading branch information
vsemozhetbyt committed Jun 7, 2017
commit 7b2ddb4ca6824eafe7a8943eab1ea1670c9dcf60
6 changes: 4 additions & 2 deletions lib/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,21 +309,23 @@ function setServers(servers) {
// servers cares won't have any servers available for resolution
const orig = cares.getServers();
const newSet = [];
const IPv6RE = /\[(.*)\](?::\d+)?/;
const addrSplitRE = /:\d+$/;

servers.forEach((serv) => {
var ipVersion = isIP(serv);
if (ipVersion !== 0)
return newSet.push([ipVersion, serv]);

const match = serv.match(/\[(.*)\](?::\d+)?/);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this was meant to be anchored, i.e. ^…$, otherwise the addition doesn’t make much sense? Anyway, your change seems correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this was for some more clarity, to show the wider pattern. But this may impact performance a bit.

const match = serv.match(IPv6RE);
// we have an IPv6 in brackets
if (match) {
ipVersion = isIP(match[1]);
if (ipVersion !== 0)
return newSet.push([ipVersion, match[1]]);
}

const s = serv.split(/:\d+$/)[0];
const s = serv.split(addrSplitRE)[0];
ipVersion = isIP(s);

if (ipVersion !== 0)
Expand Down
6 changes: 3 additions & 3 deletions lib/internal/cluster/master.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ function createWorkerProcess(id, env) {
var workerEnv = util._extend({}, process.env);
var execArgv = cluster.settings.execArgv.slice();
var debugPort = 0;
var debugArgvRE =
/^(--inspect|--inspect-(brk|port)|--debug|--debug-(brk|port))(=\d+)?$/;

util._extend(workerEnv, env);
workerEnv.NODE_UNIQUE_ID = '' + id;

for (var i = 0; i < execArgv.length; i++) {
const match = execArgv[i].match(
/^(--inspect|--inspect-(brk|port)|--debug|--debug-(brk|port))(=\d+)?$/
);
const match = execArgv[i].match(debugArgvRE);

if (match) {
if (debugPort === 0) {
Expand Down
3 changes: 2 additions & 1 deletion lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,7 @@ function complete(line, callback) {
const exts = Object.keys(this.context.require.extensions);
var indexRe = new RegExp('^index(' + exts.map(regexpEscape).join('|') +
')$');
var versionedFileNamesRe = /-\d+\.\d+(\.\d+)?/;

completeOn = match[1];
var subdir = match[2] || '';
Expand All @@ -772,7 +773,7 @@ function complete(line, callback) {
name = files[f];
ext = path.extname(name);
base = name.slice(0, -ext.length);
if (base.match(/-\d+\.\d+(\.\d+)?/) || name === '.npm') {
if (base.match(versionedFileNamesRe) || name === '.npm') {
// Exclude versioned names that 'npm' installs.
continue;
}
Expand Down
8 changes: 5 additions & 3 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ const inspectDefaultOptions = Object.seal({
breakLength: 60
});

const numbersOnlyRE = /^\d+$/;

var CIRCULAR_ERROR_MESSAGE;
var Debug;

Expand Down Expand Up @@ -664,7 +666,7 @@ function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
}
for (var n = 0; n < keys.length; n++) {
var key = keys[n];
if (typeof key === 'symbol' || !key.match(/^\d+$/)) {
if (typeof key === 'symbol' || !key.match(numbersOnlyRE)) {
output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
key, true));
}
Expand All @@ -683,7 +685,7 @@ function formatTypedArray(ctx, value, recurseTimes, visibleKeys, keys) {
output.push(`... ${remaining} more item${remaining > 1 ? 's' : ''}`);
}
for (const key of keys) {
if (typeof key === 'symbol' || !key.match(/^\d+$/)) {
if (typeof key === 'symbol' || !key.match(numbersOnlyRE)) {
output.push(
formatProperty(ctx, value, recurseTimes, visibleKeys, key, true));
}
Expand Down Expand Up @@ -798,7 +800,7 @@ function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
}
}
if (name === undefined) {
if (array && key.match(/^\d+$/)) {
if (array && key.match(numbersOnlyRE)) {
return str;
}
name = JSON.stringify('' + key);
Expand Down