Skip to content

Commit 3656a59

Browse files
committed
(133878310) URL.fileSystemPath should drop all trailing slashes
1 parent 0ffd606 commit 3656a59

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

Sources/FoundationEssentials/String/String+Path.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ extension String {
569569
}
570570
}
571571

572-
private var _droppingTrailingSlashes: String {
572+
internal var _droppingTrailingSlashes: String {
573573
guard !self.isEmpty else {
574574
return self
575575
}

Sources/FoundationEssentials/URL/URL.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,12 +1320,8 @@ public struct URL: Equatable, Sendable, Hashable {
13201320
}
13211321

13221322
private static func fileSystemPath(for urlPath: String) -> String {
1323-
var result = urlPath
1324-
if result.count > 1 && result.utf8.last == UInt8(ascii: "/") {
1325-
_ = result.popLast()
1326-
}
13271323
let charsToLeaveEncoded: Set<UInt8> = [._slash, 0]
1328-
return Parser.percentDecode(result, excluding: charsToLeaveEncoded) ?? ""
1324+
return Parser.percentDecode(urlPath._droppingTrailingSlashes, excluding: charsToLeaveEncoded) ?? ""
13291325
}
13301326

13311327
var fileSystemPath: String {

Tests/FoundationEssentialsTests/URLTests.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,21 @@ final class URLTests : XCTestCase {
571571
XCTAssertEqual(appended.relativePath, "relative/with:slash")
572572
}
573573

574+
func testURLFilePathDropsTrailingSlashes() throws {
575+
var url = URL(filePath: "/path/slashes///")
576+
XCTAssertEqual(url.path(), "/path/slashes///")
577+
// TODO: Update this once .fileSystemPath uses backslashes for Windows
578+
XCTAssertEqual(url.fileSystemPath, "/path/slashes")
579+
580+
url = URL(filePath: "/path/slashes/")
581+
XCTAssertEqual(url.path(), "/path/slashes/")
582+
XCTAssertEqual(url.fileSystemPath, "/path/slashes")
583+
584+
url = URL(filePath: "/path/slashes")
585+
XCTAssertEqual(url.path(), "/path/slashes")
586+
XCTAssertEqual(url.fileSystemPath, "/path/slashes")
587+
}
588+
574589
func testURLComponentsPercentEncodedUnencodedProperties() throws {
575590
var comp = URLComponents()
576591

0 commit comments

Comments
 (0)