Skip to content

Commit 566ffd7

Browse files
committed
Merge pull request #295 from seabaylea/sr-999
[SR-999] Add dot to NSString.stringByAppendingPathExtension() result
2 parents 186328c + 9087838 commit 566ffd7

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

Foundation/NSPathUtilities.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,8 @@ public extension NSString {
305305
if fixedSelf.length <= 1 {
306306
return fixedSelf
307307
}
308-
309-
if let extensionPos = fixedSelf._startOfPathExtension {
310-
return String(fixedSelf.characters.prefix(upTo: extensionPos))
308+
if let extensionPos = (fixedSelf._startOfPathExtension) {
309+
return String(fixedSelf.characters.prefix(upTo: extensionPos.predecessor()))
311310
} else {
312311
return fixedSelf
313312
}
@@ -318,7 +317,7 @@ public extension NSString {
318317
print("Cannot append extension \(str) to path \(self)")
319318
return nil
320319
}
321-
let result = _swiftObject + str._stringByFixingSlashes(compress: false, stripTrailing: true)
320+
let result = _swiftObject._stringByFixingSlashes(compress: false, stripTrailing: true) + "." + str
322321
return result._stringByFixingSlashes()
323322
}
324323

TestFoundation/TestNSString.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ class TestNSString : XCTestCase {
8181
("test_stringByExpandingTildeInPath", test_stringByExpandingTildeInPath),
8282
("test_stringByStandardizingPath", test_stringByStandardizingPath),
8383
("test_stringByRemovingPercentEncoding", test_stringByRemovingPercentEncoding),
84+
("test_stringByAppendingPathExtension", test_stringByAppendingPathExtension),
85+
("test_stringByDeletingPathExtension", test_stringByDeletingPathExtension),
8486
("test_ExternalRepresentation", test_ExternalRepresentation),
8587
("test_mutableStringConstructor", test_mutableStringConstructor),
8688
("test_PrefixSuffix", test_PrefixSuffix),
@@ -836,6 +838,36 @@ class TestNSString : XCTestCase {
836838
XCTAssertNil(s2, "returns nil for a string with an invalid percent encoding")
837839
}
838840

841+
func test_stringByAppendingPathExtension() {
842+
let values : Dictionary = [
843+
NSString(string: "/tmp/scratch.old") : "/tmp/scratch.old.tiff",
844+
NSString(string: "/tmp/scratch.") : "/tmp/scratch..tiff",
845+
NSString(string: "/tmp/") : "/tmp.tiff",
846+
NSString(string: "/scratch") : "/scratch.tiff",
847+
NSString(string: "/~scratch") : "/~scratch.tiff",
848+
NSString(string: "scratch") : "scratch.tiff",
849+
]
850+
for (fileName, expectedResult) in values {
851+
let result = fileName.stringByAppendingPathExtension("tiff")
852+
XCTAssertEqual(result, expectedResult, "expected \(expectedResult) for \(fileName) but got \(result)")
853+
}
854+
}
855+
856+
func test_stringByDeletingPathExtension() {
857+
let values : Dictionary = [
858+
NSString(string: "/tmp/scratch.tiff") : "/tmp/scratch",
859+
NSString(string: "/tmp/") : "/tmp",
860+
NSString(string: "scratch.bundle") : "scratch",
861+
NSString(string: "scratch..tiff") : "scratch.",
862+
NSString(string: ".tiff") : ".tiff",
863+
NSString(string: "/") : "/",
864+
]
865+
for (fileName, expectedResult) in values {
866+
let result = fileName.stringByDeletingPathExtension
867+
XCTAssertEqual(result, expectedResult, "expected \(expectedResult) for \(fileName) but got \(result)")
868+
}
869+
}
870+
839871
func test_ExternalRepresentation() {
840872
// Ensure NSString can be used to create an external data representation
841873

0 commit comments

Comments
 (0)