| 17 | */ |
| 18 | |
| 19 | function normalizeString(path, aar) { |
| 20 | let res = '' |
| 21 | let lastSegmentLength = 0 |
| 22 | let lastSlash = -1 |
| 23 | let dots = 0 |
| 24 | let char = '\x00' |
| 25 | for (let i = 0; i <= path.length; ++i) { |
| 26 | if (i < path.length) char = path[i] |
| 27 | else if (char === '/') break |
| 28 | else char = '/' |
| 29 | |
| 30 | if (char === '/') { |
| 31 | if (lastSlash === i - 1 || dots === 1) { |
| 32 | // NOOP |
| 33 | } else if (dots === 2) { |
| 34 | if ( |
| 35 | res.length < 2 || |
| 36 | lastSegmentLength !== 2 || |
| 37 | res.at(-1) !== '.' || |
| 38 | res.at(-2) !== '.' |
| 39 | ) { |
| 40 | if (res.length > 2) { |
| 41 | const lastSlashIndex = res.lastIndexOf('/') |
| 42 | if (lastSlashIndex === -1) { |
| 43 | res = '' |
| 44 | lastSegmentLength = 0 |
| 45 | } else { |
| 46 | res = res.slice(0, lastSlashIndex) |
| 47 | lastSegmentLength = res.length - 1 - res.lastIndexOf('/') |
| 48 | } |
| 49 | lastSlash = i |
| 50 | dots = 0 |
| 51 | continue |
| 52 | } else if (res.length !== 0) { |
| 53 | res = '' |
| 54 | lastSegmentLength = 0 |
| 55 | lastSlash = i |
| 56 | dots = 0 |
| 57 | continue |
| 58 | } |
| 59 | } |
| 60 | if (aar) { |
| 61 | res += res.length > 0 ? '/..' : '..' |
| 62 | lastSegmentLength = 2 |
| 63 | } |
| 64 | } else { |
| 65 | if (res.length > 0) res += '/' + path.slice(lastSlash + 1, i) |
| 66 | else res = path.slice(lastSlash + 1, i) |
| 67 | lastSegmentLength = i - lastSlash - 1 |
| 68 | } |
| 69 | lastSlash = i |
| 70 | dots = 0 |
| 71 | } else if (char === '.' && dots !== -1) { |
| 72 | ++dots |
| 73 | } else { |
| 74 | dots = -1 |
| 75 | } |
| 76 | } |