Skip to content

Refactor if statements in URL.swift #2034

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
Mar 23, 2019
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
72 changes: 16 additions & 56 deletions Foundation/URL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -433,26 +433,16 @@ public struct URL : ReferenceConvertible, Equatable {
///
/// 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: String) {
guard !string.isEmpty else { return nil }

if let inner = NSURL(string: string) {
_url = URL._converted(from: inner)
} else {
return nil
}
guard !string.isEmpty, let inner = NSURL(string: string) else { return nil }
_url = URL._converted(from: inner)
}

/// Initialize with string, relative to another URL.
///
/// 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: String, relativeTo url: URL?) {
guard !string.isEmpty else { return nil }

if let inner = NSURL(string: string, relativeTo: url) {
_url = URL._converted(from: inner)
} else {
return nil
}
guard !string.isEmpty, let inner = NSURL(string: string, relativeTo: url) else { return nil }
_url = URL._converted(from: inner)
}

/// Initializes a newly created file URL referencing the local file or directory at path, relative to a base URL.
Expand Down Expand Up @@ -750,15 +740,8 @@ public struct URL : ReferenceConvertible, Equatable {
/// If the URL has an empty path (e.g., `http://www.example.com`), then this function will return the URL unchanged.
public func deletingLastPathComponent() -> URL {
// This is a slight behavior change from NSURL, but better than returning "http://www.example.com../".
if path.isEmpty {
return self
}

if let result = _url.deletingLastPathComponent {
return result
} else {
return self
}
guard !path.isEmpty, let result = _url.deletingLastPathComponent else { return self }
return result
}

/// Returns a URL constructed by appending the given path extension to self.
Expand All @@ -768,30 +751,16 @@ public struct URL : ReferenceConvertible, Equatable {
/// Certain special characters (for example, Unicode Right-To-Left marks) cannot be used as path extensions. If any of those are contained in `pathExtension`, the function will return the URL unchanged.
/// - parameter pathExtension: The extension to append.
public func appendingPathExtension(_ pathExtension: String) -> URL {
if path.isEmpty {
return self
}

if let result = _url.appendingPathExtension(pathExtension) {
return result
} else {
return self
}
guard !path.isEmpty, let result = _url.appendingPathExtension(pathExtension) else { return self }
return result
}

/// Returns a URL constructed by removing any path extension.
///
/// If the URL has an empty path (e.g., `http://www.example.com`), then this function will return the URL unchanged.
public func deletingPathExtension() -> URL {
if path.isEmpty {
return self
}

if let result = _url.deletingPathExtension {
return result
} else {
return self
}
guard !path.isEmpty, let result = _url.deletingPathExtension else { return self }
return result
}

/// Appends a path component to the URL.
Expand Down Expand Up @@ -839,11 +808,8 @@ public struct URL : ReferenceConvertible, Equatable {
/// Returns a `URL` with any instances of ".." or "." removed from its path.
public var standardized : URL {
// The NSURL API can only return nil in case of file reference URL, which we should not be
if let result = _url.standardized {
return result
} else {
return self
}
guard let result = _url.standardized else { return self }
return result
}

/// Standardizes the path of a file URL.
Expand All @@ -858,23 +824,17 @@ public struct URL : ReferenceConvertible, Equatable {
/// If the `isFileURL` is false, this method returns `self`.
public var standardizedFileURL : URL {
// NSURL should not return nil here unless this is a file reference URL, which should be impossible
if let result = _url.standardizingPath {
return result
} else {
return self
}
guard let result = _url.standardizingPath else { return self }
return result
}

/// Resolves any symlinks in the path of a file URL.
///
/// If the `isFileURL` is false, this method returns `self`.
public func resolvingSymlinksInPath() -> URL {
// NSURL should not return nil here unless this is a file reference URL, which should be impossible
if let result = _url.resolvingSymlinksInPath {
return result
} else {
return self
}
guard let result = _url.resolvingSymlinksInPath else { return self }
return result
}

/// Resolves any symlinks in the path of a file URL.
Expand Down