Skip to content

Commit 39bd050

Browse files
committed
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 39bd050

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

Sources/SwiftFormat/Utilities/URL+isRoot.swift

+25-3
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,43 @@
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+
#endif
28+
29+
#if compiler(>=6.1)
1730
#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.
31+
return self.path.withCString(encodedAs: UTF16.self, PathCchIsRoot)
32+
#elseif os(Linux)
33+
return self.path == "/"
34+
#endif
35+
#else
36+
37+
#if os(Windows)
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 == "/" {
2242
// Canonicalize `/C:/` to `C:/`.
2343
pathComponents = Array(pathComponents.dropFirst())
2444
}
2545
return pathComponents.count <= 1
26-
#else
46+
#elseif os(Linux)
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
51+
52+
#endif
3153
}
3254
}

0 commit comments

Comments
 (0)