@@ -537,26 +537,16 @@ public struct URL : ReferenceConvertible, Equatable {
537
537
///
538
538
/// 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).
539
539
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)
547
542
}
548
543
549
544
/// Initialize with string, relative to another URL.
550
545
///
551
546
/// 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).
552
547
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)
560
550
}
561
551
562
552
/// 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 {
885
875
/// If the URL has an empty path (e.g., `http://www.example.com`), then this function will return the URL unchanged.
886
876
public func deletingLastPathComponent( ) -> URL {
887
877
// 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
897
880
}
898
881
899
882
/// Returns a URL constructed by appending the given path extension to self.
@@ -903,30 +886,16 @@ public struct URL : ReferenceConvertible, Equatable {
903
886
/// 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.
904
887
/// - parameter pathExtension: The extension to append.
905
888
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
915
891
}
916
892
917
893
/// Returns a URL constructed by removing any path extension.
918
894
///
919
895
/// If the URL has an empty path (e.g., `http://www.example.com`), then this function will return the URL unchanged.
920
896
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
930
899
}
931
900
932
901
/// Appends a path component to the URL.
@@ -974,11 +943,8 @@ public struct URL : ReferenceConvertible, Equatable {
974
943
/// Returns a `URL` with any instances of ".." or "." removed from its path.
975
944
public var standardized : URL {
976
945
// 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
982
948
}
983
949
984
950
/// Standardizes the path of a file URL.
@@ -993,23 +959,17 @@ public struct URL : ReferenceConvertible, Equatable {
993
959
/// If the `isFileURL` is false, this method returns `self`.
994
960
public var standardizedFileURL : URL {
995
961
// 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
1001
964
}
1002
965
1003
966
/// Resolves any symlinks in the path of a file URL.
1004
967
///
1005
968
/// If the `isFileURL` is false, this method returns `self`.
1006
969
public func resolvingSymlinksInPath( ) -> URL {
1007
970
// 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
1013
973
}
1014
974
1015
975
/// Resolves any symlinks in the path of a file URL.
0 commit comments