import Foundation #if canImport(ExtrasBase64) import ExtrasBase64 #endif extension Data { /// Same as ``Data(base64Encoded:)``, but adds padding automatically /// (if missing, instead of returning `nil`). fileprivate static func fromBase64(_ encoded: String) -> Data? { // Prefixes padding-character(s) (if needed). var encoded = encoded; let remainder = encoded.count % 4 if remainder > 0 { encoded = encoded.padding( toLength: encoded.count + 4 - remainder, withPad: "=", startingAt: 0); } // Finally, decode. return Data(base64Encoded: encoded); } } extension String { fileprivate static func fromBase64(_ encoded: String) -> String? { if let data = Data.fromBase64(encoded) { return String(data: data, encoding: .utf8) } return nil; } } func precompileIncludeStr(_ path: String) -> String { var content: String = "" switch (path) { // default: fatalError("Error: include file not found: \(path)") } #if canImport(ExtrasBase64) return String(bytes: try! Base64.decode(string: content), encoding: .utf8) ?? "" #else return String.fromBase64(content) ?? "" #endif } func precompileIncludeData(_ path: String) -> Data { var content: String = "" switch (path) { // default: fatalError("Error: include file not found: \(path)") } return Data.fromBase64(content) ?? Data() }