Skip to content

URL.fileSystemPath should drop all trailing slashes #852

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 1 commit into from
Aug 16, 2024
Merged
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
15 changes: 10 additions & 5 deletions Sources/FoundationEssentials/String/String+Path.swift
Original file line number Diff line number Diff line change
@@ -366,7 +366,7 @@ extension String {
return String(cString: output)
}

#if !NO_FILESYSTEM
#if !NO_FILESYSTEM
internal static func homeDirectoryPath(forUser user: String? = nil) -> String {
#if os(Windows)
func GetUserProfile() -> String? {
@@ -529,8 +529,10 @@ extension String {
#else
return "/tmp/"
#endif
#endif
#endif // os(Windows)
}
#endif // !NO_FILESYSTEM

/// Replaces any number of sequential `/`
/// characters with /
/// NOTE: Internal so it's testable
@@ -569,7 +571,7 @@ extension String {
}
}

private var _droppingTrailingSlashes: String {
internal var _droppingTrailingSlashes: String {
guard !self.isEmpty else {
return self
}
@@ -579,7 +581,9 @@ extension String {
}
return String(self[...lastNonSlash])
}


#if !NO_FILESYSTEM

static var NETWORK_PREFIX: String { #"\\"# }

private var _standardizingPath: String {
@@ -616,7 +620,8 @@ extension String {
var standardizingPath: String {
expandingTildeInPath._standardizingPath
}
#endif // !NO_FILESYSTEM

#endif // !NO_FILESYSTEM

// _NSPathComponents
var pathComponents: [String] {
6 changes: 1 addition & 5 deletions Sources/FoundationEssentials/URL/URL.swift
Original file line number Diff line number Diff line change
@@ -1320,12 +1320,8 @@ public struct URL: Equatable, Sendable, Hashable {
}

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

var fileSystemPath: String {
15 changes: 15 additions & 0 deletions Tests/FoundationEssentialsTests/URLTests.swift
Original file line number Diff line number Diff line change
@@ -571,6 +571,21 @@ final class URLTests : XCTestCase {
XCTAssertEqual(appended.relativePath, "relative/with:slash")
}

func testURLFilePathDropsTrailingSlashes() throws {
var url = URL(filePath: "/path/slashes///")
XCTAssertEqual(url.path(), "/path/slashes///")
// TODO: Update this once .fileSystemPath uses backslashes for Windows
XCTAssertEqual(url.fileSystemPath, "/path/slashes")

url = URL(filePath: "/path/slashes/")
XCTAssertEqual(url.path(), "/path/slashes/")
XCTAssertEqual(url.fileSystemPath, "/path/slashes")

url = URL(filePath: "/path/slashes")
XCTAssertEqual(url.path(), "/path/slashes")
XCTAssertEqual(url.fileSystemPath, "/path/slashes")
}

func testURLComponentsPercentEncodedUnencodedProperties() throws {
var comp = URLComponents()