Skip to content

Commit 0c82d32

Browse files
authored
Merge pull request #2034 from Luigi123/refactor-url
Refactor if statements in URL.swift
2 parents b68fdef + 96b00e7 commit 0c82d32

File tree

1 file changed

+16
-56
lines changed

1 file changed

+16
-56
lines changed

Foundation/URL.swift

Lines changed: 16 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -433,26 +433,16 @@ public struct URL : ReferenceConvertible, Equatable {
433433
///
434434
/// 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).
435435
public init?(string: String) {
436-
guard !string.isEmpty else { return nil }
437-
438-
if let inner = NSURL(string: string) {
439-
_url = URL._converted(from: inner)
440-
} else {
441-
return nil
442-
}
436+
guard !string.isEmpty, let inner = NSURL(string: string) else { return nil }
437+
_url = URL._converted(from: inner)
443438
}
444439

445440
/// Initialize with string, relative to another URL.
446441
///
447442
/// 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).
448443
public init?(string: String, relativeTo url: URL?) {
449-
guard !string.isEmpty else { return nil }
450-
451-
if let inner = NSURL(string: string, relativeTo: url) {
452-
_url = URL._converted(from: inner)
453-
} else {
454-
return nil
455-
}
444+
guard !string.isEmpty, let inner = NSURL(string: string, relativeTo: url) else { return nil }
445+
_url = URL._converted(from: inner)
456446
}
457447

458448
/// Initializes a newly created file URL referencing the local file or directory at path, relative to a base URL.
@@ -750,15 +740,8 @@ public struct URL : ReferenceConvertible, Equatable {
750740
/// If the URL has an empty path (e.g., `http://www.example.com`), then this function will return the URL unchanged.
751741
public func deletingLastPathComponent() -> URL {
752742
// This is a slight behavior change from NSURL, but better than returning "http://www.example.com../".
753-
if path.isEmpty {
754-
return self
755-
}
756-
757-
if let result = _url.deletingLastPathComponent {
758-
return result
759-
} else {
760-
return self
761-
}
743+
guard !path.isEmpty, let result = _url.deletingLastPathComponent else { return self }
744+
return result
762745
}
763746

764747
/// Returns a URL constructed by appending the given path extension to self.
@@ -768,30 +751,16 @@ public struct URL : ReferenceConvertible, Equatable {
768751
/// 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.
769752
/// - parameter pathExtension: The extension to append.
770753
public func appendingPathExtension(_ pathExtension: String) -> URL {
771-
if path.isEmpty {
772-
return self
773-
}
774-
775-
if let result = _url.appendingPathExtension(pathExtension) {
776-
return result
777-
} else {
778-
return self
779-
}
754+
guard !path.isEmpty, let result = _url.appendingPathExtension(pathExtension) else { return self }
755+
return result
780756
}
781757

782758
/// Returns a URL constructed by removing any path extension.
783759
///
784760
/// If the URL has an empty path (e.g., `http://www.example.com`), then this function will return the URL unchanged.
785761
public func deletingPathExtension() -> URL {
786-
if path.isEmpty {
787-
return self
788-
}
789-
790-
if let result = _url.deletingPathExtension {
791-
return result
792-
} else {
793-
return self
794-
}
762+
guard !path.isEmpty, let result = _url.deletingPathExtension else { return self }
763+
return result
795764
}
796765

797766
/// Appends a path component to the URL.
@@ -839,11 +808,8 @@ public struct URL : ReferenceConvertible, Equatable {
839808
/// Returns a `URL` with any instances of ".." or "." removed from its path.
840809
public var standardized : URL {
841810
// The NSURL API can only return nil in case of file reference URL, which we should not be
842-
if let result = _url.standardized {
843-
return result
844-
} else {
845-
return self
846-
}
811+
guard let result = _url.standardized else { return self }
812+
return result
847813
}
848814

849815
/// Standardizes the path of a file URL.
@@ -858,23 +824,17 @@ public struct URL : ReferenceConvertible, Equatable {
858824
/// If the `isFileURL` is false, this method returns `self`.
859825
public var standardizedFileURL : URL {
860826
// NSURL should not return nil here unless this is a file reference URL, which should be impossible
861-
if let result = _url.standardizingPath {
862-
return result
863-
} else {
864-
return self
865-
}
827+
guard let result = _url.standardizingPath else { return self }
828+
return result
866829
}
867830

868831
/// Resolves any symlinks in the path of a file URL.
869832
///
870833
/// If the `isFileURL` is false, this method returns `self`.
871834
public func resolvingSymlinksInPath() -> URL {
872835
// NSURL should not return nil here unless this is a file reference URL, which should be impossible
873-
if let result = _url.resolvingSymlinksInPath {
874-
return result
875-
} else {
876-
return self
877-
}
836+
guard let result = _url.resolvingSymlinksInPath else { return self }
837+
return result
878838
}
879839

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

0 commit comments

Comments
 (0)