Skip to content

Fix String.deletingPathExtension Inconsistency #1376

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
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
32 changes: 19 additions & 13 deletions Foundation/NSPathUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,30 @@ internal extension String {

internal var _startOfPathExtension : String.Index? {
precondition(!hasSuffix("/"))
var curPos = endIndex
let lastCompStartPos = _startOfLastPathComponent

var currentPosition = endIndex
let startOfLastPathComponent = _startOfLastPathComponent

// Find the beginning of the extension
while curPos > lastCompStartPos {
let prevPos = index(before: curPos)
let char = self[prevPos]
if char == "/" {
while currentPosition > startOfLastPathComponent {
let previousPosition = index(before: currentPosition)
let character = self[previousPosition]
if character == "/" {
return nil
} else if char == "." {
if lastCompStartPos == prevPos {
} else if character == "." {
if startOfLastPathComponent == previousPosition {
return nil
} else if case let previous2Position = index(before: previousPosition),
previousPosition == index(before: endIndex) &&
previous2Position == startOfLastPathComponent &&
self[previous2Position] == "."
{
return nil
} else {
return curPos
return currentPosition
}
}
curPos = prevPos
currentPosition = previousPosition
}
return nil
}
Expand Down Expand Up @@ -262,7 +268,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