@@ -29,39 +29,40 @@ private func _standardizedPath(_ path: String) -> String {
2929}
3030
3131internal 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)
4250 }
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
5753 }
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
5860 }
59- if p. length > 1 && p. hasSuffix ( " / " ) {
60- result. append ( " / " )
61- }
62- return result
6361 }
64- return nil
62+ if p. length > 1 && p. hasSuffix ( " / " ) {
63+ result. append ( " / " )
64+ }
65+ return result
6566}
6667
6768public struct URLResourceKey : RawRepresentable , Equatable , Hashable {
@@ -708,41 +709,42 @@ extension NSURL {
708709 }
709710
710711 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
736736 }
737+ curPos = afterLastSlashPos
738+ } else {
739+ curPos = characterView. index ( after: curPos)
737740 }
738741 }
739742 }
740- if stripTrailing && result. hasSuffix ( " / " ) {
741- result. remove ( at: result. index ( before: result. endIndex) )
742- }
743- return result
744743 }
745- return nil
744+ if stripTrailing && result. hasSuffix ( " / " ) {
745+ result. remove ( at: result. index ( before: result. endIndex) )
746+ }
747+ return result
746748 }
747749
748750 open var pathComponents : [ String ] ? {
@@ -1243,37 +1245,37 @@ open class NSURLComponents: NSObject, NSCopying {
12431245 open var queryItems : [ URLQueryItem ] ? {
12441246 get {
12451247 // 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 {
12571249 return nil
12581250 }
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+ }
12591260 }
12601261 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 {
12751263 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+ }
12761277 }
1278+ _CFURLComponentsSetQueryItems ( _components, names. _cfObject, values. _cfObject)
12771279 }
12781280 }
12791281}
0 commit comments