| 391 | } |
| 392 | |
| 393 | func GetNormalizedAbsolutePath(fileName string, currentDirectory string) string { |
| 394 | rootLength := GetRootLength(fileName) |
| 395 | if rootLength == 0 && currentDirectory != "" { |
| 396 | fileName = CombinePaths(currentDirectory, fileName) |
| 397 | } else { |
| 398 | // CombinePaths normalizes slashes, so not necessary in other branch |
| 399 | fileName = NormalizeSlashes(fileName) |
| 400 | } |
| 401 | rootLength = GetRootLength(fileName) |
| 402 | |
| 403 | if simpleNormalized, ok := simpleNormalizePath(fileName); ok { |
| 404 | length := len(simpleNormalized) |
| 405 | if length > rootLength { |
| 406 | return RemoveTrailingDirectorySeparator(simpleNormalized) |
| 407 | } |
| 408 | if length == rootLength && rootLength != 0 { |
| 409 | return EnsureTrailingDirectorySeparator(simpleNormalized) |
| 410 | } |
| 411 | return simpleNormalized |
| 412 | } |
| 413 | |
| 414 | length := len(fileName) |
| 415 | root := fileName[:rootLength] |
| 416 | // `normalized` is only initialized once `fileName` is determined to be non-normalized. |
| 417 | // `changed` is set at the same time. |
| 418 | var changed bool |
| 419 | var normalized string |
| 420 | var segmentStart int |
| 421 | index := rootLength |
| 422 | normalizedUpTo := index |
| 423 | seenNonDotDotSegment := rootLength != 0 |
| 424 | for index < length { |
| 425 | // At beginning of segment |
| 426 | segmentStart = index |
| 427 | ch := fileName[index] |
| 428 | for ch == '/' { |
| 429 | index++ |
| 430 | if index < length { |
| 431 | ch = fileName[index] |
| 432 | } else { |
| 433 | break |
| 434 | } |
| 435 | } |
| 436 | if index > segmentStart { |
| 437 | // Seen superfluous separator |
| 438 | if !changed { |
| 439 | normalized = fileName[:max(rootLength, segmentStart-1)] |
| 440 | changed = true |
| 441 | } |
| 442 | if index == length { |
| 443 | break |
| 444 | } |
| 445 | segmentStart = index |
| 446 | } |
| 447 | // Past any superfluous separators |
| 448 | segmentEnd := strings.IndexByte(fileName[index+1:], '/') |
| 449 | if segmentEnd == -1 { |
| 450 | segmentEnd = length |