| 315 | |
| 316 | // Resolves . and .. elements in a path with directory names |
| 317 | function normalizeStringPosix(path: string, allowAboveRoot: boolean) { |
| 318 | let res = "" |
| 319 | let lastSegmentLength = 0 |
| 320 | let lastSlash = -1 |
| 321 | let dots = 0 |
| 322 | let code |
| 323 | for (let i = 0; i <= path.length; ++i) { |
| 324 | if (i < path.length) { |
| 325 | code = path.charCodeAt(i) |
| 326 | } else if (code === 47 /*/*/) { |
| 327 | break |
| 328 | } else { |
| 329 | code = 47 /*/*/ |
| 330 | } |
| 331 | if (code === 47 /*/*/) { |
| 332 | if (lastSlash === i - 1 || dots === 1) { |
| 333 | // NOOP |
| 334 | } else if (lastSlash !== i - 1 && dots === 2) { |
| 335 | if ( |
| 336 | res.length < 2 || lastSegmentLength !== 2 || res.charCodeAt(res.length - 1) !== 46 /*.*/ || |
| 337 | res.charCodeAt(res.length - 2) !== 46 /*.*/ |
| 338 | ) { |
| 339 | if (res.length > 2) { |
| 340 | const lastSlashIndex = res.lastIndexOf("/") |
| 341 | if (lastSlashIndex !== res.length - 1) { |
| 342 | if (lastSlashIndex === -1) { |
| 343 | res = "" |
| 344 | lastSegmentLength = 0 |
| 345 | } else { |
| 346 | res = res.slice(0, lastSlashIndex) |
| 347 | lastSegmentLength = res.length - 1 - res.lastIndexOf("/") |
| 348 | } |
| 349 | lastSlash = i |
| 350 | dots = 0 |
| 351 | continue |
| 352 | } |
| 353 | } else if (res.length === 2 || res.length === 1) { |
| 354 | res = "" |
| 355 | lastSegmentLength = 0 |
| 356 | lastSlash = i |
| 357 | dots = 0 |
| 358 | continue |
| 359 | } |
| 360 | } |
| 361 | if (allowAboveRoot) { |
| 362 | if (res.length > 0) { |
| 363 | res += "/.." |
| 364 | } else { |
| 365 | res = ".." |
| 366 | } |
| 367 | lastSegmentLength = 2 |
| 368 | } |
| 369 | } else { |
| 370 | if (res.length > 0) { |
| 371 | res += "/" + path.slice(lastSlash + 1, i) |
| 372 | } else { |
| 373 | res = path.slice(lastSlash + 1, i) |
| 374 | } |