fix Object.free (#982)

* 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
This commit is contained in:
Pierre Curto
2023-09-12 13:49:52 +02:00
committed by GitHub
parent 37bb16cca1
commit d61482dffc
11 changed files with 141 additions and 52 deletions

View File

@@ -30,9 +30,19 @@ fn ByteBuffer*! ByteBuffer.tinit(&self, usz max_read, usz initial_capacity = 16)
return self.init(max_read, initial_capacity, mem::temp())!;
}
/**
* @require buf.len > 0
* @require self.bytes.len == 0 "Buffer already initialized."
**/
fn ByteBuffer*! ByteBuffer.init_with_buffer(&self, char[] buf)
{
*self = { .stream.fns = &BYTEBUFFER_INTERFACE, .max_read = buf.len, .bytes = buf };
return self;
}
fn void! ByteBuffer.free(&self)
{
self.allocator.free(self.bytes)!;
if (self.allocator) self.allocator.free(self.bytes)!;
*self = {};
}
@@ -104,6 +114,23 @@ fn void! ByteBuffer.pushback_byte(&self)
self.has_last = false;
}
fn void! ByteBuffer.seek(&self, isz offset, Seek seek)
{
switch (seek)
{
case SET:
if (offset < 0 || offset > self.write_idx) return IoError.INVALID_POSITION?;
self.read_idx = offset;
case CURSOR:
if ((offset < 0 && self.read_idx < -offset) ||
(offset > 0 && self.read_idx + offset > self.write_idx)) return IoError.INVALID_POSITION?;
self.read_idx += offset;
case END:
if (offset < 0 || offset > self.write_idx) return IoError.INVALID_POSITION?;
self.read_idx = self.write_idx - offset;
}
}
fn usz! ByteBuffer.available(&self) @inline
{
return self.write_idx - self.read_idx;