LEFT | RIGHT |
1 // | 1 // |
2 // FavIcon | 2 // FavIcon |
3 // Copyright © 2016 Leon Breedt | 3 // Copyright © 2016 Leon Breedt |
4 // | 4 // |
5 // Licensed under the Apache License, Version 2.0 (the "License"); | 5 // Licensed under the Apache License, Version 2.0 (the "License"); |
6 // you may not use this file except in compliance with the License. | 6 // you may not use this file except in compliance with the License. |
7 // You may obtain a copy of the License at | 7 // You may obtain a copy of the License at |
8 // | 8 // |
9 // http://www.apache.org/licenses/LICENSE-2.0 | 9 // http://www.apache.org/licenses/LICENSE-2.0 |
10 // | 10 // |
11 // Unless required by applicable law or agreed to in writing, software | 11 // Unless required by applicable law or agreed to in writing, software |
12 // distributed under the License is distributed on an "AS IS" BASIS, | 12 // distributed under the License is distributed on an "AS IS" BASIS, |
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 // See the License for the specific language governing permissions and | 14 // See the License for the specific language governing permissions and |
15 // limitations under the License. | 15 // limitations under the License. |
16 // | 16 // |
17 | 17 |
18 extension String { | 18 extension String { |
19 /// Parses this string as an HTTP Content-Type header. | 19 /// Parses this string as an HTTP Content-Type header. |
20 /// - returns: A tuple containing the mime type and string, if this could be
determined. | 20 /// - returns: A tuple containing the mime type and string, if this could be
determined. |
21 func parseAsHTTPContentTypeHeader() -> (mimeType: String, encoding: String.E
ncoding) { | 21 func parseAsHTTPContentTypeHeader() -> (mimeType: String, encoding: String.E
ncoding) { |
22 let headerComponents = | 22 let headerComponents = |
23 components(separatedBy: ";") | 23 components(separatedBy: ";") |
24 .map { $0.trimmingCharacters(in: .whitespaces) } | 24 .map { $0.trimmingCharacters(in: .whitespaces) } |
25 | 25 |
26 if headerComponents.count > 1 { | 26 if headerComponents.count > 1 { |
27 let parameters = | 27 let parameters = |
28 headerComponents[1..<headerComponents.count] | 28 headerComponents[1..<headerComponents.count] |
29 .filter { $0.contains("=") } | 29 .filter { $0.contains("=") } |
30 .map { $0.components(separatedBy: "=") } | 30 .map { $0.components(separatedBy: "=") } |
31 .toDictionary { ($0[0], $0[1]) } | 31 .toDictionary { ($0[0], $0[1]) } |
32 | 32 |
33 // Default according to RFC is ISO-8859-1, but probably nothing obey
s that, so default | 33 // Default according to RFC is ISO-8859-1, but probably nothing obey
s that, so default |
34 // to UTF-8 instead. | 34 // to UTF-8 instead. |
35 var encoding = String.Encoding.utf8 | 35 var encoding = String.Encoding.utf8 |
36 if let charset = parameters["charset"], let parsedEncoding = charset
.parseAsStringEncoding() { | 36 if let charset = parameters["charset"], let parsedEncoding = charset
.parseAsStringEncoding() { |
37 encoding = parsedEncoding | 37 encoding = parsedEncoding |
38 } | 38 } |
39 | 39 |
40 return (mimeType: headerComponents[0], encoding: encoding) | 40 return (mimeType: headerComponents[0], encoding: encoding) |
41 } else { | 41 } else { |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 /// - returns: A dictionary having items of type `K` as keys, and type `V` a
s values. | 106 /// - returns: A dictionary having items of type `K` as keys, and type `V` a
s values. |
107 func toDictionary<K, V>(_ transform: (Element) -> (K, V)) -> [K: V] { | 107 func toDictionary<K, V>(_ transform: (Element) -> (K, V)) -> [K: V] { |
108 var dict: [K: V] = [:] | 108 var dict: [K: V] = [:] |
109 for item in self { | 109 for item in self { |
110 let (key, value) = transform(item) | 110 let (key, value) = transform(item) |
111 dict[key] = value | 111 dict[key] = value |
112 } | 112 } |
113 return dict | 113 return dict |
114 } | 114 } |
115 } | 115 } |
LEFT | RIGHT |