Skip to content

Commit

Permalink
Fix String.deletingPathExtension Inconsistency
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dduan committed Dec 20, 2017
1 parent c5c6cc8 commit c171fed
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Foundation/NSPathUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ internal extension String {
} else if char == "." {
if lastCompStartPos == prevPos {
return nil
} else if case let prevPrevPos = index(before: prevPos), prevPos == index(before: endIndex) && prevPrevPos == lastCompStartPos && self[prevPrevPos] == "." {
return nil
} else {
return curPos
}
Expand Down Expand Up @@ -262,7 +264,7 @@ public extension NSString {
if fixedSelf.length <= 1 {
return fixedSelf
}
if let extensionPos = (fixedSelf._startOfPathExtension) {
if let extensionPos = fixedSelf._startOfPathExtension {
return String(fixedSelf.prefix(upTo: fixedSelf.index(before: extensionPos)))
} else {
return fixedSelf
Expand Down
1 change: 1 addition & 0 deletions TestFoundation/TestNSString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,7 @@ class TestNSString : XCTestCase {
NSString(string: "scratch..tiff") : "scratch.",
NSString(string: ".tiff") : ".tiff",
NSString(string: "/") : "/",
NSString(string: "..") : "..",
]
for (fileName, expectedResult) in values {
let result = fileName.deletingPathExtension
Expand Down

0 comments on commit c171fed

Please sign in to comment.