Skip to content

Commit 4413e7c

Browse files
committed
PathCchIsRoot Updated isRoot extension.
Now that swiftlang/swift-foundation#976 and swiftlang/swift-foundation#980 are fixed we can clean this code up a bit by removing the empty path check on Linux and by using the native `PathCchIsRoot` on Windows.
1 parent 3b27ab5 commit 4413e7c

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

Sources/SwiftFormat/Utilities/URL+isRoot.swift

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,19 @@
1212

1313
import Foundation
1414

15+
#if os(Windows)
16+
import WinSDK
17+
#endif
18+
1519
extension URL {
1620
@_spi(Testing) public var isRoot: Bool {
17-
#if os(Windows)
18-
// FIXME: We should call into Windows' native check to check if this path is a root once https://github.com/swiftlang/swift-foundation/issues/976 is fixed.
19-
// https://github.com/swiftlang/swift-format/issues/844
20-
var pathComponents = self.pathComponents
21-
if pathComponents.first == "/" {
22-
// Canonicalize `/C:/` to `C:/`.
23-
pathComponents = Array(pathComponents.dropFirst())
24-
}
25-
return pathComponents.count <= 1
21+
guard isFileURL else { return false }
22+
#if os(macOS)
23+
return self.path == NSOpenStepRootDirectory()
24+
#elseif os(Windows)
25+
return self.path.withCString(encodedAs: UTF16.self, PathCchIsRoot)
2626
#else
27-
// On Linux, we may end up with an string for the path due to https://github.com/swiftlang/swift-foundation/issues/980
28-
// TODO: Remove the check for "" once https://github.com/swiftlang/swift-foundation/issues/980 is fixed.
29-
return self.path == "/" || self.path == ""
27+
return self.path == "/"
3028
#endif
3129
}
3230
}

0 commit comments

Comments
 (0)