Skip to content

Commit

Permalink
use span for bytes reader and buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
thatstoasty committed Jun 15, 2024
1 parent 3d8bb56 commit 8da061b
Show file tree
Hide file tree
Showing 13 changed files with 251 additions and 218 deletions.
24 changes: 12 additions & 12 deletions gojo/bufio/bufio.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ struct Reader[R: io.Reader](Sized, io.Reader, io.ByteReader, io.ByteScanner):
_ = buf.write(Span(frag))
return str(buf), err

fn write_to[W: io.Writer](inout self, inout writer: W) -> (Int64, Error):
fn write_to[W: io.Writer](inout self, inout writer: W) -> (Int, Error):
"""Writes the internal buffer to the writer. This may make multiple calls to the [Reader.Read] method of the underlying [Reader].
If the underlying reader supports the [Reader.WriteTo] method,
this calls the underlying [Reader.WriteTo] without buffering.
Expand All @@ -544,7 +544,7 @@ struct Reader[R: io.Reader](Sized, io.Reader, io.ByteReader, io.ByteScanner):
self.last_byte = -1
self.last_rune_size = -1

var bytes_written: Int64
var bytes_written: Int
var err: Error
bytes_written, err = self.write_buf(writer)
if err:
Expand All @@ -556,7 +556,7 @@ struct Reader[R: io.Reader](Sized, io.Reader, io.ByteReader, io.ByteScanner):

while self.read_pos < self.write_pos:
# self.read_pos < self.write_pos => buffer is not empty
var bw: Int64
var bw: Int
var err: Error
bw, err = self.write_buf(writer)
bytes_written += bw
Expand All @@ -565,7 +565,7 @@ struct Reader[R: io.Reader](Sized, io.Reader, io.ByteReader, io.ByteScanner):

return bytes_written, Error()

fn write_buf[W: io.Writer](inout self, inout writer: W) -> (Int64, Error):
fn write_buf[W: io.Writer](inout self, inout writer: W) -> (Int, Error):
"""Writes the [Reader]'s buffer to the writer.
Args:
Expand All @@ -576,21 +576,21 @@ struct Reader[R: io.Reader](Sized, io.Reader, io.ByteReader, io.ByteScanner):
"""
# Nothing to write
if self.read_pos == self.write_pos:
return Int64(0), Error()
return Int(0), Error()

# Write the buffer to the writer, if we hit EOF it's fine. That's not a failure condition.
var bytes_written: Int
var err: Error
var buf_to_write = self.buf[self.read_pos : self.write_pos]
bytes_written, err = writer.write(Span(buf_to_write))
if err:
return Int64(bytes_written), err
return Int(bytes_written), err

if bytes_written < 0:
panic(ERR_NEGATIVE_WRITE)

self.read_pos += bytes_written
return Int64(bytes_written), Error()
return Int(bytes_written), Error()


# fn new_reader_size[R: io.Reader](owned reader: R, size: Int) -> Reader[R]:
Expand Down Expand Up @@ -835,7 +835,7 @@ struct Writer[W: io.Writer](Sized, io.Writer, io.ByteWriter, io.StringWriter, io
"""
return self.write(src.as_bytes_slice())

fn read_from[R: io.Reader](inout self, inout reader: R) -> (Int64, Error):
fn read_from[R: io.Reader](inout self, inout reader: R) -> (Int, Error):
"""Implements [io.ReaderFrom]. If the underlying writer
supports the read_from method, this calls the underlying read_from.
If there is buffered data and an underlying read_from, this fills
Expand All @@ -848,10 +848,10 @@ struct Writer[W: io.Writer](Sized, io.Writer, io.ByteWriter, io.StringWriter, io
The number of bytes read.
"""
if self.err:
return Int64(0), self.err
return Int(0), self.err

var bytes_read: Int = 0
var total_bytes_written: Int64 = 0
var total_bytes_written: Int = 0
var err = Error()
while True:
if self.available() == 0:
Expand All @@ -874,10 +874,10 @@ struct Writer[W: io.Writer](Sized, io.Writer, io.ByteWriter, io.StringWriter, io
nr += 1

if nr == MAX_CONSECUTIVE_EMPTY_READS:
return Int64(bytes_read), Error(io.ERR_NO_PROGRESS)
return Int(bytes_read), Error(io.ERR_NO_PROGRESS)

self.bytes_written += bytes_read
total_bytes_written += Int64(bytes_read)
total_bytes_written += Int(bytes_read)
if err:
break

Expand Down
24 changes: 24 additions & 0 deletions gojo/builtins/attributes.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,30 @@ fn copy[T: CollectionElement](inout target: List[T], source: List[T], start: Int
return count


fn copy[T: CollectionElement](inout target: List[T], source: Span[T], start: Int = 0) -> Int:
"""Copies the contents of source into target at the same index. Returns the number of bytes copied.
Added a start parameter to specify the index to start copying into.
Args:
target: The buffer to copy into.
source: The buffer to copy from.
start: The index to start copying into.
Returns:
The number of bytes copied.
"""
var count = 0

for i in range(len(source)):
if i + start > len(target):
target[i + start] = source[i]
else:
target.append(source[i])
count += 1

return count


fn copy(
inout target: List[UInt8],
source: DTypePointer[DType.uint8],
Expand Down
17 changes: 17 additions & 0 deletions gojo/builtins/bytes.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,23 @@ fn index_byte(bytes: DTypePointer[DType.uint8], size: Int, delim: Byte) -> Int:
return -1


fn index_byte(bytes: Span[UInt8], delim: Byte) -> Int:
"""Return the index of the first occurrence of the byte delim.
Args:
bytes: The Span to search.
delim: The byte to search for.
Returns:
The index of the first occurrence of the byte delim.
"""
for i in range(len(bytes)):
if bytes[i] == delim:
return i

return -1


fn to_string(bytes: List[Byte]) -> String:
"""Makes a deepcopy of the List[Byte] supplied and converts it to a string. If it's not null terminated, it will append a null byte.
Expand Down
Loading

0 comments on commit 8da061b

Please sign in to comment.