| 21 | |
| 22 | // Resolves . and .. elements in a path with directory names |
| 23 | function normalizeStringPosix(path: string, allowAboveRoot: boolean) { |
| 24 | let res = "" |
| 25 | let lastSegmentLength = 0 |
| 26 | let lastSlash = -1 |
| 27 | let dots = 0 |
| 28 | let code |
| 29 | for (let i = 0; i <= path.length; ++i) { |
| 30 | if (i < path.length) { |
| 31 | code = path.charCodeAt(i) |
| 32 | } else if (code === 47 /*/*/) { |
| 33 | break |
| 34 | } else { |
| 35 | code = 47 /*/*/ |
| 36 | } |
| 37 | if (code === 47 /*/*/) { |
| 38 | if (lastSlash === i - 1 || dots === 1) { |
| 39 | // NOOP |
| 40 | } else if (lastSlash !== i - 1 && dots === 2) { |
| 41 | if ( |
| 42 | res.length < 2 || lastSegmentLength !== 2 || res.charCodeAt(res.length - 1) !== 46 /*.*/ || |
| 43 | res.charCodeAt(res.length - 2) !== 46 /*.*/ |
| 44 | ) { |
| 45 | if (res.length > 2) { |
| 46 | const lastSlashIndex = res.lastIndexOf("/") |
| 47 | if (lastSlashIndex !== res.length - 1) { |
| 48 | if (lastSlashIndex === -1) { |
| 49 | res = "" |
| 50 | lastSegmentLength = 0 |
| 51 | } else { |
| 52 | res = res.slice(0, lastSlashIndex) |
| 53 | lastSegmentLength = res.length - 1 - res.lastIndexOf("/") |
| 54 | } |
| 55 | lastSlash = i |
| 56 | dots = 0 |
| 57 | continue |
| 58 | } |
| 59 | } else if (res.length === 2 || res.length === 1) { |
| 60 | res = "" |
| 61 | lastSegmentLength = 0 |
| 62 | lastSlash = i |
| 63 | dots = 0 |
| 64 | continue |
| 65 | } |
| 66 | } |
| 67 | if (allowAboveRoot) { |
| 68 | if (res.length > 0) { |
| 69 | res += "/.." |
| 70 | } else { |
| 71 | res = ".." |
| 72 | } |
| 73 | lastSegmentLength = 2 |
| 74 | } |
| 75 | } else { |
| 76 | if (res.length > 0) { |
| 77 | res += "/" + path.slice(lastSlash + 1, i) |
| 78 | } else { |
| 79 | res = path.slice(lastSlash + 1, i) |
| 80 | } |