diff --git a/faststreams/input_stream.nim b/faststreams/input_stream.nim index 9ae29c1..e99da95 100644 --- a/faststreams/input_stream.nim +++ b/faststreams/input_stream.nim @@ -1,8 +1,8 @@ import - memfiles, options, stew/ranges/ptr_arith + memfiles, options, stew/[ptrops, ranges/ptr_arith] const - pageSize = 4096 + # pageSize = 4096 debugHelpers = false type @@ -44,7 +44,7 @@ proc openFile*(filename: string): ByteStreamVar = result.head = cast[ptr byte](memFile.mem) when debugHelpers: result.bufferStart = result.head - result.bufferEnd = result.head.shift memFile.size + result.bufferEnd = offset(result.head, memFile.size) result.bufferEndPos = memFile.size result.closeStream = proc = close memFile @@ -54,7 +54,7 @@ proc init*(T: type ByteStream, asyncReader = AsyncStreamReader(nil)): ByteStream = # TODO: the result should use `from mem` once it's supported result.head = unsafeAddr mem[0] - result.bufferEnd = result.head.shift mem.len + result.bufferEnd = offset(result.head, mem.len) result.bufferEndPos = mem.len result.reader = reader result.asyncReader = asyncReader @@ -82,7 +82,7 @@ proc syncRead(s: var ByteStream): bool = s.reader = nil return true else: - s.bufferEnd = s.bufferStart.shift bytesRead + s.bufferEnd = offset(s.bufferStart, bytesRead) s.bufferEndPos += bytesRead return false @@ -118,7 +118,7 @@ when debugHelpers: proc advance*(s: var ByteStream) = if s.head != s.bufferEnd: - s.head = s.head.shift 1 + s.head = offset(s.head, 1) elif s.reader != nil: discard s.syncRead() @@ -129,7 +129,7 @@ proc read*(s: var ByteStream): byte = proc checkReadAhead(s: ByteStreamVar, n: int): ptr byte = result = s.head doAssert distance(s.head, s.bufferEnd) >= n - s.head = s.head.shift(n) + s.head = offset(s.head, n) template readBytes*(s: ByteStreamVar, n: int): auto = makeOpenArray(checkReadAhead(s, n), n) @@ -139,9 +139,9 @@ proc next*(s: var ByteStream): Option[byte] = result = some s.read() proc bufferPos(s: ByteStream, pos: int): ptr byte = - let shiftFromEnd = pos - s.bufferEndPos - doAssert shiftFromEnd < 0 - result = s.bufferEnd.shift shiftFromEnd + let offsetFromEnd = pos - s.bufferEndPos + doAssert offsetFromEnd < 0 + result = offset(s.bufferEnd, offsetFromEnd) doAssert result >= s.bufferStart proc pos*(s: ByteStream): int {.inline.} = @@ -154,7 +154,7 @@ proc `[]`*(s: ByteStream, pos: int): byte {.inline.} = s.bufferPos(pos)[] proc rewind*(s: var ByteStream, delta: int) = - s.head = s.head.shift(-delta) + s.head = offset(s.head, -delta) doAssert s.head >= s.bufferStart proc rewindTo*(s: var ByteStream, pos: int) {.inline.} = diff --git a/faststreams/output_stream.nim b/faststreams/output_stream.nim index e9f3c26..4d386e0 100644 --- a/faststreams/output_stream.nim +++ b/faststreams/output_stream.nim @@ -1,5 +1,5 @@ import - deques, stew/ranges/ptr_arith, stew/strings + deques, stew/[ptrops, strings, ranges/ptr_arith] type OutputPage = object @@ -52,7 +52,7 @@ const proc createWriteCursor*[R, T](x: var array[R, T]): WriteCursor = let startAddr = cast[ptr byte](addr x[0]) - WriteCursor(head: startAddr, bufferEnd: shift(startAddr, sizeof x)) + WriteCursor(head: startAddr, bufferEnd: offset(startAddr, sizeof x)) template canExtendOutput(s: OutputStreamVar): bool = # Streams writing to pre-allocated existing buffers cannot be grown @@ -69,7 +69,7 @@ proc flipPage(s: OutputStreamVar) = s.cursor.head = cast[ptr byte](addr s.pages[s.pages.len - 1].buffer[0]) # TODO: There is an assumption here and elsewhere that `s.pages[^1]` has # a length equal to `s.pageSize` - s.cursor.bufferEnd = cast[ptr byte](shift(s.cursor.head, s.pageSize)) + s.cursor.bufferEnd = cast[ptr byte](offset(s.cursor.head, s.pageSize)) s.endPos += s.pageSize proc addPage(s: OutputStreamVar) = @@ -121,7 +121,7 @@ proc init*(T: type OutputStream, new result let buffer = cast[ptr byte](buffer) result.cursor.head = buffer - result.cursor.bufferEnd = buffer.shift(len) + result.cursor.bufferEnd = offset(buffer, len) result.cursor.stream = result result.endPos = len @@ -183,7 +183,7 @@ proc tryFlushing(s: OutputStreamVar) {.inline.} = func endAddr(s: string): ptr byte {.inline.} = let a = unsafeAddr s[0] - shift(cast[ptr byte](a), s.len) + offset(cast[ptr byte](a), s.len) template startAddr(s: string): ptr byte = cast[ptr byte](unsafeAddr s[0]) @@ -203,7 +203,7 @@ proc findNextPage(c: var WriteCursor): int = proc moveToPage(c: var WriteCursor, p: var OutputPage) = doAssert p.startOffset > 0 c.head = cast[ptr byte](unsafeAddr p.buffer[0]) - c.bufferEnd = shift(c.head, p.startOffset) + c.bufferEnd = offset(c.head, p.startOffset) p.startOffset = 0 proc moveToNextPage(c: var WriteCursor) = @@ -218,7 +218,7 @@ proc append*(c: var WriteCursor, b: byte) = c.stream.tryFlushing() c.head[] = b - c.head = shift(c.head, 1) + c.head = offset(c.head, 1) template append*(c: var WriteCursor, x: char) = bind append @@ -237,12 +237,12 @@ proc writeDataAsPages(s: OutputStreamVar, data: ptr byte, dataLen: int) = while dataLen > s.pageSize: s.vtable.writePage(s, makeOpenArray(data, s.pageSize)) - data = shift(data, s.pageSize) + data = offset(data, s.pageSize) dec dataLen, s.pageSize s.endPos += s.pageSize copyMem(s.cursor.head, data, dataLen) - s.cursor.head = shift(s.cursor.head, dataLen) + s.cursor.head = offset(s.cursor.head, dataLen) proc newStringFromBytes(input: ptr byte, inputLen: int): string = assert inputLen > 0 @@ -257,7 +257,7 @@ proc handleLongAppend*(c: var WriteCursor, bytes: openarray[byte]) = stream = c.stream template reduceInput(delta: int) = - inputPos = shift(inputPos, delta) + inputPos = offset(inputPos, delta) inputLen -= delta # Since the input is longer, we first make sure that the top-most @@ -283,14 +283,14 @@ proc handleLongAppend*(c: var WriteCursor, bytes: openarray[byte]) = if pageRunway < pageLen: doAssert inputLen <= pageRunway copyMem(pageStart, inputPos, inputLen) - c.head = shift(pageStart, inputLen) - c.bufferEnd = shift(pageStart, pageRunway) + c.head = offset(pageStart, inputLen) + c.bufferEnd = offset(pageStart, pageRunway) return else: if inputLen <= pageLen: copyMem(pageStart, inputPos, inputLen) - c.head = shift(pageStart, inputLen) - c.bufferEnd = shift(pageStart, pageLen) + c.head = offset(pageStart, inputLen) + c.bufferEnd = offset(pageStart, pageLen) return else: copyMem(pageStart, inputPos, pageLen) @@ -343,7 +343,7 @@ proc handleLongAppend*(c: var WriteCursor, bytes: openarray[byte]) = # This will also reset the cursor to the start of the page: stream.addPage copyMem(c.head, inputPos, inputLen) - c.head = shift(c.head, inputLen) + c.head = offset(c.head, inputLen) # We must correct endPos, because it must mark the end of the top-most # page. Since `addPage` advances the endPos as well and our remaining # input was written to the newly created page, our initial increase of @@ -361,7 +361,7 @@ proc append*(c: var WriteCursor, bytes: openarray[byte]) {.inline.} = if inputLen == 0: return if inputLen <= pageRemaining: copyMem(c.head, unsafeAddr bytes[0], inputLen) - c.head = shift(c.head, inputLen) + c.head = offset(c.head, inputLen) else: handleLongAppend(c, bytes) @@ -417,7 +417,7 @@ proc createCursor(s: OutputStreamVar, size: int): WriteCursor = inc s.extCursorsCount result = WriteCursor(head: s.cursor.head, - bufferEnd: s.cursor.head.shift(size), + bufferEnd: offset(s.cursor.head, size), stream: s) s.cursor.head = result.bufferEnd @@ -439,7 +439,7 @@ proc delayFixedSizeWrite*(s: OutputStreamVar, size: Natural): WriteCursor = startOffset: size) let (pageStart, pageEnd) = boundingAddrs s.pages[s.pages.len - 1].buffer - s.cursor.head = shift(pageStart, size) + s.cursor.head = offset(pageStart, size) s.cursor.bufferEnd = pageEnd s.endPos += (s.pageSize - size) @@ -465,7 +465,7 @@ proc writeAndFinalize*(c: var VarSizeWriteCursor, data: openarray[byte]) = let overestimatedBytes = cursor.runway - data.len doAssert overestimatedBytes >= 0 page.startOffset = overestimatedBytes - copyMem(cursor.head.shift(overestimatedBytes), unsafeAddr data[0], data.len) + copyMem(offset(cursor.head, overestimatedBytes), unsafeAddr data[0], data.len) finalize cursor return