Skip to content

Commit

Permalink
Use subpatterns in isQuantifierNext
Browse files Browse the repository at this point in the history
This doesn't change its behavior, but makes it more readable and easier
to modify.
  • Loading branch information
josephfrazier committed Mar 28, 2017
1 parent 7923d35 commit cddec76
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
14 changes: 9 additions & 5 deletions src/xregexp.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,12 +308,16 @@ function isType(value, type) {
* @returns {Boolean} Whether the next token is a quantifier.
*/
function isQuantifierNext(pattern, pos, flags) {
var inlineCommentPattern = '\\(\\?#[^)]*\\)';
var lineCommentPattern = '#[^#\\n]*';
var quantifierPattern = '[?*+]|{\\d+(?:,\\d*)?}';
var patternsToIgnore = flags.indexOf('x') > -1 ?
// Ignore any leading whitespace, line comments, and inline comments
['\\s', lineCommentPattern, inlineCommentPattern] :
// Ignore any leading inline comments
[inlineCommentPattern];
return nativ.test.call(
flags.indexOf('x') > -1 ?
// Ignore any leading whitespace, line comments, and inline comments
/^(?:\s|#[^#\n]*|\(\?#[^)]*\))*(?:[?*+]|{\d+(?:,\d*)?})/ :
// Ignore any leading inline comments
/^(?:\(\?#[^)]*\))*(?:[?*+]|{\d+(?:,\d*)?})/,
RegExp('^(?:' + patternsToIgnore.join('|') + ')*(?:' + quantifierPattern + ')'),
pattern.slice(pos)
);
}
Expand Down
14 changes: 9 additions & 5 deletions xregexp-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -2959,12 +2959,16 @@ function isType(value, type) {
* @returns {Boolean} Whether the next token is a quantifier.
*/
function isQuantifierNext(pattern, pos, flags) {
var inlineCommentPattern = '\\(\\?#[^)]*\\)';
var lineCommentPattern = '#[^#\\n]*';
var quantifierPattern = '[?*+]|{\\d+(?:,\\d*)?}';
var patternsToIgnore = flags.indexOf('x') > -1 ?
// Ignore any leading whitespace, line comments, and inline comments
['\\s', lineCommentPattern, inlineCommentPattern] :
// Ignore any leading inline comments
[inlineCommentPattern];
return nativ.test.call(
flags.indexOf('x') > -1 ?
// Ignore any leading whitespace, line comments, and inline comments
/^(?:\s|#[^#\n]*|\(\?#[^)]*\))*(?:[?*+]|{\d+(?:,\d*)?})/ :
// Ignore any leading inline comments
/^(?:\(\?#[^)]*\))*(?:[?*+]|{\d+(?:,\d*)?})/,
RegExp('^(?:' + patternsToIgnore.join('|') + ')*(?:' + quantifierPattern + ')'),
pattern.slice(pos)
);
}
Expand Down

0 comments on commit cddec76

Please sign in to comment.