swiftlang / swift-foundation

The Foundation project
Apache License 2.0
2.4k stars 159 forks source link

`URL.withUnsafeFileSystemRepresentation` drops trailing backslash even if it is significant on Windows #976

Closed ahoppen closed 1 day ago

ahoppen commented 1 month ago
let url = URL(fileURLWithPath: #"C:\"#)
url.withUnsafeFileSystemRepresentation { filePath in
    print(String(cString: filePath! , encoding: .utf8))
}

prints C:. Dropping the trailing backslash is problematic here because C:\ is considered a root directory in Windows but C: is not (see table of examples in https://learn.microsoft.com/en-us/windows/win32/api/pathcch/nf-pathcch-pathcchisroot).