Skip to content

Commit 5442503

Browse files
authored
Merge pull request #2278 from compnerd/extra-wide-load
2 parents 32ec786 + 97feb0b commit 5442503

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

Foundation/NSURL.swift

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,13 @@ open class NSURL : NSObject, NSSecureCoding, NSCopying {
581581
CFURLGetFileSystemRepresentation(_cfObject, true, $0, maxBufferLength)
582582
}
583583
}
584-
584+
585+
#if os(Windows)
586+
internal func _getWideFileSystemRepresentation(_ buffer: UnsafeMutablePointer<UInt16>, maxLength: Int) -> Bool {
587+
_CFURLGetWideFileSystemRepresentation(_cfObject, true, buffer, maxLength)
588+
}
589+
#endif
590+
585591
/* Returns the URL's path in file system representation. File system representation is a null-terminated C string with canonical UTF-8 encoding. The returned C string will be automatically freed just as a returned object would be released; your code should copy the representation or use getFileSystemRepresentation:maxLength: if it needs to store the representation outside of the autorelease context in which the representation is created.
586592
*/
587593

@@ -606,7 +612,22 @@ open class NSURL : NSObject, NSSecureCoding, NSCopying {
606612
fatalError("URL cannot be expressed in the filesystem representation;" +
607613
"use getFileSystemRepresentation to handle this case")
608614
}
609-
615+
616+
#if os(Windows)
617+
internal var _wideFileSystemRepresentation: UnsafePointer<UInt16> {
618+
let capacity: Int = Int(MAX_PATH) + 1
619+
let buffer: UnsafeMutablePointer<UInt16> =
620+
UnsafeMutablePointer<UInt16>.allocate(capacity: capacity)
621+
buffer.initialize(repeating: 0, count: capacity)
622+
623+
if _getWideFileSystemRepresentation(buffer, maxLength: capacity) {
624+
return UnsafePointer(buffer)
625+
}
626+
627+
fatalError("URL cannot be expressed in the filesystem representation; use getFileSystemRepresentation to handle this case")
628+
}
629+
#endif
630+
610631
// Whether the scheme is file:; if myURL.isFileURL is true, then myURL.path is suitable for input into FileManager or NSPathUtilities.
611632
open var isFileURL: Bool {
612633
return _CFURLIsFileURL(_cfObject)

Foundation/URL.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,15 @@ public struct URL : ReferenceConvertible, Equatable {
722722
defer { fsRep.deallocate() }
723723
return try block(fsRep)
724724
}
725-
725+
726+
#if os(Windows)
727+
internal func _withUnsafeWideFileSystemRepresentation<ResultType>(_ block: (UnsafePointer<UInt16>?) throws -> ResultType) rethrows -> ResultType {
728+
let fsr: UnsafePointer<UInt16> = _url._wideFileSystemRepresentation
729+
defer { fsr.deallocate() }
730+
return try block(fsr)
731+
}
732+
#endif
733+
726734
// MARK: -
727735
// MARK: Path manipulation
728736

0 commit comments

Comments
 (0)