Skip to content

Commit 43bf20a

Browse files
committed
implementation for NSURL.standardizedURL and NSURL.filePathURL properties
1 parent 57ffb9d commit 43bf20a

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

Foundation/NSURL.swift

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,40 @@ private func _standardizedPath(_ path: String) -> String {
2828
return path
2929
}
3030

31+
/* internal function to remove .. and . from path */
32+
internal func _standardizedURLPath(_ path: String) -> String {
33+
var pComps = path.pathComponents
34+
35+
if pComps.last == "/" {
36+
pComps.removeLast()
37+
}
38+
39+
guard !pComps.isEmpty else {
40+
return path
41+
}
42+
43+
let isAbsolutePath = pComps.first == "/"
44+
var result : String = pComps.removeFirst()
45+
for pComp in pComps {
46+
switch pComp {
47+
case ".":
48+
break
49+
50+
case ".." where isAbsolutePath:
51+
result = result.bridge().stringByDeletingLastPathComponent
52+
53+
default:
54+
result = result.bridge().stringByAppendingPathComponent(pComp)
55+
}
56+
}
57+
58+
if(path.hasSuffix("/")) {
59+
result += "/"
60+
}
61+
62+
return result
63+
}
64+
3165
public class NSURL : NSObject, NSSecureCoding, NSCopying {
3266
typealias CFType = CFURL
3367
internal var _base = _CFInfo(typeID: CFURLGetTypeID())
@@ -382,7 +416,9 @@ public class NSURL : NSObject, NSSecureCoding, NSCopying {
382416

383417
/* A string constant for the "file" URL scheme. If you are using this to compare to a URL's scheme to see if it is a file URL, you should instead use the NSURL fileURL property -- the fileURL property is much faster. */
384418
public var standardizedURL: NSURL? {
385-
NSUnimplemented()
419+
let urlComponent = NSURLComponents(string: relativeString)
420+
urlComponent!.path = _standardizedURLPath(urlComponent!.path!)
421+
return urlComponent?.URLRelativeToURL(baseURL)
386422
}
387423

388424
/* Returns whether the URL's resource exists and is reachable. This method synchronously checks if the resource's backing store is reachable. Checking reachability is appropriate when making decisions that do not require other immediate operations on the resource, e.g. periodic maintenance of UI state that depends on the existence of a specific document. When performing operations such as opening a file or copying resource properties, it is more efficient to simply try the operation and handle failures. If this method returns NO, the optional error is populated. This method is currently applicable only to URLs for file system resources. For other URL types, NO is returned. Symbol is present in iOS 4, but performs no operation.
@@ -396,7 +432,10 @@ public class NSURL : NSObject, NSSecureCoding, NSCopying {
396432
/* Returns a file path URL that refers to the same resource as a specified URL. File path URLs use a file system style path. An error will occur if the url parameter is not a file URL. A file reference URL's resource must exist and be reachable to be converted to a file path URL. Symbol is present in iOS 4, but performs no operation.
397433
*/
398434
public var filePathURL: NSURL? {
399-
NSUnimplemented()
435+
guard fileURL else {
436+
return nil
437+
}
438+
return NSURL(string: absoluteString)
400439
}
401440

402441
override public var _cfTypeID: CFTypeID {

TestFoundation/TestNSURL.swift

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,7 @@ class TestNSURL : XCTestCase {
145145
result["parameterString"] = url.parameterString ?? kNullString
146146
result["relativePath"] = url.relativePath ?? kNullString
147147
result["isFileURL"] = url.fileURL ? "YES" : "NO"
148-
// Not yet implemented
149-
// result["standardizedURL"] = url.standardizedURL?.relativeString ?? kNullString
150-
148+
result["standardizedURL"] = url.standardizedURL?.relativeString ?? kNullString
151149
// Temporarily disabled because we're only checking string results
152150
// result["pathComponents"] = url.pathComponents ?? kNullString
153151
result["lastPathComponent"] = url.lastPathComponent ?? kNullString
@@ -207,14 +205,11 @@ class TestNSURL : XCTestCase {
207205
}
208206
if let url = url {
209207

210-
// TODO: NSURL.standardizedURL isn't implemented yet.
211-
var modifiedExpectedNSResult = expectedNSResult as! [String: Any]
212-
modifiedExpectedNSResult["standardizedURL"] = nil
213208
if title == "NSURLWithString-parse-ambiguous-url-001" {
214209
// TODO: Fix this test
215210
} else {
216211
let results = generateResults(url, pathComponent: inPathComponent, pathExtension: inPathExtension)
217-
let (isEqual, differences) = compareResults(url, expected: modifiedExpectedNSResult, got: results)
212+
let (isEqual, differences) = compareResults(url, expected: expectedNSResult as! [String: Any], got: results)
218213
XCTAssertTrue(isEqual, "\(title): \(differences)")
219214
}
220215
} else {

0 commit comments

Comments
 (0)