Skip to content

Commit

Permalink
[Refactor] parse: tweak the regex to not match nothing
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Apr 7, 2023
1 parent 7bcd90e commit 227d474
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
11 changes: 6 additions & 5 deletions parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var DQ = '"';
var DS = '$';

var TOKEN = '';
var mult = Math.pow(16, 8);
var mult = 0x100000000; // Math.pow(16, 8);
for (var i = 0; i < 4; i++) {
TOKEN += (mult * Math.random()).toString(16);
}
Expand All @@ -31,11 +31,12 @@ function parseInternal(s, env, opts) {

var chunker = new RegExp([
'(' + CONTROL + ')', // control chars
'(' + BAREWORD + '|' + SINGLE_QUOTE + '|' + DOUBLE_QUOTE + ')*'
'(' + BAREWORD + '|' + SINGLE_QUOTE + '|' + DOUBLE_QUOTE + ')+'
].join('|'), 'g');
var match = s.match(chunker).filter(Boolean);

if (!match) {
var matches = s.match(chunker);

if (!matches) {
return [];
}
if (!env) {
Expand All @@ -58,7 +59,7 @@ function parseInternal(s, env, opts) {
return pre + r;
}

return match.map(function (s, j) {
return matches.filter(Boolean).map(function (s, j, match) {
if (commented) {
return void undefined;
}
Expand Down
2 changes: 2 additions & 0 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ var test = require('tape');
var parse = require('../').parse;

test('parse shell commands', function (t) {
t.same(parse(''), [], 'parses an empty string');

t.same(parse('a \'b\' "c"'), ['a', 'b', 'c']);
t.same(
parse('beep "boop" \'foo bar baz\' "it\'s \\"so\\" groovy"'),
Expand Down

0 comments on commit 227d474

Please sign in to comment.