Skip to content

Commit 5c78f86

Browse files
committed
PathCchIsRoot PathCchIsRoot 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. Existing code is retained for toolchains <6.1
1 parent 3b27ab5 commit 5c78f86

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

Sources/SwiftFormat/Utilities/URL+isRoot.swift

+22-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,30 @@
1212

1313
import Foundation
1414

15+
#if os(Windows)
16+
import WinSDK
17+
#endif
18+
1519
extension URL {
20+
/// Returns a `Bool` to indicate if the given `URL` leads to the root of a filesystem.
21+
/// A non-filesystem type `URL` will always return false.
1622
@_spi(Testing) public var isRoot: Bool {
23+
guard isFileURL else { return false }
24+
25+
#if os(macOS)
26+
return self.path == NSOpenStepRootDirectory()
27+
28+
#elseif compiler(>=6.1)
29+
#if os(Windows)
30+
let pathPointer = self.withUnsafeFileSystemRepresentation { (cString:$0 ?? "") }
31+
return PathCchIsRoot(pathPointer)
32+
#else
33+
return self.path == "/"
34+
#endif
35+
#endif
36+
1737
#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.
38+
// This is needed as the fixes from #844 aren't in the Swift 6.0 toolchain.
1939
// https://github.com/swiftlang/swift-format/issues/844
2040
var pathComponents = self.pathComponents
2141
if pathComponents.first == "/" {
@@ -25,7 +45,7 @@ extension URL {
2545
return pathComponents.count <= 1
2646
#else
2747
// 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.
48+
// This is needed as the fixes from #980 aren't in the Swift 6.0 toolchain.
2949
return self.path == "/" || self.path == ""
3050
#endif
3151
}

0 commit comments

Comments
 (0)