Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix IO/FS/read_line when the line ends with a EOF #700

Merged
merged 5 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project does not currently adhere to a particular versioning scheme.
- Fix readback when hvm net has `a{n}` or `x{n}` vars. ([#659][gh-659])
- Fix imported constructors not being updated to Constructor expression. ([#674][gh-674])
- Fix parse error on parenthesized eraser. ([#675][gh-675])
- Fix IO/FS/read_line when the line ends with a EOF. ([#638][gh-638])

### Added

Expand Down Expand Up @@ -426,4 +427,5 @@ and this project does not currently adhere to a particular versioning scheme.
[gh-673]: https://github.com/HigherOrderCO/Bend/pull/673
[gh-674]: https://github.com/HigherOrderCO/Bend/issues/674
[gh-675]: https://github.com/HigherOrderCO/Bend/issues/675
[gh-638]: https://github.com/HigherOrderCO/Bend/issues/638
[Unreleased]: https://github.com/HigherOrderCO/Bend/compare/0.2.36...HEAD
13 changes: 11 additions & 2 deletions src/fun/builtins.bend
Original file line number Diff line number Diff line change
Expand Up @@ -307,17 +307,26 @@ 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, backtrack 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:
LunaAmora marked this conversation as resolved.
Show resolved Hide resolved
line = res.val
chunks = List/Cons(line, chunks)
return IO/FS/read_line.read_chunks(fd, chunks)
(length, line) = List/length(line)
# If length 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)

# IO/FS/write_file(path: String, bytes: (List u24)) -> (IO None)
# Writes a list of bytes to a file given by a path.
Expand Down
1 change: 1 addition & 0 deletions tests/golden_tests/io/eof.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
text
6 changes: 6 additions & 0 deletions tests/golden_tests/io/read_line_eof.bend
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def main:
with IO:
fd <- IO/done_on_err(IO/FS/open("tests/golden_tests/io/eof.txt", "r"))
bytes <- IO/FS/read_line(fd)
txt = String/decode_utf8(bytes)
return wrap(txt)
6 changes: 6 additions & 0 deletions tests/snapshots/io__read_line_eof.bend.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/io/read_line_eof.bend
---
Strict mode:
λa (a IO/Done/tag IO/MAGIC "text")
Loading