Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 0 additions & 29 deletions Sources/BinaryParseKit/BuiltInExtensions.swift

This file was deleted.

143 changes: 0 additions & 143 deletions Sources/BinaryParseKit/CustomExtensions.swift

This file was deleted.

9 changes: 9 additions & 0 deletions Sources/BinaryParseKit/Extensions/Data+SizedParsable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//
// Data+SizedParsable.swift
// BinaryParseKit
//
// Created by Larry Zeng on 11/28/25.
//
import Foundation

extension Data: SizedParsable {}
51 changes: 51 additions & 0 deletions Sources/BinaryParseKit/Extensions/FloatingPointExtensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//
// FloatingPointExtensions.swift
// BinaryParseKit
//
// Created by Larry Zeng on 7/16/25.
//
import BinaryParsing

/// A protocol for types that can be initialized from a bit pattern.
///
/// This protocol enables binary floating-point types to be parsed from
/// their underlying bit representation, allowing for precise control
/// over how floating-point values are interpreted from binary data.
public protocol ExpressibleByBitPattern {
/// The underlying integer type that represents the bit pattern.
associatedtype BitPattern: FixedWidthInteger & BitwiseCopyable

/// Initializes a value from its bit pattern representation.
///
/// - Parameter bitPattern: The bit pattern to interpret as this type
init(bitPattern: BitPattern)

var bitPattern: BitPattern { get }
}

extension Float16: ExpressibleByBitPattern {}
extension Float: ExpressibleByBitPattern {}
extension Double: ExpressibleByBitPattern {}

/// Provides endian-aware parsing for binary floating-point types.
///
/// This extension enables floating-point types (Float, Double, Float16) to be parsed
/// from binary data with explicit endianness control by converting through their
/// underlying bit pattern representation.
public extension BinaryFloatingPoint where Self: BitwiseCopyable & ExpressibleByBitPattern {
/// Initializes a floating-point value by parsing binary data with the specified endianness.
///
/// - Parameters:
/// - input: A mutable parser span containing the binary data to parse
/// - endianness: The byte order to use when parsing the underlying bit pattern
/// - Throws: `ParsingError` if parsing fails
init(parsing input: inout BinaryParsing.ParserSpan, endianness: BinaryParsing.Endianness) throws(ParsingError) {
let byteCount = MemoryLayout<Self>.size
let bitPattern = try BitPattern(parsing: &input, endianness: endianness, byteCount: byteCount)
self = Self(bitPattern: bitPattern)
}
}

extension Float: EndianParsable {}
extension Float16: EndianParsable {}
extension Double: EndianParsable {}
56 changes: 56 additions & 0 deletions Sources/BinaryParseKit/Extensions/IntegerExtensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// IntegerExtensions.swift
// BinaryParseKit
//
// Created by Larry Zeng on 7/16/25.
//
import BinaryParsing

extension UInt8: @retroactive ExpressibleByParsing {}
extension UInt8: EndianSizedParsable {}
extension UInt16: EndianSizedParsable, EndianParsable {}
extension UInt32: EndianSizedParsable, EndianParsable {}
extension UInt: EndianSizedParsable {}
extension UInt64: EndianSizedParsable, EndianParsable {}

extension Int8: EndianSizedParsable {}
extension Int16: EndianSizedParsable, EndianParsable {}
extension Int32: EndianSizedParsable, EndianParsable {}
extension Int: EndianSizedParsable {}
extension Int64: EndianSizedParsable, EndianParsable {}

extension UInt8: EndianParsable {
public init(
parsing input: inout BinaryParsing.ParserSpan,
endianness: BinaryParsing.Endianness,
) throws(ParsingError) {
try self.init(parsing: &input, endianness: endianness, byteCount: MemoryLayout<Self>.size)
}
}

extension UInt: EndianParsable {
public init(
parsing input: inout BinaryParsing.ParserSpan,
endianness: BinaryParsing.Endianness,
) throws(ParsingError) {
try self.init(parsing: &input, endianness: endianness, byteCount: MemoryLayout<Self>.size)
}
}

extension Int8: EndianParsable {
public init(
parsing input: inout BinaryParsing.ParserSpan,
endianness: BinaryParsing.Endianness,
) throws(ParsingError) {
try self.init(parsing: &input, endianness: endianness, byteCount: MemoryLayout<Self>.size)
}
}

extension Int: EndianParsable {
public init(
parsing input: inout BinaryParsing.ParserSpan,
endianness: BinaryParsing.Endianness,
) throws(ParsingError) {
try self.init(parsing: &input, endianness: endianness, byteCount: MemoryLayout<Self>.size)
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
//
// MatchableProtocols.swift
// Matchable+.swift
// BinaryParseKit
//
// Created by Larry Zeng on 11/11/25.
// Created by Larry Zeng on 11/28/25.
//

/// A protocol for types that can provide a sequence of bytes for matching purposes.
public protocol Matchable {
func bytesToMatch() -> [UInt8]
public extension Matchable where Self: RawRepresentable, Self.RawValue == UInt8 {
func bytesToMatch() -> [UInt8] {
[rawValue]
}
}

/// Default implementation of `bytesToMatch()` for ``Matchable`` where it's also a `RawRepresentable` and its `RawValue`
Expand Down
15 changes: 15 additions & 0 deletions Sources/BinaryParseKit/Printer/PrinterError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// PrinterError.swift
// BinaryParseKit
//
// Created by Larry Zeng on 11/28/25.
//

public enum PrinterError: Swift.Error {
/// Indicates that the construction of printer intel failed, with the underlying error provided.
case intelConstructionFailed(underlying: any Error)
/// Indicates that the provided type does not conform to ``Printable``.
case notPrintable(type: Any.Type)
/// Indicates that error is thrown by underling printer during printing process.
case printingError(underlying: any Error)
}
11 changes: 11 additions & 0 deletions Sources/BinaryParseKit/Protocols/Matchable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//
// Matchable.swift
// BinaryParseKit
//
// Created by Larry Zeng on 11/11/25.
//

/// A protocol for types that can provide a sequence of bytes for matching purposes.
public protocol Matchable {
func bytesToMatch() -> [UInt8]
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@
// Created by Larry Zeng on 11/14/25.
//

public enum PrinterError: Swift.Error {
/// Indicates that the construction of printer intel failed, with the underlying error provided.
case intelConstructionFailed(underlying: any Error)
/// Indicates that the provided type does not conform to ``Printable``.
case notPrintable(type: Any.Type)
/// Indicates that error is thrown by underling printer during printing process.
case printingError(underlying: any Error)
}

public protocol Printer {
/// The output type produced by the printer. For example, this could be `String`, `Data`, or any other type that
/// represents the printed output.
Expand Down
Loading