Skip to content

Commit 3534b3b

Browse files
committed
SR-5582 Changed NSString.appendingPathComponent implementation on Linux to match Darwin's.
Fixed following issues where path.appendingPathComponent(component) would produce incorrect result: path was empty and component did not start with / or path ended with / and component started with /
1 parent 2ec3015 commit 3534b3b

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

Foundation/NSPathUtilities.swift

100644100755
Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@ internal extension String {
8282
}
8383

8484
internal func _stringByAppendingPathComponent(_ str: String, doneAppending : Bool = true) -> String {
85-
if str.length == 0 {
85+
if str.isEmpty {
8686
return self
8787
}
88-
if self == "" {
89-
return "/" + str
88+
if isEmpty {
89+
return str
9090
}
91-
if self == "/" {
91+
if hasSuffix("/") {
9292
return self + str
9393
}
9494
return self + "/" + str
@@ -237,16 +237,7 @@ public extension NSString {
237237
}
238238

239239
internal func _stringByAppendingPathComponent(_ str: String, doneAppending : Bool = true) -> String {
240-
if str.length == 0 {
241-
return _swiftObject
242-
}
243-
if self == "" {
244-
return "/" + str
245-
}
246-
if self == "/" {
247-
return _swiftObject + str
248-
}
249-
return _swiftObject + "/" + str
240+
return _swiftObject._stringByAppendingPathComponent(str, doneAppending: doneAppending)
250241
}
251242

252243
public func appendingPathComponent(_ str: String) -> String {

TestFoundation/TestNSString.swift

100644100755
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class TestNSString : XCTestCase {
7777
("test_initializeWithFormat", test_initializeWithFormat),
7878
("test_initializeWithFormat2", test_initializeWithFormat2),
7979
("test_initializeWithFormat3", test_initializeWithFormat3),
80+
("test_appendingPathComponent", test_appendingPathComponent),
8081
("test_deletingLastPathComponent", test_deletingLastPathComponent),
8182
("test_getCString_simple", test_getCString_simple),
8283
("test_getCString_nonASCII_withASCIIAccessor", test_getCString_nonASCII_withASCIIAccessor),
@@ -752,6 +753,32 @@ class TestNSString : XCTestCase {
752753
XCTAssertEqual(string, "NSDictionary value is 1000 (42&0)")
753754
}
754755
}
756+
757+
func test_appendingPathComponent() {
758+
do {
759+
let path: NSString = "/tmp"
760+
let result = path.appendingPathComponent("scratch.tiff")
761+
XCTAssertEqual(result, "/tmp/scratch.tiff")
762+
}
763+
764+
do {
765+
let path: NSString = "/tmp/"
766+
let result = path.appendingPathComponent("scratch.tiff")
767+
XCTAssertEqual(result, "/tmp/scratch.tiff")
768+
}
769+
770+
do {
771+
let path: NSString = "/"
772+
let result = path.appendingPathComponent("scratch.tiff")
773+
XCTAssertEqual(result, "/scratch.tiff")
774+
}
775+
776+
do {
777+
let path: NSString = ""
778+
let result = path.appendingPathComponent("scratch.tiff")
779+
XCTAssertEqual(result, "scratch.tiff")
780+
}
781+
}
755782

756783
func test_deletingLastPathComponent() {
757784
do {

0 commit comments

Comments
 (0)