| 761 | // Thompson simulation over code points: one match costs path length × token |
| 762 | // count. A path is matched once per ancestor, which adds a depth factor. |
| 763 | const matchTokens = (tokens: ReadonlyArray<DockerToken>, path: string): boolean => { |
| 764 | const end = tokens.length |
| 765 | const seen = new Int32Array(end + 1) |
| 766 | let generation = 1 |
| 767 | let current: Array<number> = [] |
| 768 | let next: Array<number> = [] |
| 769 | const stack: Array<number> = [] |
| 770 | const add = (list: Array<number>, start: number) => { |
| 771 | stack.push(start) |
| 772 | while (stack.length > 0) { |
| 773 | const state = stack.pop()! |
| 774 | if (seen[state] === generation) { |
| 775 | continue |
| 776 | } |
| 777 | seen[state] = generation |
| 778 | list.push(state) |
| 779 | const token = tokens[state] |
| 780 | if (token?._tag === "Star" || token?._tag === "StarAll") { |
| 781 | stack.push(state + 1) |
| 782 | } else if (token?._tag === "GroupStart") { |
| 783 | stack.push(state + 2, state + 1) |
| 784 | } |
| 785 | } |
| 786 | } |
| 787 | add(current, 0) |
| 788 | for (let c = 0; c < path.length;) { |
| 789 | const code = path.codePointAt(c)! |
| 790 | c += code > 0xffff ? 2 : 1 |
| 791 | generation++ |
| 792 | next.length = 0 |
| 793 | for (const state of current) { |
| 794 | const token = tokens[state] |
| 795 | switch (token?._tag) { |
| 796 | case "Char": { |
| 797 | if (token.code === code) add(next, state + 1) |
| 798 | break |
| 799 | } |
| 800 | case "Any": { |
| 801 | if (code !== 0x2f) add(next, state + 1) |
| 802 | break |
| 803 | } |
| 804 | case "Class": { |
| 805 | if (Glob.matchToken(token, code)) add(next, state + 1) |
| 806 | break |
| 807 | } |
| 808 | case "Star": { |
| 809 | if (code !== 0x2f) add(next, state) |
| 810 | break |
| 811 | } |
| 812 | case "StarAll": { |
| 813 | if (code !== 0x0a) add(next, state) |
| 814 | break |
| 815 | } |
| 816 | case "GroupBody": { |
| 817 | if (code !== 0x0a) add(next, state) |
| 818 | if (code === 0x2f) add(next, state + 1) |
| 819 | break |
| 820 | } |