Skip to content

Commit 83073e2

Browse files
authored
(133878310) URL.fileSystemPath should drop all trailing slashes (#852)
1 parent ebb00b8 commit 83073e2

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
if let user {
@@ -523,8 +523,10 @@ extension String {
523523
#else
524524
return "/tmp/"
525525
#endif
526-
#endif
526+
#endif // os(Windows)
527527
}
528+
#endif // !NO_FILESYSTEM
529+
528530
/// Replaces any number of sequential `/`
529531
/// characters with /
530532
/// NOTE: Internal so it's testable
@@ -563,7 +565,7 @@ extension String {
563565
}
564566
}
565567

566-
private var _droppingTrailingSlashes: String {
568+
internal var _droppingTrailingSlashes: String {
567569
guard !self.isEmpty else {
568570
return self
569571
}
@@ -573,7 +575,9 @@ extension String {
573575
}
574576
return String(self[...lastNonSlash])
575577
}
576-
578+
579+
#if !NO_FILESYSTEM
580+
577581
static var NETWORK_PREFIX: String { #"\\"# }
578582

579583
private var _standardizingPath: String {
@@ -610,7 +614,8 @@ extension String {
610614
var standardizingPath: String {
611615
expandingTildeInPath._standardizingPath
612616
}
613-
#endif // !NO_FILESYSTEM
617+
618+
#endif // !NO_FILESYSTEM
614619

615620
// _NSPathComponents
616621
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)