Skip to content

Commit

Permalink
Document IO/FS/read_line.read_chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
LunaAmora committed Aug 29, 2024
1 parent 66911df commit 99978d6
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/fun/builtins.bend
Original file line number Diff line number Diff line change
Expand Up @@ -307,19 +307,23 @@ def IO/FS/read_line.read_chunks(fd, chunks):
# Read line in 1kB chunks
chunk <- IO/done_on_err(IO/FS/read(fd, 1024))
match res = List/split_once(chunk, '\n'):
# Found a newline, backtract and join chunks
case Result/Ok:
(line, rest) = res.val
(length, *) = List/length(rest)
* <- IO/FS/seek(fd, to_i24(length) * -1, IO/FS/SEEK_CUR)
chunks = List/Cons(line, chunks)
bytes = List/flatten(chunks)
return wrap(bytes)
# Newline not found
case Result/Err:
line = res.val
(length, line) = List/length(line)
# If lenght is 0, the end of the file was reached, return as if it was a newline
if length == 0:
bytes = List/flatten(chunks)
return wrap(bytes)
# Otherwise, the line is still ongoing, read more chunks
else:
chunks = List/Cons(line, chunks)
return IO/FS/read_line.read_chunks(fd, chunks)
Expand Down

0 comments on commit 99978d6

Please sign in to comment.