Skip to content

Support NS/CFURL re-core in Swift #1238

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 5 commits into from
Apr 12, 2025
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions Sources/FoundationEssentials/CodableUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ extension UInt8 {
internal static var _exclamation: UInt8 { UInt8(ascii: "!") }
internal static var _ampersand: UInt8 { UInt8(ascii: "&") }
internal static var _pipe: UInt8 { UInt8(ascii: "|") }
internal static var _percent: UInt8 { UInt8(ascii: "%") }
internal static var _period: UInt8 { UInt8(ascii: ".") }
internal static var _e: UInt8 { UInt8(ascii: "e") }
internal static var _E: UInt8 { UInt8(ascii: "E") }
Expand Down
12 changes: 10 additions & 2 deletions Sources/FoundationEssentials/String/String+Path.swift
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,11 @@ extension String {
guard !pathExtension.isEmpty, validatePathExtension(pathExtension) else {
return self
}
if self == "/" { return "/.\(pathExtension)"}
var result = self._droppingTrailingSlashes
guard result != "/" else {
if result == "/" {
// Path was all slashes
return self + ".\(pathExtension)"
return Substring(self.utf8.dropLast()) + ".\(pathExtension)/"
}
result += ".\(pathExtension)"
if utf8.last == ._slash {
Expand Down Expand Up @@ -404,6 +405,13 @@ extension String {
}
}

internal var _droppingTrailingSlash: String {
guard utf8.last == ._slash, utf8.count > 1 else {
return self
}
return String(Substring(utf8.dropLast()))
}

internal var _droppingTrailingSlashes: String {
guard !self.isEmpty else {
return self
Expand Down
4 changes: 4 additions & 0 deletions Sources/FoundationEssentials/URL/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

target_sources(FoundationEssentials PRIVATE
URL.swift
URL_Bridge.swift
URL_ObjC.swift
URL_Protocol.swift
URL_Swift.swift
URLComponents.swift
URLComponents_ObjC.swift
URLParser.swift)
1,551 changes: 198 additions & 1,353 deletions Sources/FoundationEssentials/URL/URL.swift

Large diffs are not rendered by default.

71 changes: 71 additions & 0 deletions Sources/FoundationEssentials/URL/URLComponents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,56 @@ public struct URLComponents: Hashable, Equatable, Sendable {
return result
}

internal func _uncheckedString(original: Bool) -> String {
let componentsToDecode = original ? urlParseInfo?.encodedComponents ?? [] : []
var result = ""
if let scheme {
result += "\(scheme):"
}
if hasAuthority {
result += "//"
}
if componentsToDecode.contains(.user), let user {
result += user
} else if let percentEncodedUser {
result += percentEncodedUser
}
if componentsToDecode.contains(.password), let password {
result += ":\(password)"
} else if let percentEncodedPassword {
result += ":\(percentEncodedPassword)"
}
if percentEncodedUser != nil || percentEncodedPassword != nil {
result += "@"
}
if componentsToDecode.contains(.host), let host {
result += host
} else if let encodedHost {
result += encodedHost
}
if parseInfoIsValidForPort, let portString = urlParseInfo?.portString {
result += ":\(portString)"
} else if let port {
result += ":\(port)"
}
if componentsToDecode.contains(.path) {
result += path
} else {
result += percentEncodedPath
}
if componentsToDecode.contains(.query), let query {
result += "?\(query)"
} else if let percentEncodedQuery {
result += "?\(percentEncodedQuery)"
}
if componentsToDecode.contains(.fragment), let fragment {
result += "#\(fragment)"
} else if let percentEncodedFragment {
result += "#\(percentEncodedFragment)"
}
return result
}

func rangeOf(_ component: Component) -> Range<String.Index>? {
if let urlParseInfo, parseInfoIsValidForAllRanges {
switch component {
Expand Down Expand Up @@ -694,6 +744,21 @@ public struct URLComponents: Hashable, Equatable, Sendable {
self.components = _URLComponents(parseInfo: parseInfo)
}

#if FOUNDATION_FRAMEWORK
internal init?(url: _BridgedURL, resolvingAgainstBaseURL resolve: Bool) {
let string: String
if resolve {
string = url.absoluteString
} else {
string = url.relativeString
}
guard let components = _URLComponents(string: string) else {
return nil
}
self.components = components
}
#endif

/// Returns a URL created from the URLComponents.
///
/// If the URLComponents has an authority component (user, password, host or port) and a path component, then the path must either begin with "/" or be an empty string. If the NSURLComponents does not have an authority component (user, password, host or port) and has a path component, the path component must not start with "//". If those requirements are not met, nil is returned.
Expand Down Expand Up @@ -729,6 +794,12 @@ public struct URLComponents: Hashable, Equatable, Sendable {
components.string
}

/// For use by URL to get a non-nil string, potentially decoding components to return an original string.
/// This does not provide any validation, since URL is historically less strict than URLComponents.
internal func _uncheckedString(original: Bool) -> String {
components._uncheckedString(original: original)
}

/// The scheme subcomponent of the URL.
///
/// The getter for this property removes any percent encoding this component may have (if the component allows percent encoding). Setting this property assumes the subcomponent or component string is not percent encoded and will add percent encoding (if the component allows percent encoding).
Expand Down
4 changes: 2 additions & 2 deletions Sources/FoundationEssentials/URL/URLComponents_ObjC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ extension NSURLComponents {
return _NSSwiftURLComponents(components: components)
}

static func _parseString(_ string: String, encodingInvalidCharacters: Bool, compatibility: URLParserCompatibility.RawValue) -> String? {
return RFC3986Parser.parse(urlString: string, encodingInvalidCharacters: encodingInvalidCharacters, compatibility: .init(rawValue: compatibility))?.urlString
static func _parseString(_ string: String, encodingInvalidCharacters: Bool, allowEmptyScheme: Bool) -> String? {
return RFC3986Parser.parse(urlString: string, encodingInvalidCharacters: encodingInvalidCharacters, allowEmptyScheme: allowEmptyScheme)?.urlString
}
}

Expand Down
Loading