Skip to content

Commit 61811e3

Browse files
authored
(142076445) Allow URL.standardized to return an empty string URL (#1110)
* (142076445) Allow URL.standardized to return an empty string URL * Add ?? self to prevent force-unwrap
1 parent 7041a50 commit 61811e3

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
@@ -1847,7 +1847,7 @@ public struct URL: Equatable, Sendable, Hashable {
18471847
var components = URLComponents(parseInfo: _parseInfo)
18481848
let newPath = components.percentEncodedPath.removingDotSegments
18491849
components.percentEncodedPath = newPath
1850-
return components.url(relativeTo: baseURL)!
1850+
return components.url(relativeTo: baseURL) ?? self
18511851
}
18521852

18531853
/// 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
@@ -1405,6 +1405,12 @@ final class URLTests : XCTestCase {
14051405
XCTAssertEqual(comp.path, "/my\u{0}path")
14061406
}
14071407

1408+
func testURLStandardizedEmptyString() {
1409+
let url = URL(string: "../../../")!
1410+
let standardized = url.standardized
1411+
XCTAssertTrue(standardized.path().isEmpty)
1412+
}
1413+
14081414
#if FOUNDATION_FRAMEWORK
14091415
func testURLComponentsBridging() {
14101416
var nsURLComponents = NSURLComponents(

0 commit comments

Comments
 (0)