import { MatcherBaseClass, MatchOptions, RepeatMatch } from '../../types' import Scoring from '../../scoring' import Options from '../../Options' import { MaybePromise } from 'rollup' /* *------------------------------------------------------------------------------- * repeats (aaa, abcabcabc) ------------------------------ *------------------------------------------------------------------------------- */ class MatchRepeat extends MatcherBaseClass { // the u flag makes '.' match a whole code point, so a repeat can't start or // end in the middle of an emoji (surrogate pair) private static readonly GREEDY = /(.+)\1+/gu private static readonly LAZY = /(.+?)\1+/gu private static readonly LAZY_ANCHORED = /^(.+?)\1+$/u private scoring: Scoring constructor(options: Options) { super(options) this.scoring = new Scoring(options) } // eslint-disable-next-line max-statements match(matchOptions: MatchOptions) { const { password } = matchOptions // Scoped to this call rather than an instance field: getBaseGuesses // recurses back into this same instance via omniMatch.match(baseToken), // and a shared field would get cleared by that nested call before the // outer loop's later iterations could reuse it. const memoizedResults = new Map>() const matches: (RepeatMatch | Promise)[] = [] let lastIndex = 0 while (lastIndex < password.length) { const greedyMatch = this.getGreedyMatch(password, lastIndex) const lazyMatch = this.getLazyMatch(password, lastIndex) if (greedyMatch == null) { break } const { match, baseToken } = this.setMatchToken(greedyMatch, lazyMatch) if (match) { const j = match.index + match[0].length - 1 const baseGuesses = this.getBaseGuesses( baseToken, matchOptions, memoizedResults, ) matches.push(this.normalizeMatch(baseToken, j, match, baseGuesses)) lastIndex = j + 1 } } const hasPromises = matches.some((match) => { return match instanceof Promise }) if (hasPromises) { // eslint-disable-next-line @typescript-eslint/await-thenable return Promise.all(matches) } return matches as RepeatMatch[] } // eslint-disable-next-line max-params normalizeMatch( baseToken: string, j: number, match: RegExpExecArray, baseGuesses: number | Promise, ): MaybePromise { const baseMatch: RepeatMatch = { pattern: 'repeat', i: match.index, j, token: match[0], baseToken, baseGuesses: 0, repeatCount: match[0].length / baseToken.length, } if (baseGuesses instanceof Promise) { return baseGuesses.then((resolvedBaseGuesses) => { return { ...baseMatch, baseGuesses: resolvedBaseGuesses, } }) } return { ...baseMatch, baseGuesses, } } getGreedyMatch(password: string, lastIndex: number) { MatchRepeat.GREEDY.lastIndex = lastIndex return MatchRepeat.GREEDY.exec(password) } getLazyMatch(password: string, lastIndex: number) { MatchRepeat.LAZY.lastIndex = lastIndex return MatchRepeat.LAZY.exec(password) } setMatchToken( greedyMatch: RegExpExecArray, lazyMatch: RegExpExecArray | null, ) { let match let baseToken = '' if (lazyMatch && greedyMatch[0].length > lazyMatch[0].length) { // greedy beats lazy for 'aabaab' // greedy: [aabaab, aab] // lazy: [aa, a] match = greedyMatch // greedy's repeated string might itself be repeated, eg. // aabaab in aabaabaabaab. // run an anchored lazy match on greedy's repeated string // to find the shortest repeated string const temp = MatchRepeat.LAZY_ANCHORED.exec(match[0]) if (temp) { baseToken = temp[1] } } else { // lazy beats greedy for 'aaaaa' // greedy: [aaaa, aa] // lazy: [aaaaa, a] match = lazyMatch if (match) { baseToken = match[1] } } return { match, baseToken, } } // the base token is matched with the same matchers and user inputs as the // password it repeats in getBaseGuesses( baseToken: string, { omniMatch, userInputsOptions }: MatchOptions, memoizedResults: Map>, ) { // memoizedResults lives for one match() call, which has a single // userInputsOptions, so baseToken alone is a safe key if (memoizedResults.has(baseToken)) { return memoizedResults.get(baseToken)! } const matches = omniMatch.match(baseToken, userInputsOptions) let result: number | Promise if (matches instanceof Promise) { result = matches.then((resolvedMatches) => { const baseAnalysis = this.scoring.mostGuessableMatchSequence( baseToken, resolvedMatches, ) return baseAnalysis.guesses }) } else { const baseAnalysis = this.scoring.mostGuessableMatchSequence( baseToken, matches, ) result = baseAnalysis.guesses } memoizedResults.set(baseToken, result) return result } } export default MatchRepeat