Skip to content

Commit 9019449

Browse files
author
Justin Kolb
committed
Missed some edits on the last commit.
1 parent a9f3467 commit 9019449

File tree

1 file changed

+55
-8
lines changed

1 file changed

+55
-8
lines changed

README.md

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
[Lilliput](http://en.wikipedia.org/wiki/Lilliput_and_Blefuscu) is a native Swift framework for working with binary data.
44

5-
## Design Decisions
5+
## Design decisions
66

77
* Does not support streaming data, it only works with data that can fit in memory.
88
* Allows types to have multiple encodings/decodings, this means that a UInt32 could be easily represented as little endian, big endian, or some custom compressed encoding.
99
* Does not support the idea of "native" endianness, when working with data where endianess matters you must always be specific.
10+
* Currently does not use the Foundation package so there are no APIs for integrating with it.
11+
* Makes use of the [swift-system](https://github.com/apple/swift-system) package for reading/writing files.
1012

11-
## Basic Usage
13+
## Basic usage
1214

1315
```swift
1416
import Lilliput
@@ -25,7 +27,7 @@ var reader = ByteSpanReader(buffer)
2527
let value = try reader.read(UInt32.LittleEndian.self)
2628
```
2729

28-
## Custom Byte Decodable/Encodable Types
30+
## Custom byte decodable/encodable types
2931

3032
```swift
3133
struct MyType {
@@ -53,10 +55,10 @@ extension MyType : ByteEncoder {
5355
}
5456
```
5557

56-
## Advanced Byte Decodable/Encodable Types
58+
## Mutiple ways to decode/encode the same type
5759

5860
```swift
59-
public extension MyType {
61+
extension MyType {
6062
@frozen enum Custom {}
6163
}
6264

@@ -65,6 +67,7 @@ extension MyType.Custom : ByteDecoder {
6567
// Values are loaded/stored in reverse order from the type definition and using big endian
6668
let value2 = try reader.read(Float64.BigEndian.self)
6769
let value1 = try reader.read(Int8.self)
70+
let _ = try reader.read(reader.readCount.offset(alignment: 4)) // Make sure next read occurs after three padding bytes
6871
let value0 = Int(try reader.read(UInt32.BigEndian.self))
6972

7073
return MyType(
@@ -80,12 +83,13 @@ extension MyType.Custom : ByteEncoder {
8083
// Values are loaded/stored in reverse order from the type definition and using big endian
8184
try writer.write(value.value2, as: Float64.BigEndian.self)
8285
try writer.write(value.value1)
86+
try writer.write((0, 0, 0), as: UInt8.Tuple3.self) // Add three padding bytes
8387
try writer.write(UInt32(value.value0), as: UInt32.BigEndian.self)
8488
}
8589
}
8690
```
8791

88-
## Reading Custom Types From Disk
92+
## Reading types from disk
8993

9094
```swift
9195
import Lilliput
@@ -101,7 +105,7 @@ let myType = try reader.read(MyType.self)
101105
let myTypeCustom = try reader.read(MyType.Custom.self)
102106
```
103107

104-
## Writing Custom Types To Disk
108+
## Writing types to disk
105109
```swift
106110
import Lilliput
107111
import SystemPackage
@@ -118,6 +122,49 @@ let file = try FileDescriptor.open(path, .writeOnly)
118122
file.writeAll(buffer)
119123
```
120124

125+
## Reading arrays of decodable types
126+
127+
```swift
128+
let count = Int(try reader.read(UInt32.LittleEndian.self))
129+
let arrayOfMyType = try reader.read(Element<MyType>.self, count: count)
130+
```
131+
132+
## Writing arrays of encodable types
133+
134+
```swift
135+
try writer.write(UInt32(arrayOfMyType.count), as: UInt32.LittleEndian.self)
136+
try writer.write(arrayOfMyType, as: Element<MyType>.self)
137+
```
138+
139+
## Custom array decoding/encoding
140+
141+
```swift
142+
// Note: This is not built in to the library due to decisions on how to encode the count value could differ wildly across use cases.
143+
@frozen struct MyArray<E> {}
144+
145+
extension MyArray : ByteDecoder where E : ByteDecoder {
146+
static func decode<R: Reader>(from reader: R) throws -> [E.Decodable] {
147+
let count = Int(try reader.read(UInt32.LittleEndian.self))
148+
return try reader.read(Element<E>.self, count: count)
149+
}
150+
}
151+
152+
extension MyArray : ByteEncoder where E: ByteEncoder {
153+
static func encode<W: ByteWriter>(_ value: [E.Encodable], to writer: inout W) throws {
154+
try writer.write(UInt32(value.count), as: UInt32.LittleEndian.self)
155+
156+
for element in value {
157+
try writer.write(element, as: E.self)
158+
}
159+
}
160+
}
161+
162+
// Usage (note that the count value is handled without extra work now)
163+
let arrayOfMyType = try reader.read(MyArray<MyType>.self)
164+
165+
try writer.write(arrayOfMyType, as: MyArray<MyType>.self)
166+
```
167+
121168
## Adding `Lilliput` as a Dependency
122169

123170
To use the `Lilliput` library in a SwiftPM project, add the following line to the dependencies in your `Package.swift` file:
@@ -132,7 +179,7 @@ Finally, include `"Lilliput"` as a dependency for your target:
132179
let package = Package(
133180
// name, platforms, products, etc.
134181
dependencies: [
135-
.package(url: "https://github.com/jkolb/c.git", from: "10.0.0"),
182+
.package(url: "https://github.com/jkolb/Lilliput.git", from: "10.0.0"),
136183
// other dependencies
137184
],
138185
targets: [

0 commit comments

Comments
 (0)