Skip to content

Commit 7fa60fa

Browse files
committed
Merge branch 'itaiferber-pr-urls'
2 parents 0f2f4f8 + 25c83ef commit 7fa60fa

22 files changed

+1163
-4366
lines changed

Foundation/NSBundle.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,7 @@ open class Bundle: NSObject {
5050
}
5151

5252
public convenience init?(url: URL) {
53-
if let path = url.path {
54-
self.init(path: path)
55-
} else {
56-
return nil
57-
}
53+
self.init(path: url.path)
5854
}
5955

6056
public init(for aClass: AnyClass) { NSUnimplemented() }
@@ -70,7 +66,7 @@ open class Bundle: NSObject {
7066
}
7167

7268
override open var description: String {
73-
return "\(String(describing: Bundle.self)) <\(bundleURL.path!)> (\(isLoaded ? "loaded" : "not yet loaded"))"
69+
return "\(String(describing: Bundle.self)) <\(bundleURL.path)> (\(isLoaded ? "loaded" : "not yet loaded"))"
7470
}
7571

7672

@@ -144,7 +140,7 @@ open class Bundle: NSObject {
144140
}
145141

146142
open var bundlePath: String {
147-
return bundleURL.path!
143+
return bundleURL.path
148144
}
149145

150146
open var resourcePath: String? {

Foundation/NSData.swift

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ extension NSData {
336336

337337
public convenience init(contentsOf url: URL, options readOptionsMask: ReadingOptions) throws {
338338
if url.isFileURL {
339-
try self.init(contentsOfFile: url.path!, options: readOptionsMask)
339+
try self.init(contentsOfFile: url.path, options: readOptionsMask)
340340
} else {
341341
let session = URLSession(configuration: URLSessionConfiguration.defaultSessionConfiguration())
342342
let cond = NSCondition()
@@ -503,9 +503,7 @@ extension NSData {
503503

504504
public func write(to url: URL, atomically: Bool) -> Bool {
505505
if url.isFileURL {
506-
if let path = url.path {
507-
return write(toFile: path, atomically: atomically)
508-
}
506+
return write(toFile: url.path, atomically: atomically)
509507
}
510508
return false
511509
}
@@ -519,12 +517,12 @@ extension NSData {
519517
///
520518
/// This method is invoked in a `try` expression and the caller is responsible for handling any errors in the `catch` clauses of a `do` statement, as described in [Error Handling](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ErrorHandling.html#//apple_ref/doc/uid/TP40014097-CH42) in [The Swift Programming Language](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/index.html#//apple_ref/doc/uid/TP40014097) and [Error Handling](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/AdoptingCocoaDesignPatterns.html#//apple_ref/doc/uid/TP40014216-CH7-ID10) in [Using Swift with Cocoa and Objective-C](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/index.html#//apple_ref/doc/uid/TP40014216).
521519
public func write(to url: URL, options writeOptionsMask: WritingOptions = []) throws {
522-
guard let path = url.path, url.isFileURL == true else {
520+
guard url.isFileURL else {
523521
let userInfo = [NSLocalizedDescriptionKey : "The folder at “\(url)” does not exist or is not a file URL.", // NSLocalizedString() not yet available
524-
NSURLErrorKey : url.absoluteString ?? ""] as Dictionary<String, Any>
522+
NSURLErrorKey : url.absoluteString] as Dictionary<String, Any>
525523
throw NSError(domain: NSCocoaErrorDomain, code: 4, userInfo: userInfo)
526524
}
527-
try write(toFile: path, options: writeOptionsMask)
525+
try write(toFile: url.path, options: writeOptionsMask)
528526
}
529527

530528
public func range(of searchData: Data, options mask: SearchOptions = [], in searchRange: NSRange) -> NSRange {

Foundation/NSFileHandle.swift

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -232,15 +232,12 @@ extension FileHandle {
232232
}
233233

234234
internal static func _openFileDescriptorForURL(_ url : URL, flags: Int32, reading: Bool) throws -> Int32 {
235-
if let path = url.path {
236-
let fd = _CFOpenFile(path, flags)
237-
if fd < 0 {
238-
throw _NSErrorWithErrno(errno, reading: reading, url: url)
239-
}
240-
return fd
241-
} else {
242-
throw _NSErrorWithErrno(ENOENT, reading: reading, url: url)
235+
let path = url.path
236+
let fd = _CFOpenFile(path, flags)
237+
if fd < 0 {
238+
throw _NSErrorWithErrno(errno, reading: reading, url: url)
243239
}
240+
return fd
244241
}
245242

246243
public convenience init(forReadingFrom url: URL) throws {

Foundation/NSFileManager.swift

Lines changed: 15 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,7 @@ open class FileManager: NSObject {
144144
guard url.isFileURL else {
145145
throw NSError(domain: NSCocoaErrorDomain, code: NSCocoaError.FileWriteUnsupportedSchemeError.rawValue, userInfo: [NSURLErrorKey : url])
146146
}
147-
guard let path = url.path else {
148-
throw NSError(domain: NSCocoaErrorDomain, code: NSCocoaError.FileNoSuchFileError.rawValue, userInfo: nil)
149-
}
150-
try self.createDirectory(atPath: path, withIntermediateDirectories: createIntermediates, attributes: attributes)
147+
try self.createDirectory(atPath: url.path, withIntermediateDirectories: createIntermediates, attributes: attributes)
151148
}
152149

153150
/* createSymbolicLinkAtURL:withDestinationURL:error: returns YES if the symbolic link that point at 'destURL' was able to be created at the location specified by 'url'. 'destURL' is always resolved against its base URL, if it has one. If 'destURL' has no base URL and it's 'relativePath' is indeed a relative path, then a relative symlink will be created. If this method returns NO, the link was unable to be created and an NSError will be returned by reference in the 'error' parameter. This method does not traverse a terminal symlink.
@@ -159,13 +156,7 @@ open class FileManager: NSObject {
159156
guard destURL.scheme == nil || destURL.isFileURL else {
160157
throw NSError(domain: NSCocoaErrorDomain, code: NSCocoaError.FileWriteUnsupportedSchemeError.rawValue, userInfo: [NSURLErrorKey : destURL])
161158
}
162-
guard let path = url.path else {
163-
throw NSError(domain: NSCocoaErrorDomain, code: NSCocoaError.FileNoSuchFileError.rawValue, userInfo: [NSURLErrorKey : url])
164-
}
165-
guard let destPath = destURL.path else {
166-
throw NSError(domain: NSCocoaErrorDomain, code: NSCocoaError.FileNoSuchFileError.rawValue, userInfo: [NSURLErrorKey : destURL])
167-
}
168-
try self.createSymbolicLink(atPath: path, withDestinationPath: destPath)
159+
try self.createSymbolicLink(atPath: url.path, withDestinationPath: destURL.path)
169160
}
170161

171162
/* Instances of NSFileManager may now have delegates. Each instance has one delegate, and the delegate is not retained. In versions of Mac OS X prior to 10.5, the behavior of calling [[NSFileManager alloc] init] was undefined. In Mac OS X 10.5 "Leopard" and later, calling [[NSFileManager alloc] init] returns a new instance of an NSFileManager.
@@ -516,13 +507,7 @@ open class FileManager: NSObject {
516507
guard dstURL.isFileURL else {
517508
throw NSError(domain: NSCocoaErrorDomain, code: NSCocoaError.FileWriteUnsupportedSchemeError.rawValue, userInfo: [NSURLErrorKey : dstURL])
518509
}
519-
guard let srcPath = srcURL.path else {
520-
throw NSError(domain: NSCocoaErrorDomain, code: NSCocoaError.FileNoSuchFileError.rawValue, userInfo: [NSURLErrorKey : srcURL])
521-
}
522-
guard let dstPath = dstURL.path else {
523-
throw NSError(domain: NSCocoaErrorDomain, code: NSCocoaError.FileNoSuchFileError.rawValue, userInfo: [NSURLErrorKey : dstURL])
524-
}
525-
try copyItem(atPath: srcPath, toPath: dstPath)
510+
try copyItem(atPath: srcURL.path, toPath: dstURL.path)
526511
}
527512

528513
open func moveItem(at srcURL: URL, to dstURL: URL) throws {
@@ -532,13 +517,7 @@ open class FileManager: NSObject {
532517
guard dstURL.isFileURL else {
533518
throw NSError(domain: NSCocoaErrorDomain, code: NSCocoaError.FileWriteUnsupportedSchemeError.rawValue, userInfo: [NSURLErrorKey : dstURL])
534519
}
535-
guard let srcPath = srcURL.path else {
536-
throw NSError(domain: NSCocoaErrorDomain, code: NSCocoaError.FileNoSuchFileError.rawValue, userInfo: [NSURLErrorKey : srcURL])
537-
}
538-
guard let dstPath = dstURL.path else {
539-
throw NSError(domain: NSCocoaErrorDomain, code: NSCocoaError.FileNoSuchFileError.rawValue, userInfo: [NSURLErrorKey : dstURL])
540-
}
541-
try moveItem(atPath: srcPath, toPath: dstPath)
520+
try moveItem(atPath: srcURL.path, toPath: dstURL.path)
542521
}
543522

544523
open func linkItem(at srcURL: URL, to dstURL: URL) throws {
@@ -548,23 +527,14 @@ open class FileManager: NSObject {
548527
guard dstURL.isFileURL else {
549528
throw NSError(domain: NSCocoaErrorDomain, code: NSCocoaError.FileWriteUnsupportedSchemeError.rawValue, userInfo: [NSURLErrorKey : dstURL])
550529
}
551-
guard let srcPath = srcURL.path else {
552-
throw NSError(domain: NSCocoaErrorDomain, code: NSCocoaError.FileNoSuchFileError.rawValue, userInfo: [NSURLErrorKey : srcURL])
553-
}
554-
guard let dstPath = dstURL.path else {
555-
throw NSError(domain: NSCocoaErrorDomain, code: NSCocoaError.FileNoSuchFileError.rawValue, userInfo: [NSURLErrorKey : dstURL])
556-
}
557-
try linkItem(atPath: srcPath, toPath: dstPath)
530+
try linkItem(atPath: srcURL.path, toPath: dstURL.path)
558531
}
559532

560533
open func removeItem(at url: URL) throws {
561534
guard url.isFileURL else {
562535
throw NSError(domain: NSCocoaErrorDomain, code: NSCocoaError.FileWriteUnsupportedSchemeError.rawValue, userInfo: [NSURLErrorKey : url])
563536
}
564-
guard let path = url.path else {
565-
throw NSError(domain: NSCocoaErrorDomain, code: NSCocoaError.FileNoSuchFileError.rawValue, userInfo: [NSURLErrorKey : url])
566-
}
567-
try self.removeItem(atPath: path)
537+
try self.removeItem(atPath: url.path)
568538
}
569539

570540
/* Process working directory management. Despite the fact that these are instance methods on NSFileManager, these methods report and change (respectively) the working directory for the entire process. Developers are cautioned that doing so is fraught with peril.
@@ -890,7 +860,7 @@ extension FileManager {
890860
guard let url = o as? URL else {
891861
return nil
892862
}
893-
let path = url.path!.replacingOccurrences(of: baseURL.path!+"/", with: "")
863+
let path = url.path.replacingOccurrences(of: baseURL.path+"/", with: "")
894864
return path
895865
}
896866

@@ -910,22 +880,17 @@ extension FileManager {
910880
_options = options
911881
_errorHandler = errorHandler
912882

913-
if let path = _url.path {
914-
if FileManager.default.fileExists(atPath: path) {
915-
let fsRep = FileManager.default.fileSystemRepresentation(withPath: path)
916-
let ps = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>.allocate(capacity: 2)
917-
ps.initialize(to: UnsafeMutablePointer(mutating: fsRep))
918-
ps.advanced(by: 1).initialize(to: nil)
919-
_stream = fts_open(ps, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR, nil)
920-
ps.deinitialize(count: 2)
921-
ps.deallocate(capacity: 2)
922-
} else {
923-
_rootError = _NSErrorWithErrno(ENOENT, reading: true, url: url)
924-
}
883+
if FileManager.default.fileExists(atPath: _url.path) {
884+
let fsRep = FileManager.default.fileSystemRepresentation(withPath: _url.path)
885+
let ps = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>.allocate(capacity: 2)
886+
ps.initialize(to: UnsafeMutablePointer(mutating: fsRep))
887+
ps.advanced(by: 1).initialize(to: nil)
888+
_stream = fts_open(ps, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR, nil)
889+
ps.deinitialize(count: 2)
890+
ps.deallocate(capacity: 2)
925891
} else {
926892
_rootError = _NSErrorWithErrno(ENOENT, reading: true, url: url)
927893
}
928-
929894
}
930895

931896
deinit {

Foundation/NSPathUtilities.swift

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -377,26 +377,13 @@ public extension NSString {
377377
let url = URL(fileURLWithPath: path)
378378

379379
let searchAllFilesInDirectory = _stringIsPathToDirectory(path)
380-
let namePrefix = searchAllFilesInDirectory ? nil : url.lastPathComponent
381-
let checkFileName = _getFileNamePredicate(namePrefix, caseSensetive: flag)
382-
let checkExtension = _getExtensionPredicate(filterTypes, caseSensetive: flag)
383-
384-
let resolvedURL: URL
385-
let urlWhereToSearch: URL
386-
if let url = url._resolveSymlinksInPath(excludeSystemDirs: false) {
387-
resolvedURL = url
388-
} else {
389-
return 0
390-
}
391-
380+
let namePrefix = searchAllFilesInDirectory ? "" : url.lastPathComponent
381+
let checkFileName = _getFileNamePredicate(namePrefix, caseSensitive: flag)
382+
let checkExtension = _getExtensionPredicate(filterTypes, caseSensitive: flag)
392383

393-
do {
394-
urlWhereToSearch = searchAllFilesInDirectory ? resolvedURL : try resolvedURL.deletingLastPathComponent()
395-
} catch {
396-
return 0
397-
}
384+
let resolvedURL: URL = url.resolvingSymlinksInPath()
385+
let urlWhereToSearch: URL = searchAllFilesInDirectory ? resolvedURL : resolvedURL.deletingLastPathComponent()
398386

399-
400387
var matches = _getNamesAtURL(urlWhereToSearch, prependWith: "", namePredicate: checkFileName, typePredicate: checkExtension)
401388

402389
if matches.count == 1 {
@@ -445,9 +432,9 @@ public extension NSString {
445432

446433
if matchByName && matchByExtension {
447434
if prependWith.isEmpty {
448-
result.append(itemName!)
435+
result.append(itemName)
449436
} else {
450-
result.append(prependWith._bridgeToObjectiveC().appendingPathComponent(itemName!))
437+
result.append(prependWith._bridgeToObjectiveC().appendingPathComponent(itemName))
451438
}
452439
}
453440
}
@@ -456,12 +443,12 @@ public extension NSString {
456443
return result
457444
}
458445

459-
internal func _getExtensionPredicate(_ extensions: [String]?, caseSensetive: Bool) -> _FileNamePredicate {
446+
fileprivate func _getExtensionPredicate(_ extensions: [String]?, caseSensitive: Bool) -> _FileNamePredicate {
460447
guard let exts = extensions else {
461448
return { _ in true }
462449
}
463450

464-
if caseSensetive {
451+
if caseSensitive {
465452
let set = Set(exts)
466453
return { $0 != nil && set.contains($0!) }
467454
} else {
@@ -470,15 +457,15 @@ public extension NSString {
470457
}
471458
}
472459

473-
internal func _getFileNamePredicate(_ prefix: String?, caseSensetive: Bool) -> _FileNamePredicate {
474-
guard let thePrefix = prefix else {
460+
fileprivate func _getFileNamePredicate(_ prefix: String, caseSensitive: Bool) -> _FileNamePredicate {
461+
guard !prefix.isEmpty else {
475462
return { _ in true }
476463
}
477464

478-
if caseSensetive {
479-
return { $0 != nil && $0!.hasPrefix(thePrefix) }
465+
if caseSensitive {
466+
return { $0 != nil && $0!.hasPrefix(prefix) }
480467
} else {
481-
return { $0 != nil && $0!._bridgeToObjectiveC().range(of: thePrefix, options: .caseInsensitive).location == 0 }
468+
return { $0 != nil && $0!._bridgeToObjectiveC().range(of: prefix, options: .caseInsensitive).location == 0 }
482469
}
483470
}
484471

Foundation/NSStringAPI.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ extension String {
146146
/// by concatenating them with a path separator between each pair.
147147
@available(*, unavailable, message: "Use fileURL(withPathComponents:) on URL instead.")
148148
public static func path(withComponents components: [String]) -> String {
149-
return (NSURL.fileURLWithPathComponents(components)?.path)!
149+
return (NSURL.fileURL(withPathComponents: components)?.path)!
150150
}
151151

152152
//===--------------------------------------------------------------------===//

0 commit comments

Comments
 (0)