|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014-2017 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See http://swift.org/LICENSE.txt for license information |
| 9 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#if swift(>=5.9) |
| 14 | + |
| 15 | +/// A enum representing data types for legacy PropertyList type. |
| 16 | +/// Note that the `identifier` enum is not strictly necessary, |
| 17 | +/// but useful to semantically distinguish the strings that |
| 18 | +/// represents object identifiers from those that are just data. |
| 19 | +/// see: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/PropertyLists/OldStylePlists/OldStylePLists.html |
| 20 | +public enum PropertyList { |
| 21 | + case identifier(String) |
| 22 | + case string(String) |
| 23 | + case array([PropertyList]) |
| 24 | + case dictionary([String: PropertyList]) |
| 25 | + |
| 26 | + var string: String? { |
| 27 | + if case .string(let string) = self { |
| 28 | + return string |
| 29 | + } |
| 30 | + return nil |
| 31 | + } |
| 32 | + |
| 33 | + var array: [PropertyList]? { |
| 34 | + if case .array(let array) = self { |
| 35 | + return array |
| 36 | + } |
| 37 | + return nil |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +extension PropertyList: ExpressibleByStringLiteral { |
| 42 | + public typealias UnicodeScalarLiteralType = StringLiteralType |
| 43 | + public typealias ExtendedGraphemeClusterLiteralType = StringLiteralType |
| 44 | + |
| 45 | + public init(unicodeScalarLiteral value: UnicodeScalarLiteralType) { |
| 46 | + self = .string(value) |
| 47 | + } |
| 48 | + public init(extendedGraphemeClusterLiteral value: ExtendedGraphemeClusterLiteralType) { |
| 49 | + self = .string(value) |
| 50 | + } |
| 51 | + public init(stringLiteral value: StringLiteralType) { |
| 52 | + self = .string(value) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +extension PropertyList: CustomStringConvertible { |
| 57 | + public var description: String { |
| 58 | + return serialize() |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +extension PropertyList { |
| 63 | + /// Serializes the Plist enum to string. |
| 64 | + public func serialize() -> String { |
| 65 | + return generatePlistRepresentation(plist: self, indentation: Indentation()) |
| 66 | + } |
| 67 | + |
| 68 | + /// Escapes the string for plist. |
| 69 | + /// Finds the instances of quote (") and backward slash (\) and prepends |
| 70 | + /// the escape character backward slash (\). |
| 71 | + static func escape(string: String) -> String { |
| 72 | + func needsEscape(_ char: UInt8) -> Bool { |
| 73 | + return char == UInt8(ascii: "\\") || char == UInt8(ascii: "\"") |
| 74 | + } |
| 75 | + |
| 76 | + guard let pos = string.utf8.firstIndex(where: needsEscape) else { |
| 77 | + return string |
| 78 | + } |
| 79 | + var newString = String(string[..<pos]) |
| 80 | + for char in string.utf8[pos...] { |
| 81 | + if needsEscape(char) { |
| 82 | + newString += "\\" |
| 83 | + } |
| 84 | + newString += String(UnicodeScalar(char)) |
| 85 | + } |
| 86 | + return newString |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +extension PropertyList { |
| 91 | + /// Private struct to generate indentation strings. |
| 92 | + fileprivate struct Indentation: CustomStringConvertible { |
| 93 | + var level: Int = 0 |
| 94 | + mutating func increase() { |
| 95 | + level += 1 |
| 96 | + precondition(level > 0, "indentation level overflow") |
| 97 | + } |
| 98 | + mutating func decrease() { |
| 99 | + precondition(level > 0, "indentation level underflow") |
| 100 | + level -= 1 |
| 101 | + } |
| 102 | + var description: String { |
| 103 | + return String(repeating: " ", count: level) |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /// Private function to generate OPENSTEP-style plist representation. |
| 108 | + fileprivate func generatePlistRepresentation(plist: PropertyList, indentation: Indentation) -> String { |
| 109 | + // Do the appropriate thing for each type of plist node. |
| 110 | + switch plist { |
| 111 | + |
| 112 | + case .identifier(let ident): |
| 113 | + // FIXME: we should assert that the identifier doesn't need quoting |
| 114 | + return ident |
| 115 | + |
| 116 | + case .string(let string): |
| 117 | + return "\"" + PropertyList.escape(string: string) + "\"" |
| 118 | + |
| 119 | + case .array(let array): |
| 120 | + var indent = indentation |
| 121 | + var str = "(\n" |
| 122 | + indent.increase() |
| 123 | + for (i, item) in array.enumerated() { |
| 124 | + str += "\(indent)\(generatePlistRepresentation(plist: item, indentation: indent))" |
| 125 | + str += (i != array.count - 1) ? ",\n" : "\n" |
| 126 | + } |
| 127 | + indent.decrease() |
| 128 | + str += "\(indent))" |
| 129 | + return str |
| 130 | + |
| 131 | + case .dictionary(let dict): |
| 132 | + var indent = indentation |
| 133 | + let dict = dict.sorted(by: { |
| 134 | + // Make `isa` sort first (just for readability purposes). |
| 135 | + switch ($0.key, $1.key) { |
| 136 | + case ("isa", "isa"): return false |
| 137 | + case ("isa", _): return true |
| 138 | + case (_, "isa"): return false |
| 139 | + default: return $0.key < $1.key |
| 140 | + } |
| 141 | + }) |
| 142 | + var str = "{\n" |
| 143 | + indent.increase() |
| 144 | + for item in dict { |
| 145 | + str += "\(indent)\(item.key) = \(generatePlistRepresentation(plist: item.value, indentation: indent));\n" |
| 146 | + } |
| 147 | + indent.decrease() |
| 148 | + str += "\(indent)}" |
| 149 | + return str |
| 150 | + } |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +#endif |
0 commit comments