|
@@ -26,13 +26,9 @@ public struct Bytes: RawRepresentable {
|
|
|
|
|
|
|
|
public var rawValue: [UInt8]
|
|
public var rawValue: [UInt8]
|
|
|
|
|
|
|
|
- public var lenght: Int {
|
|
|
|
|
- rawValue.count
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ public var lenght: Int { rawValue.count }
|
|
|
|
|
|
|
|
- public var data: Data {
|
|
|
|
|
- Data(rawValue)
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ public var data: Data { Data(rawValue) }
|
|
|
|
|
|
|
|
public var hexa: String {
|
|
public var hexa: String {
|
|
|
rawValue.map { .init(format: "%02x", $0) }.joined()
|
|
rawValue.map { .init(format: "%02x", $0) }.joined()
|
|
@@ -106,12 +102,12 @@ public struct Bytes: RawRepresentable {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public subscript (index: Int) -> UInt8 {
|
|
public subscript (index: Int) -> UInt8 {
|
|
|
- get { return rawValue[index] }
|
|
|
|
|
|
|
+ get { rawValue[index] }
|
|
|
set { rawValue[index] = newValue }
|
|
set { rawValue[index] = newValue }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public subscript (range: CountableRange<Int>) -> Bytes {
|
|
public subscript (range: CountableRange<Int>) -> Bytes {
|
|
|
- return Bytes(slice: rawValue[range])
|
|
|
|
|
|
|
+ Bytes(slice: rawValue[range])
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public mutating func append(_ byte: UInt8) {
|
|
public mutating func append(_ byte: UInt8) {
|
|
@@ -128,20 +124,20 @@ public struct Bytes: RawRepresentable {
|
|
|
|
|
|
|
|
@discardableResult
|
|
@discardableResult
|
|
|
public func withBytes<T>(_ body: ([UInt8]) -> T) -> T {
|
|
public func withBytes<T>(_ body: ([UInt8]) -> T) -> T {
|
|
|
- return body(rawValue)
|
|
|
|
|
|
|
+ body(rawValue)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@discardableResult
|
|
@discardableResult
|
|
|
public mutating func withMutableBytes<T>(_ body: (inout [UInt8]) -> T) -> T {
|
|
public mutating func withMutableBytes<T>(_ body: (inout [UInt8]) -> T) -> T {
|
|
|
- return body(&rawValue)
|
|
|
|
|
|
|
+ body(&rawValue)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public func map<T>() -> [T] where T: BinaryInteger {
|
|
public func map<T>() -> [T] where T: BinaryInteger {
|
|
|
- return map { T($0) }
|
|
|
|
|
|
|
+ map { T($0) }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public static func + (lhs: Bytes, rhs: Bytes) -> Bytes {
|
|
public static func + (lhs: Bytes, rhs: Bytes) -> Bytes {
|
|
|
- return Bytes(rawValue: lhs.rawValue + rhs.rawValue)
|
|
|
|
|
|
|
+ Bytes(rawValue: lhs.rawValue + rhs.rawValue)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public static func += (lhs: inout Bytes, rhs: Bytes) {
|
|
public static func += (lhs: inout Bytes, rhs: Bytes) {
|
|
@@ -149,7 +145,7 @@ public struct Bytes: RawRepresentable {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public static func + (lhs: Bytes, rhs: UInt8) -> Bytes {
|
|
public static func + (lhs: Bytes, rhs: UInt8) -> Bytes {
|
|
|
- return Bytes(rawValue: lhs.rawValue + [rhs])
|
|
|
|
|
|
|
+ Bytes(rawValue: lhs.rawValue + [rhs])
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public static func += (lhs: inout Bytes, rhs: UInt8) {
|
|
public static func += (lhs: inout Bytes, rhs: UInt8) {
|
|
@@ -174,7 +170,7 @@ extension Bytes: Sequence {
|
|
|
/// - Complexity: O(1), except if the sequence also conforms to `Collection`.
|
|
/// - Complexity: O(1), except if the sequence also conforms to `Collection`.
|
|
|
/// In this case, see the documentation of `Collection.underestimatedCount`.
|
|
/// In this case, see the documentation of `Collection.underestimatedCount`.
|
|
|
public var underestimatedCount: Int {
|
|
public var underestimatedCount: Int {
|
|
|
- return rawValue.underestimatedCount
|
|
|
|
|
|
|
+ rawValue.underestimatedCount
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// Call `body(p)`, where `p` is a pointer to the collection's
|
|
/// Call `body(p)`, where `p` is a pointer to the collection's
|
|
@@ -188,7 +184,7 @@ extension Bytes: Sequence {
|
|
|
/// can be generated by advancing the pointer by the distance to the
|
|
/// can be generated by advancing the pointer by the distance to the
|
|
|
/// slice's `startIndex`.
|
|
/// slice's `startIndex`.
|
|
|
public func withContiguousStorageIfAvailable<R>(_ body: (UnsafeBufferPointer<UInt8>) throws -> R) rethrows -> R? {
|
|
public func withContiguousStorageIfAvailable<R>(_ body: (UnsafeBufferPointer<UInt8>) throws -> R) rethrows -> R? {
|
|
|
- return try rawValue.withContiguousStorageIfAvailable(body)
|
|
|
|
|
|
|
+ try rawValue.withContiguousStorageIfAvailable(body)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|
|
@@ -209,27 +205,21 @@ extension Bytes: ContiguousBytes {
|
|
|
/// - warning: The buffer argument to the body should not be stored or used
|
|
/// - warning: The buffer argument to the body should not be stored or used
|
|
|
/// outside of the lifetime of the call to the closure.
|
|
/// outside of the lifetime of the call to the closure.
|
|
|
public func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
|
|
public func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
|
|
|
- return try rawValue.withUnsafeBytes(body)
|
|
|
|
|
|
|
+ try rawValue.withUnsafeBytes(body)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public mutating func withUnsafeMutableBytes<T>(_ body: (UnsafeMutableRawBufferPointer) throws -> T) rethrows -> T {
|
|
public mutating func withUnsafeMutableBytes<T>(_ body: (UnsafeMutableRawBufferPointer) throws -> T) rethrows -> T {
|
|
|
- return try rawValue.withUnsafeMutableBytes(body)
|
|
|
|
|
|
|
+ try rawValue.withUnsafeMutableBytes(body)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
extension Bytes: DataProtocol {
|
|
extension Bytes: DataProtocol {
|
|
|
|
|
|
|
|
- public var regions: CollectionOfOne<[UInt8]> {
|
|
|
|
|
- rawValue.regions
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ public var regions: CollectionOfOne<[UInt8]> { rawValue.regions }
|
|
|
|
|
|
|
|
- public var startIndex: Int {
|
|
|
|
|
- rawValue.startIndex
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ public var startIndex: Int { rawValue.startIndex }
|
|
|
|
|
|
|
|
- public var endIndex: Int {
|
|
|
|
|
- rawValue.endIndex
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ public var endIndex: Int { rawValue.endIndex }
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|