Skip to content

Commit b50ea3d

Browse files
committed
(142076445) Allow URL.standardized to return an empty string URL (swiftlang#1110)
* (142076445) Allow URL.standardized to return an empty string URL * Add ?? self to prevent force-unwrap
1 parent 5e31f0c commit b50ea3d

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

Sources/FoundationEssentials/URL/URL.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1837,7 +1837,7 @@ public struct URL: Equatable, Sendable, Hashable {
18371837
var components = URLComponents(parseInfo: _parseInfo)
18381838
let newPath = components.percentEncodedPath.removingDotSegments
18391839
components.percentEncodedPath = newPath
1840-
return components.url(relativeTo: baseURL)!
1840+
return components.url(relativeTo: baseURL) ?? self
18411841
}
18421842

18431843
/// Standardizes the path of a file URL by removing dot segments.

Sources/FoundationEssentials/URL/URLComponents.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ public struct URLComponents: Hashable, Equatable, Sendable {
676676
return CFURLCreateWithString(kCFAllocatorDefault, string as CFString, nil) as URL?
677677
}
678678
#endif
679-
return URL(string: string)
679+
return URL(string: string, relativeTo: nil)
680680
}
681681

682682
/// Returns a URL created from the URLComponents relative to a base URL.

Tests/FoundationEssentialsTests/URLTests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,6 +1425,12 @@ final class URLTests : XCTestCase {
14251425
XCTAssertEqual(comp.path, "/my\u{0}path")
14261426
}
14271427

1428+
func testURLStandardizedEmptyString() {
1429+
let url = URL(string: "../../../")!
1430+
let standardized = url.standardized
1431+
XCTAssertTrue(standardized.path().isEmpty)
1432+
}
1433+
14281434
#if FOUNDATION_FRAMEWORK
14291435
func testURLComponentsBridging() {
14301436
var nsURLComponents = NSURLComponents(

0 commit comments

Comments
 (0)