mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
io::read_fully now handles unbounded streams properly
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
module std::io;
|
||||
import std::math;
|
||||
import std::core::env;
|
||||
|
||||
|
||||
interface InStream
|
||||
{
|
||||
@@ -36,7 +34,7 @@ fn usz? available(InStream s)
|
||||
s.seek(curr, Seek.SET)!;
|
||||
return len - curr;
|
||||
}
|
||||
return 0;
|
||||
return ^io::UNSUPPORTED_OPERATION;
|
||||
}
|
||||
|
||||
macro bool @is_instream(#expr) @const
|
||||
@@ -90,21 +88,31 @@ macro usz? read_all(stream, char[] buffer)
|
||||
}
|
||||
|
||||
<*
|
||||
This function will read to the end of the stream.
|
||||
|
||||
@require @is_instream(stream)
|
||||
*>
|
||||
macro char[]? read_fully(Allocator allocator, stream)
|
||||
{
|
||||
usz len = available(stream)!;
|
||||
char* data = allocator::malloc_try(allocator, len)!;
|
||||
defer catch allocator::free(allocator, data);
|
||||
usz read = 0;
|
||||
while (read < len)
|
||||
// Efficient path if it is possible to pre-allocate
|
||||
if (try len = available(stream))
|
||||
{
|
||||
read += stream.read(data[read:len - read])!;
|
||||
char* data = allocator::malloc_try(allocator, len)!;
|
||||
defer catch allocator::free(allocator, data);
|
||||
usz read = 0;
|
||||
while (read < len)
|
||||
{
|
||||
read += stream.read(data[read:len - read])!;
|
||||
}
|
||||
return data[:len];
|
||||
}
|
||||
return data[:len];
|
||||
ByteWriter writer;
|
||||
writer.init(allocator);
|
||||
copy_to(stream, &writer)!;
|
||||
return writer.array_view();
|
||||
}
|
||||
|
||||
|
||||
<*
|
||||
@require @is_outstream(stream)
|
||||
*>
|
||||
|
||||
@@ -43,6 +43,11 @@ fn void? ByteWriter.destroy(&self) @dynamic
|
||||
*self = { };
|
||||
}
|
||||
|
||||
fn char[] ByteWriter.array_view(self) @inline
|
||||
{
|
||||
return self.bytes[:self.index];
|
||||
}
|
||||
|
||||
fn String ByteWriter.str_view(&self) @inline
|
||||
{
|
||||
return (String)self.bytes[:self.index];
|
||||
@@ -91,7 +96,7 @@ fn usz? ByteWriter.read_from(&self, InStream reader) @dynamic
|
||||
}
|
||||
if (self.bytes.len == 0)
|
||||
{
|
||||
self.ensure_capacity(16)!;
|
||||
self.ensure_capacity(256)!;
|
||||
}
|
||||
while (true)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user