import Foundation // swiftlint:disable:next large_tuple private typealias Replace = (orig: String, asName: String, asCode: String) private let replaces = [ Replace("&", "&", "&"), Replace("<", "<", "<"), Replace(">", ">", ">"), Replace("\"", """, """), Replace("'", "'", "'") ] extension String { var unescaped: String { if !contains("&") { return self } var result = self var indx = replaces.count - 1 while indx >= 0 { // this is for libxml2 as it replaces every & with & which in XML both replaces & result = result.replacingOccurrences(of: replaces[indx].asCode, with: replaces[indx].orig) // this is for normal replacement (may not be needed but let's keep it for compatibility result = result.replacingOccurrences(of: replaces[indx].asName, with: replaces[indx].orig) indx -= 1 } return result } var escaped: String { var result = self var indx = 0 while indx < replaces.count { result = result.replacingOccurrences(of: replaces[indx].orig, with: replaces[indx].asName) indx += 1 } return result } } extension String { var prettyStr: String { var line = self line.replace(">", with: ">\n") line.replace("") { indentLevel += 1 } } return formattedString } }