Skip to content

Commit

Permalink
Merge pull request #700 from HigherOrderCO/638-read-line-eof-bug
Browse files Browse the repository at this point in the history
Fix IO/FS/read_line when the line ends with a EOF
  • Loading branch information
kings177 authored Aug 29, 2024
2 parents 2446f3c + 17e3cfe commit 0945efc
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 2 deletions.
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:
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")

0 comments on commit 0945efc

Please sign in to comment.