Skip to content

Commit 6aadec9

Browse files
committed
Refactor if statements in URL.swift
I refactored some of the if statements in URL.swift to be guard instead. This made the code shorter and easier to read. This mirrors my commit on swift-corelibs-foundation (PR #2034)
1 parent 3b3ee3f commit 6aadec9

File tree

1 file changed

+16
-56
lines changed

1 file changed

+16
-56
lines changed

Darwin/Foundation-swiftoverlay/URL.swift

Lines changed: 16 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -537,26 +537,16 @@ public struct URL : ReferenceConvertible, Equatable {
537537
///
538538
/// 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).
539539
public init?(string: __shared String) {
540-
guard !string.isEmpty else { return nil }
541-
542-
if let inner = NSURL(string: string) {
543-
_url = URL._converted(from: inner)
544-
} else {
545-
return nil
546-
}
540+
guard !string.isEmpty, let inner = NSURL(string: string) else { return nil }
541+
_url = URL._converted(from: inner)
547542
}
548543

549544
/// Initialize with string, relative to another URL.
550545
///
551546
/// 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).
552547
public init?(string: __shared String, relativeTo url: __shared URL?) {
553-
guard !string.isEmpty else { return nil }
554-
555-
if let inner = NSURL(string: string, relativeTo: url) {
556-
_url = URL._converted(from: inner)
557-
} else {
558-
return nil
559-
}
548+
guard !string.isEmpty, let inner = NSURL(string: string, relativeTo: url) else { return nil }
549+
_url = URL._converted(from: inner)
560550
}
561551

562552
/// Initializes a newly created file URL referencing the local file or directory at path, relative to a base URL.
@@ -885,15 +875,8 @@ public struct URL : ReferenceConvertible, Equatable {
885875
/// If the URL has an empty path (e.g., `http://www.example.com`), then this function will return the URL unchanged.
886876
public func deletingLastPathComponent() -> URL {
887877
// This is a slight behavior change from NSURL, but better than returning "http://www.example.com../".
888-
if path.isEmpty {
889-
return self
890-
}
891-
892-
if let result = _url.deletingLastPathComponent.map({ URL(reference: $0 as NSURL) }) {
893-
return result
894-
} else {
895-
return self
896-
}
878+
guard !path.isEmpty, let result = _url.deletingLastPathComponent.map({ URL(reference: $0 as NSURL) }) else { return self }
879+
return result
897880
}
898881

899882
/// Returns a URL constructed by appending the given path extension to self.
@@ -903,30 +886,16 @@ public struct URL : ReferenceConvertible, Equatable {
903886
/// 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.
904887
/// - parameter pathExtension: The extension to append.
905888
public func appendingPathExtension(_ pathExtension: String) -> URL {
906-
if path.isEmpty {
907-
return self
908-
}
909-
910-
if let result = _url.appendingPathExtension(pathExtension) {
911-
return result
912-
} else {
913-
return self
914-
}
889+
guard !path.isEmpty, let result = _url.appendingPathExtension(pathExtension) else { return self }
890+
return result
915891
}
916892

917893
/// Returns a URL constructed by removing any path extension.
918894
///
919895
/// If the URL has an empty path (e.g., `http://www.example.com`), then this function will return the URL unchanged.
920896
public func deletingPathExtension() -> URL {
921-
if path.isEmpty {
922-
return self
923-
}
924-
925-
if let result = _url.deletingPathExtension.map({ URL(reference: $0 as NSURL) }) {
926-
return result
927-
} else {
928-
return self
929-
}
897+
guard !path.isEmpty, let result = _url.deletingPathExtension.map({ URL(reference: $0 as NSURL) }) else { return self }
898+
return result
930899
}
931900

932901
/// Appends a path component to the URL.
@@ -974,11 +943,8 @@ public struct URL : ReferenceConvertible, Equatable {
974943
/// Returns a `URL` with any instances of ".." or "." removed from its path.
975944
public var standardized : URL {
976945
// The NSURL API can only return nil in case of file reference URL, which we should not be
977-
if let result = _url.standardized.map({ URL(reference: $0 as NSURL) }) {
978-
return result
979-
} else {
980-
return self
981-
}
946+
guard let result = _url.standardized.map({ URL(reference: $0 as NSURL) }) else { return self }
947+
return result
982948
}
983949

984950
/// Standardizes the path of a file URL.
@@ -993,23 +959,17 @@ public struct URL : ReferenceConvertible, Equatable {
993959
/// If the `isFileURL` is false, this method returns `self`.
994960
public var standardizedFileURL : URL {
995961
// NSURL should not return nil here unless this is a file reference URL, which should be impossible
996-
if let result = _url.standardizingPath.map({ URL(reference: $0 as NSURL) }) {
997-
return result
998-
} else {
999-
return self
1000-
}
962+
guard let result = _url.standardizingPath.map({ URL(reference: $0 as NSURL) }) else { return self }
963+
return result
1001964
}
1002965

1003966
/// Resolves any symlinks in the path of a file URL.
1004967
///
1005968
/// If the `isFileURL` is false, this method returns `self`.
1006969
public func resolvingSymlinksInPath() -> URL {
1007970
// NSURL should not return nil here unless this is a file reference URL, which should be impossible
1008-
if let result = _url.resolvingSymlinksInPath.map({ URL(reference: $0 as NSURL) }) {
1009-
return result
1010-
} else {
1011-
return self
1012-
}
971+
guard let result = _url.resolvingSymlinksInPath.map({ URL(reference: $0 as NSURL) }) else { return self }
972+
return result
1013973
}
1014974

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

0 commit comments

Comments
 (0)