Skip to content

Commit

Permalink
optimize ???? patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Feb 11, 2023
1 parent 61df471 commit 6318308
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 3 deletions.
41 changes: 40 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const minimatch = (
export default minimatch

// Optimized checking for the most common glob patterns.
const starDotExtRE = /^\*+(\.[^!?\*\[\(]*)$/
const starDotExtRE = /^\*+([^+@!?\*\[\(]*)$/
const starDotExtTest = (ext: string) => (f: string) =>
!f.startsWith('.') && f.endsWith(ext)
const starDotExtTestDot = (ext: string) => (f: string) => f.endsWith(ext)
Expand All @@ -55,6 +55,35 @@ const dotStarTest = (f: string) => f !== '.' && f !== '..' && f.startsWith('.')
const starRE = /^\*+$/
const starTest = (f: string) => f.length !== 0 && !f.startsWith('.')
const starTestDot = (f: string) => f.length !== 0 && f !== '.' && f !== '..'
const qmarksRE = /^\?+([^+@!?\*\[\(]*)?$/
const qmarksTestNocase = ([$0, ext = '']: RegExpMatchArray) => {
const noext = qmarksTestNoExt([$0])
if (!ext) return noext
ext = ext.toLowerCase()
return (f: string) => noext(f) && f.toLowerCase().endsWith(ext)
}
const qmarksTestNocaseDot = ([$0, ext = '']: RegExpMatchArray) => {
const noext = qmarksTestNoExtDot([$0])
if (!ext) return noext
ext = ext.toLowerCase()
return (f: string) => noext(f) && f.toLowerCase().endsWith(ext)
}
const qmarksTestDot = ([$0, ext = '']: RegExpMatchArray) => {
const noext = qmarksTestNoExtDot([$0])
return !ext ? noext : (f: string) => noext(f) && f.endsWith(ext)
}
const qmarksTest = ([$0, ext = '']: RegExpMatchArray) => {
const noext = qmarksTestNoExt([$0])
return !ext ? noext : (f: string) => noext(f) && f.endsWith(ext)
}
const qmarksTestNoExt = ([$0]: RegExpMatchArray) => {
const len = $0.length
return (f: string) => f.length === len && !f.startsWith('.')
}
const qmarksTestNoExtDot = ([$0]: RegExpMatchArray) => {
const len = $0.length
return (f: string) => f.length === len && f !== '.' && f !== '..'
}

/* c8 ignore start */
const platform =
Expand Down Expand Up @@ -667,6 +696,16 @@ export class Minimatch {
? starDotExtTestDot
: starDotExtTest
)(m[1])
} else if ((m = pattern.match(qmarksRE))) {
fastTest = (
options.nocase
? options.dot
? qmarksTestNocaseDot
: qmarksTestNocase
: options.dot
? qmarksTestDot
: qmarksTest
)(m)
} else if ((m = pattern.match(starDotStarRE))) {
fastTest = options.dot ? starDotStarTestDot : starDotStarTest
} else if ((m = pattern.match(dotStarRE))) {
Expand Down
60 changes: 60 additions & 0 deletions tap-snapshots/test/basic.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ exports[`test/basic.js TAP basic tests > makeRe *c*?** 1`] = `
/^(?:(?!\\.)(?=.)[^/]*?c[^/]*?[^/][^/]*?[^/]*?)$/
`

exports[`test/basic.js TAP basic tests > makeRe *js 1`] = `
/^(?:(?!\\.)(?=.)[^/]*?js)$/
`

exports[`test/basic.js TAP basic tests > makeRe +(a)!(b)+(c) 1`] = `
/^(?:(?=.)(?:(?!\\.)a)+(?:(?!(?:b)(?:c)+)[^/]*?)(?:c)+)$/
`
Expand Down Expand Up @@ -213,6 +217,10 @@ exports[`test/basic.js TAP basic tests > makeRe /^root:/{s/^[^:]*:[^:]*:([^:]*).
/^(?:\\/\\^root:\\/\\{s\\/(?=.)\\^[^:][^/]*?:[^:][^/]*?:\\([^:]\\)[^/]*?\\.[^/]*?\\$\\/1\\/)$/
`

exports[`test/basic.js TAP basic tests > makeRe ? 1`] = `
/^(?:(?!\\.)(?=.)[^/])$/
`

exports[`test/basic.js TAP basic tests > makeRe ?************c****?**** 1`] = `
/^(?:(?!\\.)(?=.)[^/][^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?c[^/]*?[^/]*?[^/]*?[^/]*?[^/][^/]*?[^/]*?[^/]*?[^/]*?)$/
`
Expand All @@ -237,6 +245,38 @@ exports[`test/basic.js TAP basic tests > makeRe ?***?****c 1`] = `
/^(?:(?!\\.)(?=.)[^/][^/]*?[^/]*?[^/]*?[^/][^/]*?[^/]*?[^/]*?[^/]*?c)$/
`

exports[`test/basic.js TAP basic tests > makeRe ?.js 1`] = `
/^(?:(?!\\.)(?=.)[^/]\\.js)$/
`

exports[`test/basic.js TAP basic tests > makeRe ?.js 2`] = `
/^(?:(?!(?:^|\\/)\\.{1,2}(?:$|\\/))(?=.)[^/]\\.js)$/
`

exports[`test/basic.js TAP basic tests > makeRe ?.js 3`] = `
/^(?:(?!\\.)(?=.)[^/]\\.js)$/i
`

exports[`test/basic.js TAP basic tests > makeRe ?.js 4`] = `
/^(?:(?!(?:^|\\/)\\.{1,2}(?:$|\\/))(?=.)[^/]\\.js)$/i
`

exports[`test/basic.js TAP basic tests > makeRe ?? 1`] = `
/^(?:(?!\\.)(?=.)[^/][^/])$/
`

exports[`test/basic.js TAP basic tests > makeRe ?? 2`] = `
/^(?:(?!(?:^|\\/)\\.{1,2}(?:$|\\/))(?=.)[^/][^/])$/
`

exports[`test/basic.js TAP basic tests > makeRe ?? 3`] = `
/^(?:(?!(?:^|\\/)\\.{1,2}(?:$|\\/))(?=.)[^/][^/])$/i
`

exports[`test/basic.js TAP basic tests > makeRe ?? 4`] = `
/^(?:(?!\\.)(?=.)[^/][^/])$/i
`

exports[`test/basic.js TAP basic tests > makeRe ??**********?****? 1`] = `
/^(?:(?!\\.)(?=.)[^/][^/][^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/][^/]*?[^/]*?[^/]*?[^/]*?[^/])$/
`
Expand All @@ -245,6 +285,26 @@ exports[`test/basic.js TAP basic tests > makeRe ??**********?****c 1`] = `
/^(?:(?!\\.)(?=.)[^/][^/][^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/][^/]*?[^/]*?[^/]*?[^/]*?c)$/
`

exports[`test/basic.js TAP basic tests > makeRe ??? 1`] = `
/^(?:(?!\\.)(?=.)[^/][^/][^/])$/
`

exports[`test/basic.js TAP basic tests > makeRe ??? 2`] = `
/^(?:(?!(?:^|\\/)\\.{1,2}(?:$|\\/))(?=.)[^/][^/][^/])$/
`

exports[`test/basic.js TAP basic tests > makeRe ?js 1`] = `
/^(?:(?!\\.)(?=.)[^/]js)$/
`

exports[`test/basic.js TAP basic tests > makeRe ?js 2`] = `
/^(?:(?!(?:^|\\/)\\.{1,2}(?:$|\\/))(?=.)[^/]js)$/
`

exports[`test/basic.js TAP basic tests > makeRe ?js 3`] = `
/^(?:(?!(?:^|\\/)\\.{1,2}(?:$|\\/))(?=.)[^/]js)$/i
`

exports[`test/basic.js TAP basic tests > makeRe @(*|.*) 1`] = `
/^(?:(?=.)(?:(?!\\.)[^/]*?|\\.[^/]*?))$/
`
Expand Down
21 changes: 19 additions & 2 deletions test/patterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,13 +364,30 @@ module.exports = [
'a',
'a.js',
'js',
'JS',
'a.JS',
'.a.JS',
'.JS',
'.',
'..',
]),
['?', ['a']],
['??', ['js', 'JS']],
['??', ['js', 'JS', '.a'], { dot: true }],
['??', ['js', 'JS', '.a'], { dot: true, nocase: true }],
['??', ['js', 'JS'], { nocase: true }],
['???', []],
['???', ['.js', '.JS'], { dot: true }],
['?.js', ['a.js']],
['?js', []],
['?.js', ['a.js'], { dot: true }],
['?js', ['.js'], { dot: true }],
['?.js', ['a.js', 'a.JS'], { nocase: true }],
['?js', ['.js', '.JS'], {dot: true, nocase: true}],
['?.js', ['a.js', 'a.JS'], { dot: true, nocase: true }],

['*.js', ['a.js']],
['*js', ['a.js', 'js']],
['*.js', ['a.js', '.a.js', '.js'], { dot: true }],
['*.js', ['a.js', 'a.JS'], { nocase: true }],
[
Expand All @@ -385,10 +402,10 @@ module.exports = [
{ dot: true },
],
['.*', ['.a', '.a.js', '.js', '.a.JS', '.JS']],
['*', ['a', 'a.js', 'js', 'a.JS']],
['*', ['a', 'a.js', 'js', 'a.JS', 'JS']],
[
'*',
['.a', '.a.js', '.js', 'a', 'a.js', 'js', 'a.JS', '.a.JS', '.JS'],
['.a', '.a.js', '.js', 'a', 'a.js', 'js', 'a.JS', '.a.JS', '.JS', 'JS'],
{ dot: true },
],
]
Expand Down

0 comments on commit 6318308

Please sign in to comment.