Skip to content

Commit ed01028

Browse files
authored
Merge pull request #892 from macshome/PathCchIsRoot
PathCchIsRoot Updated isRoot extension.
2 parents 3b27ab5 + f077071 commit ed01028

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

Sources/SwiftFormat/Utilities/URL+isRoot.swift

+27-4
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,44 @@
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 compiler(>=6.1)
26+
#if os(Windows)
27+
let filePath = self.withUnsafeFileSystemRepresentation { pointer in
28+
guard let pointer else {
29+
return ""
30+
}
31+
return String(cString: pointer)
32+
}
33+
return filePath.withCString(encodedAs: UTF16.self, PathCchIsRoot)
34+
#else // os(Windows)
35+
return self.path == "/"
36+
#endif // os(Windows)
37+
#else // compiler(>=6.1)
38+
1739
#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.
40+
// This is needed as the fixes from #844 aren't in the Swift 6.0 toolchain.
1941
// https://github.com/swiftlang/swift-format/issues/844
2042
var pathComponents = self.pathComponents
2143
if pathComponents.first == "/" {
2244
// Canonicalize `/C:/` to `C:/`.
2345
pathComponents = Array(pathComponents.dropFirst())
2446
}
2547
return pathComponents.count <= 1
26-
#else
48+
#else // os(Windows)
2749
// 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.
50+
// This is needed as the fixes from #980 aren't in the Swift 6.0 toolchain.
2951
return self.path == "/" || self.path == ""
30-
#endif
52+
#endif // os(Windows)
53+
#endif // compiler(>=6.1)
3154
}
3255
}

0 commit comments

Comments
 (0)