XQueryComponents.swift 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import Foundation
  21. import CocoaLumberjackSwift
  22. @objc extension NSString {
  23. func decodingURLFormat() -> String {
  24. var result = replacingOccurrences(of: "+", with: " ")
  25. result = result.removingPercentEncoding ?? ""
  26. return result
  27. }
  28. func encodingURLFormat() -> String {
  29. var result = replacingOccurrences(of: " ", with: "+")
  30. result = result.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlFragmentAllowed) ?? ""
  31. return result
  32. }
  33. @objc func dictionaryFromQueryComponents() -> [AnyHashable : Any]? {
  34. var queryComponents: [AnyHashable : Any] = [:]
  35. for keyValuePairString in components(separatedBy: "&") {
  36. let keyValuePairArray = keyValuePairString.components(separatedBy: "=")
  37. if keyValuePairArray.count < 2 {
  38. continue
  39. }
  40. let key = keyValuePairArray[0].decodingURLFormat()
  41. let value = keyValuePairArray[1].decodingURLFormat()
  42. var results = queryComponents[key] as? [AnyHashable]
  43. if results == nil {
  44. results = [AnyHashable]()
  45. queryComponents[key] = results
  46. }
  47. results?.append(value)
  48. }
  49. return queryComponents
  50. }
  51. }
  52. extension NSDictionary {
  53. @objc func stringFromQueryComponents() -> String? {
  54. var result: String? = nil
  55. for key in self.allKeys {
  56. guard var key = key as? String else {
  57. continue
  58. }
  59. key = key.encodingURLFormat()
  60. if let allValues = self[key] as? [AnyHashable] {
  61. for value in allValues {
  62. guard var value = value as? String else {
  63. continue
  64. }
  65. value = value.description.encodingURLFormat()
  66. if result == nil {
  67. result = "\(key)=\(value)"
  68. } else {
  69. result = (result ?? "") + "&\(key)=\(value)"
  70. }
  71. }
  72. } else if let allValues = self[key] as? AnyHashable {
  73. let value = allValues.description.encodingURLFormat()
  74. if result == nil {
  75. result = "\(key)=\(value )"
  76. } else {
  77. result = (result ?? "") + "&\(key)=\(value )"
  78. }
  79. }
  80. }
  81. return result
  82. }
  83. }