1
1
/*
2
- * DataReader.swift
3
- * StreamReader
4
- *
5
- * Created by François Lamboley on 20/08/2017.
6
- */
2
+ * DataReader.swift
3
+ * StreamReader
4
+ *
5
+ * Created by François Lamboley on 20/08/2017.
6
+ */
7
7
8
8
import Foundation
9
9
@@ -17,8 +17,8 @@ public final class DataReader : StreamReader {
17
17
public private( set) var currentReadPosition = 0
18
18
19
19
/**
20
- The whole data is in memory: the “underlying stream” (the `sourceData`) is
21
- _always_ read to `EOF`. */
20
+ The whole data is in memory: the “underlying stream” (the `sourceData`) is
21
+ _always_ read to `EOF`. */
22
22
public let streamHasReachedEOF = true
23
23
/** Always the size of the source data. */
24
24
public let currentStreamReadPosition : Int
@@ -34,7 +34,7 @@ public final class DataReader : StreamReader {
34
34
35
35
public func clearStreamHasReachedEOF( ) {
36
36
/* nop: the data reader has always reached EOF because all of the data is
37
- * in memory at all time. */
37
+ * in memory at all time. */
38
38
}
39
39
40
40
public func readData< T> ( size: Int , allowReadingLess: Bool , updateReadPosition: Bool , _ handler: ( UnsafeRawBufferPointer ) throws -> T ) throws -> T {
@@ -55,10 +55,10 @@ public final class DataReader : StreamReader {
55
55
return try sourceData. withUnsafeBytes { bytes in
56
56
let sizeToRead = sizeConstrainedToRemainingAllowedSizeToRead ( size)
57
57
/* Only possibility for bytes.baseAddress to be nil is if data is empty
58
- * which makes currentReadPosition and size to read being different
59
- * than 0 theoratically impossible.
60
- * This assert validates the flatMap we have in the raw buffer pointer
61
- * creation on next line. */
58
+ * which makes currentReadPosition and size to read being different
59
+ * than 0 theoratically impossible.
60
+ * This assert validates the flatMap we have in the raw buffer pointer
61
+ * creation on next line. */
62
62
assert ( bytes. baseAddress != nil || ( currentReadPosition == 0 && sizeToRead == 0 ) , " INTERNAL LOGIC ERROR " )
63
63
let ret = UnsafeRawBufferPointer ( start: bytes. baseAddress. flatMap { $0 + currentReadPosition } , count: sizeToRead)
64
64
if updateReadPosition { currentReadPosition += sizeToRead}
@@ -71,9 +71,9 @@ public final class DataReader : StreamReader {
71
71
72
72
if delimiters. count == 0 || ( !failIfNotFound && delimiters. count == 1 && delimiters [ 0 ] == Data ( ) ) {
73
73
/* When there are no delimiters or if there is only one delimiter which
74
- * is empty and we do not fail if we do not find the delimiter, we
75
- * simply read the stream to the end.
76
- * There may be more optimization possible, but we don’t care for now. */
74
+ * is empty and we do not fail if we do not find the delimiter, we
75
+ * simply read the stream to the end.
76
+ * There may be more optimization possible, but we don’t care for now. */
77
77
return try readData ( size: sizeToEnd, allowReadingLess: false , updateReadPosition: updateReadPosition, { ret in try handler ( ret, Data ( ) ) } )
78
78
}
79
79
@@ -88,8 +88,8 @@ public final class DataReader : StreamReader {
88
88
return try readData ( size: match. length, allowReadingLess: false , updateReadPosition: updateReadPosition, { ret in try handler ( ret, delimiters [ match. delimiterIdx] ) } )
89
89
}
90
90
/* matchDelimiters did not find an indisputable match. However, we have
91
- * fed all the data we have to it. We cannot find more matches! We
92
- * simply return the best match we got. */
91
+ * fed all the data we have to it. We cannot find more matches! We
92
+ * simply return the best match we got. */
93
93
if let match = findBestMatch ( fromMatchedDatas: matchedDatas, usingMatchingMode: matchingMode) {
94
94
return try readData ( size: match. length, allowReadingLess: false , updateReadPosition: updateReadPosition, { ret in try handler ( ret, delimiters [ match. delimiterIdx] ) } )
95
95
}
0 commit comments