Relative Paths
(from string, to string, options ComparePathsOptions)
| 727 | //// Relative Paths |
| 728 | |
| 729 | func GetPathComponentsRelativeTo(from string, to string, options ComparePathsOptions) []string { |
| 730 | fromComponents := reducePathComponents(GetPathComponents(from, options.CurrentDirectory)) |
| 731 | toComponents := reducePathComponents(GetPathComponents(to, options.CurrentDirectory)) |
| 732 | |
| 733 | start := 0 |
| 734 | maxCommonComponents := min(len(fromComponents), len(toComponents)) |
| 735 | stringEqualer := options.getEqualityComparer() |
| 736 | for ; start < maxCommonComponents; start++ { |
| 737 | fromComponent := fromComponents[start] |
| 738 | toComponent := toComponents[start] |
| 739 | if start == 0 { |
| 740 | if !stringutil.EquateStringCaseInsensitive(fromComponent, toComponent) { |
| 741 | break |
| 742 | } |
| 743 | } else { |
| 744 | if !stringEqualer(fromComponent, toComponent) { |
| 745 | break |
| 746 | } |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | if start == 0 { |
| 751 | return toComponents |
| 752 | } |
| 753 | |
| 754 | numDotDotSlashes := len(fromComponents) - start |
| 755 | result := make([]string, 1+numDotDotSlashes+len(toComponents)-start) |
| 756 | |
| 757 | result[0] = "" |
| 758 | i := 1 |
| 759 | // Add all the relative components until we hit a common directory. |
| 760 | for range numDotDotSlashes { |
| 761 | result[i] = ".." |
| 762 | i++ |
| 763 | } |
| 764 | // Now add all the remaining components of the "to" path. |
| 765 | for _, component := range toComponents[start:] { |
| 766 | result[i] = component |
| 767 | i++ |
| 768 | } |
| 769 | |
| 770 | return result |
| 771 | } |
| 772 | |
| 773 | func GetRelativePathFromDirectory(fromDirectory string, to string, options ComparePathsOptions) string { |
| 774 | if (GetRootLength(fromDirectory) > 0) != (GetRootLength(to) > 0) { |
no test coverage detected