File tree Expand file tree Collapse file tree 1 file changed +27
-4
lines changed
Sources/SwiftFormat/Utilities Expand file tree Collapse file tree 1 file changed +27
-4
lines changed Original file line number Diff line number Diff line change 1212
1313import Foundation
1414
15+ #if os(Windows)
16+ import WinSDK
17+ #endif
18+
1519extension 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}
You can’t perform that action at this time.
0 commit comments