summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2018-11-04 22:10:15 -0800
committerKaz Kylheku <kaz@kylheku.com>2018-11-04 22:10:15 -0800
commitce6ada3b340607177bec7cbfa2b5eaa47ebb07a4 (patch)
tree11808552fdee0ba62ba5555f2a1a6df280872aba
parent44c8e70f810ad52b0f26f06147d5613ed1271900 (diff)
downloadtxr-ce6ada3b340607177bec7cbfa2b5eaa47ebb07a4.tar.gz
txr-ce6ada3b340607177bec7cbfa2b5eaa47ebb07a4.tar.bz2
txr-ce6ada3b340607177bec7cbfa2b5eaa47ebb07a4.zip
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.
-rw-r--r--buf.c9
-rw-r--r--buf.h1
-rw-r--r--lib.c2
-rw-r--r--txr.122
4 files changed, 34 insertions, 0 deletions
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 )