diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2025-05-27 22:11:00 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2025-05-27 22:11:00 -0700 |
commit | a983fbea2c1b39a71555452de87c392458b9b8b0 (patch) | |
tree | ef909bf9ec56d8a4d73a5905c59cd2ce8a6298ef | |
parent | b782a2e60148aae8166330c7b75924dc3926b001 (diff) | |
download | txr-a983fbea2c1b39a71555452de87c392458b9b8b0.tar.gz txr-a983fbea2c1b39a71555452de87c392458b9b8b0.tar.bz2 txr-a983fbea2c1b39a71555452de87c392458b9b8b0.zip |
buf: buf stream don't need to type check the buf obj.
* buf.c (us_buf_handle): New inline function.
(buf_strm_get_byte_callback, buf_strm_unget_byte,
buf_strm_seek, buf_strm_get_error): Use
us_buf_handle instead of buf_handle. A few functions
don't need the self variable because of this.
(make_buf_stream): Call buf_handle here to force
a type check. After this, we are sure the buf_strm
holds a buffer.
-rw-r--r-- | buf.c | 17 |
1 files changed, 12 insertions, 5 deletions
@@ -260,6 +260,11 @@ static struct buf *buf_handle(val buf, val ctx) ctx, buf, nao); } +INLINE struct buf *us_buf_handle(val buf) +{ + return &buf->b; +} + val copy_buf(val buf) { struct buf *b = buf_handle(buf, lit("copy-buf")); @@ -1131,7 +1136,7 @@ static int buf_strm_get_byte_callback(mem_t *ctx) { val self = lit("get-byte"); struct buf_strm *s = coerce(struct buf_strm *, ctx); - struct buf *b = buf_handle(s->buf, self); + struct buf *b = us_buf_handle(s->buf); cnum p = buf_check_index(b, s->pos, self); s->pos = num(p + 1); return (p >= c_num(b->len, self)) ? EOF : b->data[p]; @@ -1177,7 +1182,7 @@ static val buf_strm_unget_byte(val stream, int byte) { val self = lit("unget-byte"); struct buf_strm *s = coerce(struct buf_strm *, stream->co.handle); - struct buf *b = buf_handle(s->buf, self); + struct buf *b = us_buf_handle(s->buf); cnum p = c_num(s->pos, self); if (p <= 0) { @@ -1195,7 +1200,7 @@ static val buf_strm_seek(val stream, val offset, enum strm_whence whence) { val self = lit("seek-stream"); struct buf_strm *s = coerce(struct buf_strm *, stream->co.handle); - struct buf *b = buf_handle(s->buf, self); + struct buf *b = us_buf_handle(s->buf); val npos; switch (whence) { @@ -1257,9 +1262,8 @@ static val buf_strm_set_prop(val stream, val ind, val prop) static val buf_strm_get_error(val stream) { - val self = lit("get-error"); struct buf_strm *s = coerce(struct buf_strm *, stream->co.handle); - struct buf *b = buf_handle(s->buf, self); + struct buf *b = us_buf_handle(s->buf); return ge(s->pos, b->len); } @@ -1309,10 +1313,13 @@ static struct buf_strm *buf_strm(val stream, val self) val make_buf_stream(val buf_opt) { + val self = lit("make-buf-stream"); val stream; val buf = default_arg(buf_opt, make_buf(zero, zero, num_fast(64))); struct buf_strm *s = coerce(struct buf_strm *, chk_malloc(sizeof *s)); + (void) buf_handle(buf, self); + strm_base_init(&s->a); utf8_decoder_init(&s->ud); s->buf = nil; |