Skip to content

Commit

Permalink
fixed bufio reader (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
thatstoasty authored Apr 2, 2024
1 parent 7102d0a commit e3e9323
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 17 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ All of these packages are partially implemented and do not support unicode chara

### Gojo

- `builtins`
- `Bytes` struct (backed by List[Int8])
- `bufio`
- `Reader`: Buffered `io.Reader`
- `Scanner`: Scanner interface to read data via tokens.
Expand Down
20 changes: 9 additions & 11 deletions gojo/bufio/bufio.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,14 @@ struct Reader[R: io.Reader](
"""Reads a new chunk into the buffer."""
# Slide existing data to beginning.
if self.read_pos > 0:
_ = copy(self.buf, self.buf[self.read_pos : self.write_pos])
var current_capacity = self.buf.capacity
self.buf = self.buf[self.read_pos : self.write_pos]
self.buf.reserve(current_capacity)
self.write_pos -= self.read_pos
self.read_pos = 0

# Compares to the length of the entire List[Byte] object, including 0 initialized positions.
# IE. var b = List[Byte](4096), then trying to write at b[4096] and onwards will fail.
# IE. var b = List[Byte](capacity=4096), then trying to write at b[4096] and onwards will fail.
if self.write_pos >= self.buf.capacity:
panic("bufio.Reader: tried to fill full buffer")

Expand All @@ -104,11 +106,8 @@ struct Reader[R: io.Reader](
while i > 0:
# TODO: Using temp until slicing can return a Reference
var temp = List[Byte](capacity=DEFAULT_BUF_SIZE)

# TODO: filehandle read is not maintaining reader position?
var result = self.reader.read(temp)
var bytes_read = copy(self.buf, temp, self.write_pos)

if bytes_read < 0:
panic(ERR_NEGATIVE_READ)

Expand Down Expand Up @@ -358,7 +357,7 @@ struct Reader[R: io.Reader](
"""
var err: Optional[WrappedError] = None
var s = 0 # search start index
var line: List[Byte] = List[Byte](DEFAULT_BUF_SIZE)
var line: List[Byte] = List[Byte](capacity=DEFAULT_BUF_SIZE)
while True:
# Search buffer.
var i = index_byte(self.buf[self.read_pos + s : self.write_pos], delim)
Expand Down Expand Up @@ -426,7 +425,6 @@ struct Reader[R: io.Reader](

self.read_pos -= 1
line = line[: len(line) - 1]

return line, True

if len(line) == 0:
Expand Down Expand Up @@ -472,7 +470,7 @@ struct Reader[R: io.Reader](
break

# Make a copy of the buffer.
var buf = frag
var buf = List[Byte](frag)
full_buffers.append(buf)
total_len += len(buf)

Expand All @@ -495,12 +493,12 @@ struct Reader[R: io.Reader](
The List[Byte] from the internal buffer.
"""
var full = List[List[Byte]]()
var frag = List[Byte](4096)
var frag = List[Byte](capacity=4096)
var n: Int = 0
var err = self.collect_fragments(delim, frag, full, n)

# Allocate new buffer to hold the full pieces and the fragment.
var buf = List[Byte](n)
var buf = List[Byte](capacity=n)
n = 0

# copy full pieces and fragment in.
Expand Down Expand Up @@ -632,7 +630,7 @@ struct Reader[R: io.Reader](
# # return b

# var r = Reader(reader ^)
# r.reset(List[Byte](max(size, MIN_READ_BUFFER_SIZE)), reader ^)
# r.reset(List[Byte](capacity=max(size, MIN_READ_BUFFER_SIZE)), reader ^)
# return r


Expand Down
2 changes: 1 addition & 1 deletion goodies/std.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ struct STDWriter(Copyable, io.Writer, io.StringWriter):
Returns:
The number of bytes written to the file descriptor.
"""
var buffer = List[Byte](io.BUFFER_SIZE)
var buffer = List[Byte](capacity=io.BUFFER_SIZE)
_ = reader.read(buffer)
return self.write(buffer)
2 changes: 1 addition & 1 deletion tests/test_builtins_bytes.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ from gojo.builtins.bytes import Byte, index_byte

# fn test_slice_out_of_bounds() raises:
# var test = MojoTest("Testing builtins.List[Byte] slice out of bounds")
# var bytes = List[Byte]("hello")
# var bytes = String("hello").as_bytes()
# var successful = True

# try:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_goodies_csv.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,5 @@ fn test_csv_writer() raises:

fn main() raises:
test_csv_reader()
# test_csv_reader_buffered_read()
test_csv_reader_buffered_read()
test_csv_writer()
2 changes: 1 addition & 1 deletion tests/test_strings_reader.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn test_read_and_unread_byte() raises:
var reader = new_reader("Hello, World!")

# Read the first byte from the reader.
var buffer = List[Byte](512)
var buffer = List[Byte](capacity=512)
var byte = reader.read_byte()
test.assert_equal(byte.value, 72)

Expand Down

0 comments on commit e3e9323

Please sign in to comment.