public extension PureYAML.Tagged.Scalar { /// The scalar's `!!timestamp` value: non-`nil` when the scalar carries /// the standard timestamp tag and its raw content parses under the /// YAML 1.1 timestamp grammar. Bridge to a Foundation date with /// `Date(timeIntervalSince1970: timestampValue!.secondsSinceEpoch)`; /// the package itself stays Foundation-free. var timestampValue: PureYAML.Tagged.Timestamp? { guard tag == .timestamp else { return nil } return PureYAML.Tagged.Timestamp(yaml: rawValue) } /// The scalar's `!!binary` value: non-`nil` when the scalar carries the /// standard binary tag and its raw content is valid base64 (whitespace /// and line breaks between groups are ignored, matching base64 content /// spread over a block scalar). Bridge to Foundation with /// `Data(binaryValue!)`; the package itself stays Foundation-free. var binaryValue: [UInt8]? { guard tag == .binary else { return nil } return PureYAML.Tagged.Scalar.decodeBase64(rawValue) } /// Pure-Swift base64 decoding over the standard alphabet with `=` /// padding. Whitespace is skipped; any other non-alphabet character /// rejects the whole input rather than decoding a prefix. static func decodeBase64(_ text: String) -> [UInt8]? { var bytes: [UInt8] = [] var buffer = 0 var bitCount = 0 var padding = 0 for character in text { if character == " " || character == "\n" || character == "\t" || character == "\r" { continue } if character == "=" { padding += 1 continue } guard padding == 0, let value = base64Value(of: character) else { return nil } buffer = (buffer << 6) | value bitCount += 6 if bitCount >= 8 { bitCount -= 8 bytes.append(UInt8((buffer >> bitCount) & 0xFF)) } } // Leftover bits must be zero-padding from the final group, and the // group structure must be consistent: 8 leftover bits (two missing // characters) pairs with two `=`, 4 bits with one, 0 with none. let expectedPadding = bitCount == 4 ? 2 : bitCount == 2 ? 1 : 0 guard bitCount != 6, padding == expectedPadding || padding == 0, buffer & ((1 << bitCount) - 1) == 0 || bitCount == 0 else { return nil } return bytes } private static func base64Value(of character: Character) -> Int? { guard let ascii = character.asciiValue else { return nil } switch ascii { case 65 ... 90: return Int(ascii - 65) case 97 ... 122: return Int(ascii - 97) + 26 case 48 ... 57: return Int(ascii - 48) + 52 case 43: return 62 case 47: return 63 default: return nil } } }