@@ -29,39 +29,40 @@ private func _standardizedPath(_ path: String) -> String {
29
29
}
30
30
31
31
internal func _pathComponents( _ path: String ? ) -> [ String ] ? {
32
- if let p = path {
33
- var result = [ String] ( )
34
- if p. length == 0 {
35
- return result
36
- } else {
37
- let characterView = p
38
- var curPos = characterView. startIndex
39
- let endPos = characterView. endIndex
40
- if characterView [ curPos] == " / " {
41
- result. append ( " / " )
32
+ guard let p = path else {
33
+ return nil
34
+ }
35
+
36
+ var result = [ String] ( )
37
+ if p. length == 0 {
38
+ return result
39
+ } else {
40
+ let characterView = p
41
+ var curPos = characterView. startIndex
42
+ let endPos = characterView. endIndex
43
+ if characterView [ curPos] == " / " {
44
+ result. append ( " / " )
45
+ }
46
+
47
+ while curPos < endPos {
48
+ while curPos < endPos && characterView [ curPos] == " / " {
49
+ curPos = characterView. index ( after: curPos)
42
50
}
43
-
44
- while curPos < endPos {
45
- while curPos < endPos && characterView [ curPos] == " / " {
46
- curPos = characterView. index ( after: curPos)
47
- }
48
- if curPos == endPos {
49
- break
50
- }
51
- var curEnd = curPos
52
- while curEnd < endPos && characterView [ curEnd] != " / " {
53
- curEnd = characterView. index ( after: curEnd)
54
- }
55
- result. append ( String ( characterView [ curPos ..< curEnd] ) )
56
- curPos = curEnd
51
+ if curPos == endPos {
52
+ break
57
53
}
54
+ var curEnd = curPos
55
+ while curEnd < endPos && characterView [ curEnd] != " / " {
56
+ curEnd = characterView. index ( after: curEnd)
57
+ }
58
+ result. append ( String ( characterView [ curPos ..< curEnd] ) )
59
+ curPos = curEnd
58
60
}
59
- if p. length > 1 && p. hasSuffix ( " / " ) {
60
- result. append ( " / " )
61
- }
62
- return result
63
61
}
64
- return nil
62
+ if p. length > 1 && p. hasSuffix ( " / " ) {
63
+ result. append ( " / " )
64
+ }
65
+ return result
65
66
}
66
67
67
68
public struct URLResourceKey : RawRepresentable , Equatable , Hashable {
@@ -708,41 +709,42 @@ extension NSURL {
708
709
}
709
710
710
711
internal func _pathByFixingSlashes( compress : Bool = true , stripTrailing: Bool = true ) -> String ? {
711
- if let p = path {
712
- if p == " / " {
713
- return p
714
- }
715
-
716
- var result = p
717
- if compress {
718
- result. withMutableCharacters { characterView in
719
- let startPos = characterView. startIndex
720
- var endPos = characterView. endIndex
721
- var curPos = startPos
722
-
723
- while curPos < endPos {
724
- if characterView [ curPos] == " / " {
725
- var afterLastSlashPos = curPos
726
- while afterLastSlashPos < endPos && characterView [ afterLastSlashPos] == " / " {
727
- afterLastSlashPos = characterView. index ( after: afterLastSlashPos)
728
- }
729
- if afterLastSlashPos != characterView. index ( after: curPos) {
730
- characterView. replaceSubrange ( curPos ..< afterLastSlashPos, with: [ " / " ] )
731
- endPos = characterView. endIndex
732
- }
733
- curPos = afterLastSlashPos
734
- } else {
735
- curPos = characterView. index ( after: curPos)
712
+ guard let p = path else {
713
+ return nil
714
+ }
715
+
716
+ if p == " / " {
717
+ return p
718
+ }
719
+
720
+ var result = p
721
+ if compress {
722
+ result. withMutableCharacters { characterView in
723
+ let startPos = characterView. startIndex
724
+ var endPos = characterView. endIndex
725
+ var curPos = startPos
726
+
727
+ while curPos < endPos {
728
+ if characterView [ curPos] == " / " {
729
+ var afterLastSlashPos = curPos
730
+ while afterLastSlashPos < endPos && characterView [ afterLastSlashPos] == " / " {
731
+ afterLastSlashPos = characterView. index ( after: afterLastSlashPos)
732
+ }
733
+ if afterLastSlashPos != characterView. index ( after: curPos) {
734
+ characterView. replaceSubrange ( curPos ..< afterLastSlashPos, with: [ " / " ] )
735
+ endPos = characterView. endIndex
736
736
}
737
+ curPos = afterLastSlashPos
738
+ } else {
739
+ curPos = characterView. index ( after: curPos)
737
740
}
738
741
}
739
742
}
740
- if stripTrailing && result. hasSuffix ( " / " ) {
741
- result. remove ( at: result. index ( before: result. endIndex) )
742
- }
743
- return result
744
743
}
745
- return nil
744
+ if stripTrailing && result. hasSuffix ( " / " ) {
745
+ result. remove ( at: result. index ( before: result. endIndex) )
746
+ }
747
+ return result
746
748
}
747
749
748
750
open var pathComponents : [ String ] ? {
@@ -1243,37 +1245,37 @@ open class NSURLComponents: NSObject, NSCopying {
1243
1245
open var queryItems : [ URLQueryItem ] ? {
1244
1246
get {
1245
1247
// This CFURL implementation returns a CFArray of CFDictionary; each CFDictionary has an entry for name and optionally an entry for value
1246
- if let queryArray = _CFURLComponentsCopyQueryItems ( _components) {
1247
- let count = CFArrayGetCount ( queryArray)
1248
-
1249
- return ( 0 ..< count) . map { idx in
1250
- let oneEntry = unsafeBitCast ( CFArrayGetValueAtIndex ( queryArray, idx) , to: NSDictionary . self)
1251
- let swiftEntry = oneEntry. _swiftObject
1252
- let entryName = swiftEntry [ " name " ] as! String
1253
- let entryValue = swiftEntry [ " value " ] as? String
1254
- return URLQueryItem ( name: entryName, value: entryValue)
1255
- }
1256
- } else {
1248
+ guard let queryArray = _CFURLComponentsCopyQueryItems ( _components) else {
1257
1249
return nil
1258
1250
}
1251
+
1252
+ let count = CFArrayGetCount ( queryArray)
1253
+ return ( 0 ..< count) . map { idx in
1254
+ let oneEntry = unsafeBitCast ( CFArrayGetValueAtIndex ( queryArray, idx) , to: NSDictionary . self)
1255
+ let swiftEntry = oneEntry. _swiftObject
1256
+ let entryName = swiftEntry [ " name " ] as! String
1257
+ let entryValue = swiftEntry [ " value " ] as? String
1258
+ return URLQueryItem ( name: entryName, value: entryValue)
1259
+ }
1259
1260
}
1260
1261
set ( new) {
1261
- if let new = new {
1262
- // The CFURL implementation requires two CFArrays, one for names and one for values
1263
- var names = [ CFTypeRef] ( )
1264
- var values = [ CFTypeRef] ( )
1265
- for entry in new {
1266
- names. append ( entry. name. _cfObject)
1267
- if let v = entry. value {
1268
- values. append ( v. _cfObject)
1269
- } else {
1270
- values. append ( kCFNull)
1271
- }
1272
- }
1273
- _CFURLComponentsSetQueryItems ( _components, names. _cfObject, values. _cfObject)
1274
- } else {
1262
+ guard let new = new else {
1275
1263
self . percentEncodedQuery = nil
1264
+ return
1265
+ }
1266
+
1267
+ // The CFURL implementation requires two CFArrays, one for names and one for values
1268
+ var names = [ CFTypeRef] ( )
1269
+ var values = [ CFTypeRef] ( )
1270
+ for entry in new {
1271
+ names. append ( entry. name. _cfObject)
1272
+ if let v = entry. value {
1273
+ values. append ( v. _cfObject)
1274
+ } else {
1275
+ values. append ( kCFNull)
1276
+ }
1276
1277
}
1278
+ _CFURLComponentsSetQueryItems ( _components, names. _cfObject, values. _cfObject)
1277
1279
}
1278
1280
}
1279
1281
}
0 commit comments