Skip to content

Commit 4e5d318

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

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

Sources/FoundationEssentials/String/String+Path.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ extension String {
366366
return String(cString: output)
367367
}
368368

369-
#if !NO_FILESYSTEM
369+
#if !NO_FILESYSTEM
370370
internal static func homeDirectoryPath(forUser user: String? = nil) -> String {
371371
#if os(Windows)
372372
func GetUserProfile() -> String? {
@@ -529,8 +529,10 @@ extension String {
529529
#else
530530
return "/tmp/"
531531
#endif
532-
#endif
532+
#endif // os(Windows)
533533
}
534+
#endif // !NO_FILESYSTEM
535+
534536
/// Replaces any number of sequential `/`
535537
/// characters with /
536538
/// NOTE: Internal so it's testable
@@ -569,7 +571,7 @@ extension String {
569571
}
570572
}
571573

572-
private var _droppingTrailingSlashes: String {
574+
internal var _droppingTrailingSlashes: String {
573575
guard !self.isEmpty else {
574576
return self
575577
}
@@ -579,7 +581,9 @@ extension String {
579581
}
580582
return String(self[...lastNonSlash])
581583
}
582-
584+
585+
#if !NO_FILESYSTEM
586+
583587
static var NETWORK_PREFIX: String { #"\\"# }
584588

585589
private var _standardizingPath: String {
@@ -616,7 +620,8 @@ extension String {
616620
var standardizingPath: String {
617621
expandingTildeInPath._standardizingPath
618622
}
619-
#endif // !NO_FILESYSTEM
623+
624+
#endif // !NO_FILESYSTEM
620625

621626
// _NSPathComponents
622627
var pathComponents: [String] {

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)