From 34b35d2e4ca27db3ece1b420136d30ed6c5a556a Mon Sep 17 00:00:00 2001 From: Jonathan Flat Date: Thu, 2 Jan 2025 19:12:54 -0700 Subject: [PATCH 1/2] (142076445) Allow URL.standardized to return an empty string URL --- Sources/FoundationEssentials/URL/URLComponents.swift | 2 +- Tests/FoundationEssentialsTests/URLTests.swift | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Sources/FoundationEssentials/URL/URLComponents.swift b/Sources/FoundationEssentials/URL/URLComponents.swift index f5ce53ae7..6eb3a6680 100644 --- a/Sources/FoundationEssentials/URL/URLComponents.swift +++ b/Sources/FoundationEssentials/URL/URLComponents.swift @@ -676,7 +676,7 @@ public struct URLComponents: Hashable, Equatable, Sendable { return CFURLCreateWithString(kCFAllocatorDefault, string as CFString, nil) as URL? } #endif - return URL(string: string) + return URL(string: string, relativeTo: nil) } /// Returns a URL created from the URLComponents relative to a base URL. diff --git a/Tests/FoundationEssentialsTests/URLTests.swift b/Tests/FoundationEssentialsTests/URLTests.swift index ed3ef5a36..1017254ac 100644 --- a/Tests/FoundationEssentialsTests/URLTests.swift +++ b/Tests/FoundationEssentialsTests/URLTests.swift @@ -1405,6 +1405,12 @@ final class URLTests : XCTestCase { XCTAssertEqual(comp.path, "/my\u{0}path") } + func testURLStandardizedEmptyString() { + let url = URL(string: "../../../")! + let standardized = url.standardized + XCTAssertTrue(standardized.path().isEmpty) + } + #if FOUNDATION_FRAMEWORK func testURLComponentsBridging() { var nsURLComponents = NSURLComponents( From 6e4d8fc7b96327a5c2ace03b5825cd63587ed1f9 Mon Sep 17 00:00:00 2001 From: Jonathan Flat Date: Fri, 3 Jan 2025 10:56:57 -0700 Subject: [PATCH 2/2] Add ?? self to prevent force-unwrap --- Sources/FoundationEssentials/URL/URL.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/FoundationEssentials/URL/URL.swift b/Sources/FoundationEssentials/URL/URL.swift index 1b117efea..44c367854 100644 --- a/Sources/FoundationEssentials/URL/URL.swift +++ b/Sources/FoundationEssentials/URL/URL.swift @@ -1847,7 +1847,7 @@ public struct URL: Equatable, Sendable, Hashable { var components = URLComponents(parseInfo: _parseInfo) let newPath = components.percentEncodedPath.removingDotSegments components.percentEncodedPath = newPath - return components.url(relativeTo: baseURL)! + return components.url(relativeTo: baseURL) ?? self } /// Standardizes the path of a file URL by removing dot segments.