From 61ec270c837bdcef179d99f6515ee68fe617ff73 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Sun, 4 Nov 2018 22:10:15 -0800 Subject: buffers: implement copy-buf. * buf.c (copy_buf): New function. (buf_init): Register copy-buf intrinsic. * buf.h (copy_buf): Declared. * lib.c (copy): Handle BUF via copy_buf. * txr.1: Documented. --- buf.c | 9 +++++++++ buf.h | 1 + lib.c | 2 ++ txr.1 | 22 ++++++++++++++++++++++ 4 files changed, 34 insertions(+) diff --git a/buf.c b/buf.c index 40e81607..959b7880 100644 --- a/buf.c +++ b/buf.c @@ -137,6 +137,14 @@ static struct buf *buf_handle(val buf, val ctx) ctx, buf, nao); } +val copy_buf(val buf) +{ + struct buf *b = buf_handle(buf, lit("copy-buf")); + return if3(b->size, + make_duplicate_buf(b->len, b->data), + make_borrowed_buf(b->len, b->data)); +} + static void buf_grow(struct buf *b, val init_val, val self) { cnum len = c_num(b->len); @@ -935,6 +943,7 @@ void buf_init(void) reg_fun(intern(lit("buf-trim"), user_package), func_n1(buf_trim)); reg_fun(intern(lit("buf-set-length"), user_package), func_n3o(buf_set_length, 2)); reg_fun(intern(lit("length-buf"), user_package), func_n1(length_buf)); + reg_fun(intern(lit("copy-buf"), user_package), func_n1(copy_buf)); #if HAVE_I8 reg_fun(intern(lit("buf-put-i8"), user_package), func_n3(buf_put_i8)); diff --git a/buf.h b/buf.h index cf394ff0..0086f6f0 100644 --- a/buf.h +++ b/buf.h @@ -30,6 +30,7 @@ val bufp(val object); val make_borrowed_buf(val len, mem_t *data); val init_borrowed_buf(obj_t *buf, val len, mem_t *data); val make_duplicate_buf(val len, mem_t *data); +val copy_buf(val buf); val buf_trim(val buf); val buf_set_length(val obj, val len, val init_val); val length_buf(val buf); diff --git a/lib.c b/lib.c index 4070ce4e..f86576b9 100644 --- a/lib.c +++ b/lib.c @@ -9939,6 +9939,8 @@ val copy(val seq) return copy_str(seq); case VEC: return copy_vec(seq); + case BUF: + return copy_buf(seq); case COBJ: if (seq->co.cls == hash_s) return copy_hash(seq); diff --git a/txr.1 b/txr.1 index 1aa8f26f..f1c71f4c 100644 --- a/txr.1 +++ b/txr.1 @@ -26177,6 +26177,12 @@ is a structure, it returns .cble If .meta object +is a buffer, it returns +.cblk +.meti (copy-buf << object ). +.cble +If +.meta object is a .codn carray , it returns @@ -56985,6 +56991,22 @@ If is specified, its value must be in the range 0 to 255. It defaults to zero. +.coNP Function @ copy-buf +.synb +.mets (copy-buf << buf ) +.syne +.desc +The +.code copy-buf +function returns a duplicate of +.metn buf : +an object distinct from +.meta buf +which has the same length and contents, and compares +.code equal +to +.metn buf . + .coNP Function @ buf-put-i8 .synb .mets (buf-put-i8 < buf < pos << val ) -- cgit v1.2.3