A cursor models stream position without touching the filesystem.

cursor After reading a chunk, move the cursor to the next unread position.

Cursor Read

width
cursor_read.lua
Replay: real traced execution (multi-file project)
local width = 3
local source = "abcdef"
local cursor = 1
local chunk = string.sub(source, cursor, cursor + width - 1)
cursor = cursor + width

print("width=" .. width)
print("chunk=" .. chunk)
print("cursor=" .. cursor)
local width = 2
local source = "abcdef"
local cursor = 1
local chunk = string.sub(source, cursor, cursor + width - 1)
cursor = cursor + width

print("width=" .. width)
print("chunk=" .. chunk)
print("cursor=" .. cursor)
local width = 4
local source = "abcdef"
local cursor = 1
local chunk = string.sub(source, cursor, cursor + width - 1)
cursor = cursor + width

print("width=" .. width)
print("chunk=" .. chunk)
print("cursor=" .. cursor)
  1. width ← 3, source ← abcdef, cursor ← 1, chunk ← abc

    1local width→ 3 = 3 --@width=2, 42local source→ abcdef = "abcdef"3local cursor→ 1 = 14local chunk→ abc = string.sub(sourceabcdef, cursor1, cursor + width3 - 1)5cursor→ 4 = cursor + width367print("width=" .. width3)8print("chunk=" .. chunkabc)9print("cursor=" .. cursor4)
    outputwidth=3
    chunk=abc
    cursor=4
  1. width ← 2, source ← abcdef, cursor ← 1, chunk ← ab

    1local width→ 2 = 22local source→ abcdef = "abcdef"3local cursor→ 1 = 14local chunk→ ab = string.sub(sourceabcdef, cursor1, cursor + width2 - 1)5cursor→ 3 = cursor + width267print("width=" .. width2)8print("chunk=" .. chunkab)9print("cursor=" .. cursor3)
    outputwidth=2
    chunk=ab
    cursor=3
  1. width ← 4, source ← abcdef, cursor ← 1, chunk ← abcd

    1local width→ 4 = 42local source→ abcdef = "abcdef"3local cursor→ 1 = 14local chunk→ abcd = string.sub(sourceabcdef, cursor1, cursor + width4 - 1)5cursor→ 5 = cursor + width467print("width=" .. width4)8print("chunk=" .. chunkabcd)9print("cursor=" .. cursor5)
    outputwidth=4
    chunk=abcd
    cursor=5