Skip to content

Commit cfe9c41

Browse files
committed
Merge branch 'dev.v3' into develop
2 parents a653c20 + c08c3d1 commit cfe9c41

19 files changed

+1068
-716
lines changed

Package.resolved

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ import PackageDescription
44

55

66
let package = Package(
7-
name: "SimpleStream",
7+
name: "stream-reader",
88
products: [
9-
.library(name: "SimpleStream", targets: ["SimpleStream"])
9+
.library(name: "StreamReader", targets: ["StreamReader"])
10+
],
11+
dependencies: [
12+
.package(url: "https://github.com/apple/swift-system.git", from: "0.0.1")
1013
],
1114
targets: [
12-
.target(name: "SimpleStream", dependencies: []),
13-
.testTarget(name: "SimpleStreamTests", dependencies: ["SimpleStream"])
15+
.target(name: "StreamReader", dependencies: [.product(name: "SystemPackage", package: "swift-system")]),
16+
.testTarget(name: "StreamReaderTests", dependencies: ["StreamReader"])
1417
]
1518
)

Readme.adoc

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,45 @@
1-
= SimpleStream
1+
= Stream Reader
22
François Lamboley <fload@me.com>
33

4-
A simple stream protocol for Swift named `SimpleStream`, with two concrete implementations:
5-
`SimpleGenericReadStream` and `SimpleDataStream`.
4+
A simple stream reader protocol for Swift named `StreamReader`, with two
5+
concrete implementations: `GenericStreamReader` and `DataReader`.
66

7-
The `SimpleGenericReadStream` can read from any `GenericReadStream`, which the
8-
`FileHandle` and `IntputStream` classes have been made conform to.
7+
The `GenericStreamReader` can read from any `GenericReadStream`, which the
8+
`FileDescriptor` (from https://github.com/apple/swift-system[SystemPackage]),
9+
`FileHandle` and `IntputStream` classes have been made to conform to.
910

1011
== Usage Examples
1112
=== Reading a Stream to the End
1213
[source,swift]
1314
----
14-
let ds = SimpleDataStream(data: d)
15-
let rd = try ds.readDataToEnd()
16-
assert(rd == d)
15+
let data = ...
16+
let reader = DataReader(data: data)
17+
let readData = try reader.readDataToEnd()
18+
assert(readData == data)
1719
----
1820

19-
### Reading a Stream Until a Delimitor Is Found
21+
=== Reading a Stream Until a Delimitor Is Found
2022
[source,swift]
2123
----
22-
let s = SimpleInputStream(stream: yourInputStream, bufferSize: 1024, bufferSizeIncrement: 512, streamReadSizeLimit: nil)
23-
/* Read the stream until a newline (whether macOS or Classic Mac OS) is found,
24-
* does not include the newline in the result. */
25-
let line = try s.readData(upTo: [Data("\n".utf8), Data("\r".utf8)], matchingMode: .anyMatchWins, includeDelimiter: false).data
26-
/* Note: If the file has Windows new lines, this will add an empty new line
27-
* after each line (the separator for Windows being "\r\n"). */
24+
let inputStream = ...
25+
let reader = InputStreamReader(stream: inputStream, bufferSize: 1024, bufferSizeIncrement: 512)
26+
/* Read the stream until a newline (whether macOS or Classic MacOS) is found, and returns the data without the newline. */
27+
let (line, separator) = try reader.readData(upTo: [Data("\n".utf8), Data("\r".utf8)], matchingMode: .anyMatchWins, failIfNotFound: false, includeDelimiter: false)
28+
_ = try reader.readData(size: separator.count) /* We must read the line separator before next read, probably :) */
29+
----
30+
31+
Note: In the example above, if the file has Windows new lines, this will add an
32+
empty new line after each line (the separator for Windows being `\r\n`).
33+
34+
Stream Reader has also a dedicated method to read a line in a stream:
35+
[source,swift]
36+
----
37+
/* Does not return the line separator, _but_ set stream position after the line separator. */
38+
let lineData = try reader.readLine(allowUnixNewLines: true, allowLegacyMacOSNewLines: true, allowWindowsNewLines: true).line
2839
----
2940

3041
== TODO
31-
Make the reads async! This will change a lot of things, but the core of the project should stay the same.
42+
Make the reads async? This would change a lot of things, but the core of the
43+
project should stay the same.
44+
45+
Or maybe just be thread-safe, idk.

Sources/SimpleStream/Core/SimpleReadStream.swift

Lines changed: 0 additions & 196 deletions
This file was deleted.

Sources/SimpleStream/Core/SimpleStreamError.swift

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)