Skip to content

Commit a850fea

Browse files
mbvreddyPushkar N Kulkarni
authored andcommitted
implementation for NSURL.standardizedURL and NSURL.filePathURL properties
change _standardizedURLPath to private function
1 parent e74f745 commit a850fea

File tree

2 files changed

+52
-13
lines changed

2 files changed

+52
-13
lines changed

Foundation/NSURL.swift

Lines changed: 49 additions & 5 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+
/* private function to remove .. and . from path */
32+
private 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
internal func _pathComponents(_ path: String?) -> [String]? {
3266
if let p = path {
3367
var result = [String]()
@@ -160,7 +194,7 @@ extension URLFileResourceType {
160194
public static let unknown = URLFileResourceType(rawValue: "NSURLFileResourceTypeUnknown")
161195
}
162196

163-
public class NSURL: NSObject, NSSecureCoding, NSCopying {
197+
public class NSURL : NSObject, NSSecureCoding, NSCopying {
164198
typealias CFType = CFURL
165199
internal var _base = _CFInfo(typeID: CFURLGetTypeID())
166200
internal var _flags : UInt32 = 0
@@ -520,8 +554,15 @@ public class NSURL: NSObject, NSSecureCoding, NSCopying {
520554
}
521555

522556
/* 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. */
523-
public var standardized: URL? {
524-
NSUnimplemented()
557+
public var standardizedURL: NSURL? {
558+
let urlComponent = NSURLComponents(string: relativeString)
559+
560+
guard urlComponent != nil && urlComponent!.path != nil else {
561+
return nil
562+
}
563+
564+
urlComponent!.path = _standardizedURLPath(urlComponent!.path!)
565+
return urlComponent?.URLRelativeToURL(baseURL)
525566
}
526567

527568
/* 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.
@@ -534,8 +575,11 @@ public class NSURL: NSObject, NSSecureCoding, NSCopying {
534575

535576
/* 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.
536577
*/
537-
public var filePathURL: URL? {
538-
NSUnimplemented()
578+
public var filePathURL: NSURL? {
579+
guard fileURL else {
580+
return nil
581+
}
582+
return NSURL(string: absoluteString)
539583
}
540584

541585
override public var _cfTypeID: CFTypeID {

TestFoundation/TestNSURL.swift

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,8 @@ class TestNSURL : XCTestCase {
148148
result["fragment"] = url.fragment ?? kNullString
149149
result["parameterString"] = url.parameterString ?? kNullString
150150
result["relativePath"] = url.relativePath ?? kNullString
151-
result["isFileURL"] = url.isFileURL ? "YES" : "NO"
152-
// Not yet implemented
153-
// result["standardizedURL"] = url.standardizedURL?.relativeString ?? kNullString
154-
151+
result["isFileURL"] = url.fileURL ? "YES" : "NO"
152+
result["standardizedURL"] = url.standardizedURL?.relativeString ?? kNullString
155153
// Temporarily disabled because we're only checking string results
156154
// result["pathComponents"] = url.pathComponents ?? kNullString
157155
result["lastPathComponent"] = url.lastPathComponent ?? kNullString
@@ -222,14 +220,11 @@ class TestNSURL : XCTestCase {
222220
}
223221
if let url = url {
224222

225-
// TODO: NSURL.standardizedURL isn't implemented yet.
226-
var modifiedExpectedNSResult = expectedNSResult as! [String: Any]
227-
modifiedExpectedNSResult["standardizedURL"] = nil
228223
if title == "NSURLWithString-parse-ambiguous-url-001" {
229224
// TODO: Fix this test
230225
} else {
231226
let results = generateResults(url, pathComponent: inPathComponent, pathExtension: inPathExtension)
232-
let (isEqual, differences) = compareResults(url, expected: modifiedExpectedNSResult, got: results)
227+
let (isEqual, differences) = compareResults(url, expected: expectedNSResult as! [String: Any], got: results)
233228
XCTAssertTrue(isEqual, "\(title): \(differences)")
234229
}
235230
} else {

0 commit comments

Comments
 (0)