Skip to content

Restore behavior of URL(string: "") returning nil #1103

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
Dec 20, 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
11 changes: 2 additions & 9 deletions Sources/FoundationEssentials/URL/URL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -777,20 +777,13 @@ public struct URL: Equatable, Sendable, Hashable {
///
/// Returns `nil` if a `URL` cannot be formed with the string (for example, if the string contains characters that are illegal in a URL, or is an empty string).
public init?(string: __shared String) {
guard !string.isEmpty else { return nil }
#if FOUNDATION_FRAMEWORK
guard foundation_swift_url_enabled() else {
guard !string.isEmpty, let inner = NSURL(string: string) else { return nil }
guard let inner = NSURL(string: string) else { return nil }
_url = URL._converted(from: inner)
return
}
// Linked-on-or-after check for apps which pass an empty string.
// The new URL(string:) implementations allow the empty string
// as input since an empty path is valid and can be resolved
// against a base URL. This is shown in the RFC 3986 examples:
// https://datatracker.ietf.org/doc/html/rfc3986#section-5.4.1
if Self.compatibility1 && string.isEmpty {
return nil
}
#endif // FOUNDATION_FRAMEWORK
guard let parseInfo = Parser.parse(urlString: string, encodingInvalidCharacters: true) else {
return nil
Expand Down