Updated stream API.

This commit is contained in:
Christoffer Lerno
2023-09-02 19:48:51 +02:00
committed by Christoffer Lerno
parent a6cff5c2a5
commit 9a6d83f526
44 changed files with 837 additions and 952 deletions

View File

@@ -7,7 +7,7 @@ fn void! simple_test()
{
ByteReader reader;
reader.init(`{ "b": 123, "c": [ { "d": 66 }, null, "hello", false, { "id": "xyz" } ] }`);
Object* o = json::parse(reader.as_stream())!;
Object* o = json::parse(&reader)!;
assert(o.get_int("b")! == 123);
assert(o.get("c").get_len()! == 5);
assert(o.get("c").get_at(0).get_int("d")! == 66);

View File

@@ -8,7 +8,7 @@ fn void! readbuffer_large()
src.init(DATA);
char[DATA.len] buf;
ReadBuffer reader_buf;
reader_buf.init(src.as_stream(), buf[..]);
reader_buf.init(&src, buf[..]);
char[DATA.len] bytes;
usz n = reader_buf.read(bytes[..])!;
@@ -24,12 +24,12 @@ fn void! readbuffer()
src.init(DATA);
char[3] buf;
ReadBuffer reader_buf;
reader_buf.init(src.as_stream(), buf[..]);
reader_buf.init(&src, buf[..]);
ByteWriter bw;
bw.tinit();
usz n = reader_buf.as_stream().copy_to(bw.as_stream())!;
usz n = reader_buf.copy_to(&bw)!;
assert(n == DATA.len, "got %d; want %d", n, DATA.len);
String got = bw.as_str();
@@ -42,7 +42,7 @@ fn void! writebuffer_large()
out.tinit();
char[16] buf;
WriteBuffer write_buf;
write_buf.init(out.as_stream(), buf[..]);
write_buf.init(&out, buf[..]);
usz n = write_buf.write(DATA)!;
@@ -59,9 +59,9 @@ fn void! writebuffer()
out.tinit();
char[3] buf;
WriteBuffer write_buf;
write_buf.init(out.as_stream(), buf[..]);
write_buf.init(&out, buf[..]);
usz n = br.as_stream().copy_to(write_buf.as_stream())!;
usz n = br.copy_to(&write_buf)!;
assert(n == DATA.len, "got %d; want %d", n, DATA.len);
String got = out.as_str();

View File

@@ -4,7 +4,7 @@ fn void! bytestream()
{
ByteReader r;
r.init("abc");
Stream s = r.as_stream();
Stream* s = &r;
assert(s.len()! == 3);
char[5] buffer;
assert('a' == s.read_byte()!);
@@ -13,7 +13,7 @@ fn void! bytestream()
assert((String)buffer[:len] == "abc");
ByteWriter w;
w.init();
Stream ws = w.as_stream();
Stream* ws = &w;
ws.write("helloworld")!;
assert(w.as_str() == "helloworld");
s.seek(0, SET)!;
@@ -28,7 +28,7 @@ fn void! bytewriter_buffer()
ByteWriter writer;
char[8] z;
writer.init_buffer(&z);
Stream s = writer.as_stream();
Stream* s = &writer;
s.write("hello")!!;
s.write_byte(0)!!;
String o = ((ZString)&z).as_str();
@@ -40,7 +40,7 @@ fn void! bytewriter_read_from()
{
char[] data = "Lorem ipsum dolor sit amet biam.";
TestReader r = { .bytes = data };
Stream s = r.as_stream();
Stream* s = &&r.as_stream();
ByteWriter bw;
bw.tinit();
@@ -58,9 +58,9 @@ struct TestReader
usz index;
}
fn Stream TestReader.as_stream(TestReader *r)
fn StreamWrapper TestReader.as_stream(TestReader *r)
{
return { .fns = &testReader_interface, .data = r };
return { .stream = { &testReader_interface }, .data = r };
}
fn usz! TestReader.read(TestReader *r, char[] bytes)
@@ -74,5 +74,5 @@ fn usz! TestReader.read(TestReader *r, char[] bytes)
}
StreamInterface testReader_interface = {
.read_fn = fn(s, char[] bytes) => ((TestReader*)s.data).read(bytes),
.read_fn = fn(s, char[] bytes) => ((TestReader*)((StreamWrapper*)s).data).read(bytes),
};

View File

@@ -4,14 +4,13 @@ fn void! test_writing()
{
DString foo;
foo.init();
Stream s = foo.as_stream();
Stream* s = DStringStream{}.init(&foo);
s.write("hello")!!;
s.write_byte('-')!!;
s.write("what?")!!;
s.write("what?-------------------------------------------------------")!!;
ByteReader r;
String test_str = "2134";
r.init(test_str);
s.read_from(r.as_stream())!;
s.read_from(r.init(test_str))!;
String o = foo.as_str();
assert(o == "hello-what?2134");
assert(o == "hello-what?-------------------------------------------------------2134");
}

View File

@@ -8,7 +8,7 @@ fn void! limitreader()
src.init(DATA);
const LIMIT = 5;
LimitReader lmr;
lmr.init(src.as_stream(), LIMIT);
lmr.init(&src, LIMIT);
char[DATA.len] bytes;
usz n = lmr.read(bytes[..])!;

View File

@@ -25,7 +25,7 @@ fn void! test_scanner()
br.init(tc.in);
Scanner sc;
char[4] buffer; // max match (2) + pattern length (2)
sc.init(br.as_stream(), buffer[..]);
sc.init(&br, buffer[..]);
Results results;
while LOOP: (true)