Skip to content

Commit 879e7f6

Browse files
authored
Merge pull request #1376 from dduan/fix-deletingPathExtension-inconsistency
2 parents c5c6cc8 + 32d542a commit 879e7f6

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

Foundation/NSPathUtilities.swift

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,30 @@ internal extension String {
5555

5656
internal var _startOfPathExtension : String.Index? {
5757
precondition(!hasSuffix("/"))
58-
59-
var curPos = endIndex
60-
let lastCompStartPos = _startOfLastPathComponent
61-
58+
59+
var currentPosition = endIndex
60+
let startOfLastPathComponent = _startOfLastPathComponent
61+
6262
// Find the beginning of the extension
63-
while curPos > lastCompStartPos {
64-
let prevPos = index(before: curPos)
65-
let char = self[prevPos]
66-
if char == "/" {
63+
while currentPosition > startOfLastPathComponent {
64+
let previousPosition = index(before: currentPosition)
65+
let character = self[previousPosition]
66+
if character == "/" {
6767
return nil
68-
} else if char == "." {
69-
if lastCompStartPos == prevPos {
68+
} else if character == "." {
69+
if startOfLastPathComponent == previousPosition {
70+
return nil
71+
} else if case let previous2Position = index(before: previousPosition),
72+
previousPosition == index(before: endIndex) &&
73+
previous2Position == startOfLastPathComponent &&
74+
self[previous2Position] == "."
75+
{
7076
return nil
7177
} else {
72-
return curPos
78+
return currentPosition
7379
}
7480
}
75-
curPos = prevPos
81+
currentPosition = previousPosition
7682
}
7783
return nil
7884
}
@@ -262,7 +268,7 @@ public extension NSString {
262268
if fixedSelf.length <= 1 {
263269
return fixedSelf
264270
}
265-
if let extensionPos = (fixedSelf._startOfPathExtension) {
271+
if let extensionPos = fixedSelf._startOfPathExtension {
266272
return String(fixedSelf.prefix(upTo: fixedSelf.index(before: extensionPos)))
267273
} else {
268274
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)