EncompassingPaths returns the minimal set of paths that root all paths from the original collection. For example, ["/foo", "/foo/bar", "/foo", "/baz"] -> ["/foo", "/baz].
(paths []string)
| 82 | // |
| 83 | // For example, ["/foo", "/foo/bar", "/foo", "/baz"] -> ["/foo", "/baz]. |
| 84 | func EncompassingPaths(paths []string) []string { |
| 85 | result := []string{} |
| 86 | for _, current := range paths { |
| 87 | isCovered := false |
| 88 | hasRemovals := false |
| 89 | |
| 90 | for i, existing := range result { |
| 91 | if IsChild(existing, current) { |
| 92 | // The path is already covered, so there's no need to include it |
| 93 | isCovered = true |
| 94 | break |
| 95 | } |
| 96 | |
| 97 | if IsChild(current, existing) { |
| 98 | // Mark the element empty for removal. |
| 99 | result[i] = "" |
| 100 | hasRemovals = true |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | if !isCovered { |
| 105 | result = append(result, current) |
| 106 | } |
| 107 | |
| 108 | if hasRemovals { |
| 109 | // Remove all the empties |
| 110 | newResult := []string{} |
| 111 | for _, r := range result { |
| 112 | if r != "" { |
| 113 | newResult = append(newResult, r) |
| 114 | } |
| 115 | } |
| 116 | result = newResult |
| 117 | } |
| 118 | } |
| 119 | return result |
| 120 | } |