@@ -433,26 +433,16 @@ public struct URL : ReferenceConvertible, Equatable {
433
433
///
434
434
/// 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).
435
435
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)
443
438
}
444
439
445
440
/// Initialize with string, relative to another URL.
446
441
///
447
442
/// 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).
448
443
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)
456
446
}
457
447
458
448
/// 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 {
750
740
/// If the URL has an empty path (e.g., `http://www.example.com`), then this function will return the URL unchanged.
751
741
public func deletingLastPathComponent( ) -> URL {
752
742
// 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
762
745
}
763
746
764
747
/// Returns a URL constructed by appending the given path extension to self.
@@ -768,30 +751,16 @@ public struct URL : ReferenceConvertible, Equatable {
768
751
/// 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.
769
752
/// - parameter pathExtension: The extension to append.
770
753
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
780
756
}
781
757
782
758
/// Returns a URL constructed by removing any path extension.
783
759
///
784
760
/// If the URL has an empty path (e.g., `http://www.example.com`), then this function will return the URL unchanged.
785
761
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
795
764
}
796
765
797
766
/// Appends a path component to the URL.
@@ -839,11 +808,8 @@ public struct URL : ReferenceConvertible, Equatable {
839
808
/// Returns a `URL` with any instances of ".." or "." removed from its path.
840
809
public var standardized : URL {
841
810
// 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
847
813
}
848
814
849
815
/// Standardizes the path of a file URL.
@@ -858,23 +824,17 @@ public struct URL : ReferenceConvertible, Equatable {
858
824
/// If the `isFileURL` is false, this method returns `self`.
859
825
public var standardizedFileURL : URL {
860
826
// 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
866
829
}
867
830
868
831
/// Resolves any symlinks in the path of a file URL.
869
832
///
870
833
/// If the `isFileURL` is false, this method returns `self`.
871
834
public func resolvingSymlinksInPath( ) -> URL {
872
835
// 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
878
838
}
879
839
880
840
/// Resolves any symlinks in the path of a file URL.
0 commit comments