fixes #9026 adds a readLineImpl to streams so that FileStreams can be fast (#12234)

This commit is contained in:
Ray Imber 2019-09-22 23:25:26 -07:00 • committed by Andreas Rumpf
commit 6e681b546f

View file

@ -122,6 +122,9 @@ type
readDataStrImpl*: proc (s: Stream, buffer: var string, slice: Slice[int]): int readDataStrImpl*: proc (s: Stream, buffer: var string, slice: Slice[int]): int
{.nimcall, raises: [Defect, IOError, OSError], tags: [ReadIOEffect], gcsafe.} {.nimcall, raises: [Defect, IOError, OSError], tags: [ReadIOEffect], gcsafe.}
readLineImpl*: proc(s: Stream, line: var TaintedString): bool
{.nimcall, raises: [Defect, IOError, OSError], tags: [ReadIOEffect], gcsafe.}
readDataImpl*: proc (s: Stream, buffer: pointer, bufLen: int): int readDataImpl*: proc (s: Stream, buffer: pointer, bufLen: int): int
{.nimcall, raises: [Defect, IOError, OSError], tags: [ReadIOEffect], gcsafe.} {.nimcall, raises: [Defect, IOError, OSError], tags: [ReadIOEffect], gcsafe.}
peekDataImpl*: proc (s: Stream, buffer: pointer, bufLen: int): int peekDataImpl*: proc (s: Stream, buffer: pointer, bufLen: int): int
@ -877,6 +880,10 @@ proc readLine*(s: Stream, line: var TaintedString): bool =
doAssert line == "" doAssert line == ""
strm.close() strm.close()
if s.readLineImpl != nil:
result = s.readLineImpl(s, line)
else:
# fallback
line.string.setLen(0) line.string.setLen(0)
while true: while true:
var c = readChar(s) var c = readChar(s)
@ -1131,6 +1138,9 @@ when not defined(js):
if writeBuffer(FileStream(s).f, buffer, bufLen) != bufLen: if writeBuffer(FileStream(s).f, buffer, bufLen) != bufLen:
raise newEIO("cannot write to stream") raise newEIO("cannot write to stream")
proc fsReadLine(s: Stream, line: var TaintedString): bool =
result = readLine(FileStream(s).f, line)
proc newFileStream*(f: File): owned FileStream = proc newFileStream*(f: File): owned FileStream =
## Creates a new stream from the file `f`. ## Creates a new stream from the file `f`.
## ##
@ -1169,6 +1179,7 @@ when not defined(js):
result.getPositionImpl = fsGetPosition result.getPositionImpl = fsGetPosition
result.readDataStrImpl = fsReadDataStr result.readDataStrImpl = fsReadDataStr
result.readDataImpl = fsReadData result.readDataImpl = fsReadData
result.readLineImpl = fsReadLine
result.peekDataImpl = fsPeekData result.peekDataImpl = fsPeekData
result.writeDataImpl = fsWriteData result.writeDataImpl = fsWriteData
result.flushImpl = fsFlush result.flushImpl = fsFlush