Skip to content

Commit c08c3d1

Browse files
committed
Add a new test for reading lines and fix two bugs
1 parent 4397c83 commit c08c3d1

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

Sources/StreamReader/Core/StreamReader.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,11 @@ public extension StreamReader {
203203
let (line, separator) = try readData(upTo: separators, matchingMode: .shortestDataWins, failIfNotFound: false, includeDelimiter: false)
204204
_ = try readData(size: separator.count, allowReadingLess: false) /* We must read the line separator! */
205205

206-
if !allowWindowsNewLines || !allowLegacyMacOSNewLines || separator == lf {
206+
if !allowWindowsNewLines || !allowLegacyMacOSNewLines || separator == lf || separator.isEmpty {
207207
/* If Windows new lines are not allowed, or the separator that matched
208-
 * was lf, or if legacy MacOS new lines are not allowed, we can
209-
 * directly return the data we found as there is no ambiguity possible. */
208+
 * was lf or empty (end of stream), or if legacy MacOS new lines are
209+
 * not allowed, we can directly return the data we found as there is no
210+
 * ambiguity possible. */
210211
return (line: line, newLineChars: separator)
211212
} else {
212213
/* Windows and legacy MacOS new lines are allowed, and the separator

Sources/StreamReader/Utils.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ internal func matchDelimiters(inData data: UnsafeRawBufferPointer, usingMatching
2222
/* Reversed enumeration in order to be able to remove an element from the
2323
 * unmatchedDelimiters array while still enumerating it and keeping valid
2424
 * indexes. Not 100% sure this is valid, but it seems to work… */
25-
for enumeratedDelimiter in unmatchedDelimiters.reversed().enumerated() {
25+
for enumeratedDelimiter in unmatchedDelimiters.enumerated().reversed() {
2626
if let range = data.firstRange(of: enumeratedDelimiter.element.element) {
2727
/* Found one of the delimiter. Let's see what we do with it... */
2828
let matchedLength = range.lowerBound + (includeDelimiter ? enumeratedDelimiter.element.element.count : 0)

Tests/StreamReaderTests/StreamReaderTests.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,22 @@ class StreamReaderTests : XCTestCase {
2626
super.tearDown()
2727
}
2828

29+
private func checkReadLine(reader r: StreamReader, expectedLine: String, expectedSeparator: String) throws {
30+
let ret = try r.readLine(allowUnixNewLines: true, allowLegacyMacOSNewLines: true, allowWindowsNewLines: true)
31+
XCTAssertEqual(ret.line, Data(expectedLine.utf8))
32+
XCTAssertEqual(ret.newLineChars, Data(expectedSeparator.utf8))
33+
}
34+
35+
func testDataStreamReadLine() throws {
36+
let r = DataReader(data: Data("Hello World,\r\nHow are you\ntoday?\rHope you’re\n\rokay!".utf8))
37+
try checkReadLine(reader: r, expectedLine: "Hello World,", expectedSeparator: "\r\n")
38+
try checkReadLine(reader: r, expectedLine: "How are you", expectedSeparator: "\n")
39+
try checkReadLine(reader: r, expectedLine: "today?", expectedSeparator: "\r")
40+
try checkReadLine(reader: r, expectedLine: "Hope you’re", expectedSeparator: "\n")
41+
try checkReadLine(reader: r, expectedLine: "", expectedSeparator: "\r")
42+
try checkReadLine(reader: r, expectedLine: "okay!", expectedSeparator: "")
43+
}
44+
2945
func testDataStreamBasicUpToDelimiterRead() throws {
3046
let delim = Data(hexEncoded: "45")!
3147
let d = Data(hexEncoded: "01 23 45 67 89")!

0 commit comments

Comments
 (0)