Components splits the path into its components. This calls filepath.Split repeatedly. The path is expected to be normalized.
(path string)
| 118 | // |
| 119 | // The path is expected to be normalized. |
| 120 | func Components(path string) []string { |
| 121 | var components []string |
| 122 | |
| 123 | if len(path) < 1 { |
| 124 | return []string{"."} |
| 125 | } |
| 126 | |
| 127 | dir := Unnormalize(path) |
| 128 | |
| 129 | volumeComponent := filepath.VolumeName(dir) |
| 130 | if len(volumeComponent) > 0 { |
| 131 | // On Windows the volume of an absolute path could be one of the following 3 forms |
| 132 | // c.f. https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file?redirectedfrom=MSDN#fully-qualified-vs-relative-paths |
| 133 | // * A disk designator: `C:\` |
| 134 | // * A UNC Path: `\\servername\share\` |
| 135 | // c.f. https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-dfsc/149a3039-98ce-491a-9268-2f5ddef08192 |
| 136 | // * A "current volume absolute path" `\` |
| 137 | // This refers to the root of the current volume |
| 138 | // |
| 139 | // We do not support paths with string parsing disabled such as |
| 140 | // `\\?\path` |
| 141 | // |
| 142 | // If we did extract a volume name, we need to add a path separator to turn it into |
| 143 | // a path component. Volume Names without path separators have an implied "current directory" |
| 144 | // when performing a join operation, or using them as a path directly, which is not the |
| 145 | // intention of `Split` so we ensure they always mean "the root of this volume". |
| 146 | volumeComponent = volumeComponent + stringOSPathSeparator |
| 147 | } |
| 148 | if len(volumeComponent) < 1 && dir[0] == os.PathSeparator { |
| 149 | // If we didn't extract a volume name then the path is either |
| 150 | // absolute and starts with an os.PathSeparator (it must be exactly 1 |
| 151 | // otherwise its a UNC path and we would have found a volume above) or it is relative. |
| 152 | // If it is absolute, we set the expected volume component to os.PathSeparator. |
| 153 | // otherwise we leave it as an empty string. |
| 154 | volumeComponent = stringOSPathSeparator |
| 155 | } |
| 156 | for { |
| 157 | var file string |
| 158 | dir, file = filepath.Split(dir) |
| 159 | // puts in reverse |
| 160 | components = append(components, file) |
| 161 | |
| 162 | if dir == volumeComponent { |
| 163 | if volumeComponent != "" { |
| 164 | components = append(components, dir) |
| 165 | } |
| 166 | break |
| 167 | } |
| 168 | |
| 169 | dir = strings.TrimSuffix(dir, stringOSPathSeparator) |
| 170 | } |
| 171 | slices.Reverse(components) |
| 172 | for i, component := range components { |
| 173 | components[i] = Normalize(component) |
| 174 | } |
| 175 | return components |
| 176 | } |
no test coverage detected
searching dependent graphs…