mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
* lib/std/collections: add HashMap.@each_entry()
* lib/std/json: fix Object.free() when object is a map
* lib/std/collections: fix allocator use in Object.{set,set_at,append}
* lib/std: add char.from_hex
* lib/std/collections: print arrays and objects compactly
* lib/std/io: fix Formatter.vprintf result
* lib/std/io/stream: rename module for ByteBuffer
* lib/std/io/stream: make Scanner a Stream reader
* lib/std/io: make std{in,err,out} return File* if no libc
45 lines
1.0 KiB
C
45 lines
1.0 KiB
C
module std::io;
|
|
|
|
struct LimitReader
|
|
{
|
|
inline Stream stream;
|
|
Stream* wrapped_stream;
|
|
usz limit;
|
|
}
|
|
|
|
/**
|
|
* @param [&inout] wrapped_stream "The stream to read from"
|
|
* @param limit "The max limit to read"
|
|
**/
|
|
fn LimitReader* LimitReader.init(&self, Stream* wrapped_stream, usz limit)
|
|
{
|
|
*self = { .stream.fns = &LIMITREADER_INTERFACE, .wrapped_stream = wrapped_stream, .limit = limit };
|
|
return self;
|
|
}
|
|
|
|
fn usz! LimitReader.read(&self, char[] bytes)
|
|
{
|
|
if (self.limit == 0) return IoError.EOF?;
|
|
usz m = min(bytes.len, self.limit);
|
|
usz n = self.wrapped_stream.read(bytes[:m])!;
|
|
self.limit -= n;
|
|
return n;
|
|
}
|
|
|
|
fn char! LimitReader.read_byte(&self)
|
|
{
|
|
if (self.limit == 0) return IoError.EOF?;
|
|
defer try self.limit--;
|
|
return self.wrapped_stream.read_byte();
|
|
}
|
|
|
|
fn usz LimitReader.available(&self) @inline
|
|
{
|
|
return self.limit;
|
|
}
|
|
|
|
const StreamInterface LIMITREADER_INTERFACE = {
|
|
.read_fn = (ReadStreamFn)&LimitReader.read,
|
|
.read_byte_fn = (ReadByteStreamFn)&LimitReader.read_byte,
|
|
.available_fn = fn(s) => ((LimitReader*)s).available(),
|
|
}; |