Skip to content

[SR-999] Add dot to NSString.stringByAppendingPathExtension() result #295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions Foundation/NSPathUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,8 @@ public extension NSString {
if fixedSelf.length <= 1 {
return fixedSelf
}

if let extensionPos = fixedSelf._startOfPathExtension {
return String(fixedSelf.characters.prefix(upTo: extensionPos))
if let extensionPos = (fixedSelf._startOfPathExtension) {
return String(fixedSelf.characters.prefix(upTo: extensionPos.predecessor()))
} else {
return fixedSelf
}
Expand All @@ -318,7 +317,7 @@ public extension NSString {
print("Cannot append extension \(str) to path \(self)")
return nil
}
let result = _swiftObject + str._stringByFixingSlashes(compress: false, stripTrailing: true)
let result = _swiftObject._stringByFixingSlashes(compress: false, stripTrailing: true) + "." + str
return result._stringByFixingSlashes()
}

Expand Down
32 changes: 32 additions & 0 deletions TestFoundation/TestNSString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ class TestNSString : XCTestCase {
("test_stringByExpandingTildeInPath", test_stringByExpandingTildeInPath),
("test_stringByStandardizingPath", test_stringByStandardizingPath),
("test_stringByRemovingPercentEncoding", test_stringByRemovingPercentEncoding),
("test_stringByAppendingPathExtension", test_stringByAppendingPathExtension),
("test_stringByDeletingPathExtension", test_stringByDeletingPathExtension),
("test_ExternalRepresentation", test_ExternalRepresentation),
("test_mutableStringConstructor", test_mutableStringConstructor),
("test_PrefixSuffix", test_PrefixSuffix),
Expand Down Expand Up @@ -836,6 +838,36 @@ class TestNSString : XCTestCase {
XCTAssertNil(s2, "returns nil for a string with an invalid percent encoding")
}

func test_stringByAppendingPathExtension() {
let values : Dictionary = [
NSString(string: "/tmp/scratch.old") : "/tmp/scratch.old.tiff",
NSString(string: "/tmp/scratch.") : "/tmp/scratch..tiff",
NSString(string: "/tmp/") : "/tmp.tiff",
NSString(string: "/scratch") : "/scratch.tiff",
NSString(string: "/~scratch") : "/~scratch.tiff",
NSString(string: "scratch") : "scratch.tiff",
]
for (fileName, expectedResult) in values {
let result = fileName.stringByAppendingPathExtension("tiff")
XCTAssertEqual(result, expectedResult, "expected \(expectedResult) for \(fileName) but got \(result)")
}
}

func test_stringByDeletingPathExtension() {
let values : Dictionary = [
NSString(string: "/tmp/scratch.tiff") : "/tmp/scratch",
NSString(string: "/tmp/") : "/tmp",
NSString(string: "scratch.bundle") : "scratch",
NSString(string: "scratch..tiff") : "scratch.",
NSString(string: ".tiff") : ".tiff",
NSString(string: "/") : "/",
]
for (fileName, expectedResult) in values {
let result = fileName.stringByDeletingPathExtension
XCTAssertEqual(result, expectedResult, "expected \(expectedResult) for \(fileName) but got \(result)")
}
}

func test_ExternalRepresentation() {
// Ensure NSString can be used to create an external data representation

Expand Down