Skip to content

Commit c171fed

Browse files
committed
Fix String.deletingPathExtension Inconsistency
The following code behaves differently on macOS and Linux: ```swift NSString(string: "..").deletingPathExtension ``` On Linux, the resulting value is `.` while on macOS, it's `..`. I think the latter make far more sense since `..` mean parent directly, treating the last dot as "extension" is a bit of a stretch. Plus, this logic existed on macOS longer (therefore has more existing users). So it make sense to align Linux Foundation's behavior to that of macOS.
1 parent c5c6cc8 commit c171fed

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

Foundation/NSPathUtilities.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ internal extension String {
6868
} else if char == "." {
6969
if lastCompStartPos == prevPos {
7070
return nil
71+
} else if case let prevPrevPos = index(before: prevPos), prevPos == index(before: endIndex) && prevPrevPos == lastCompStartPos && self[prevPrevPos] == "." {
72+
return nil
7173
} else {
7274
return curPos
7375
}
@@ -262,7 +264,7 @@ public extension NSString {
262264
if fixedSelf.length <= 1 {
263265
return fixedSelf
264266
}
265-
if let extensionPos = (fixedSelf._startOfPathExtension) {
267+
if let extensionPos = fixedSelf._startOfPathExtension {
266268
return String(fixedSelf.prefix(upTo: fixedSelf.index(before: extensionPos)))
267269
} else {
268270
return fixedSelf

TestFoundation/TestNSString.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,7 @@ class TestNSString : XCTestCase {
10781078
NSString(string: "scratch..tiff") : "scratch.",
10791079
NSString(string: ".tiff") : ".tiff",
10801080
NSString(string: "/") : "/",
1081+
NSString(string: "..") : "..",
10811082
]
10821083
for (fileName, expectedResult) in values {
10831084
let result = fileName.deletingPathExtension

0 commit comments

Comments
 (0)