From 99978d68cbea3af56d7bb8d4a0282b22afd40a88 Mon Sep 17 00:00:00 2001 From: LunaAmora Date: Thu, 29 Aug 2024 11:05:42 -0300 Subject: [PATCH] Document IO/FS/read_line.read_chunks --- src/fun/builtins.bend | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/fun/builtins.bend b/src/fun/builtins.bend index 00072588..39536080 100644 --- a/src/fun/builtins.bend +++ b/src/fun/builtins.bend @@ -307,6 +307,7 @@ 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) @@ -314,12 +315,15 @@ def IO/FS/read_line.read_chunks(fd, chunks): 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)