summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2019-07-05 06:22:38 -0700
committerKaz Kylheku <kaz@kylheku.com>2019-07-05 06:22:38 -0700
commit5a3b0bba720a3edf72a6c85010727078ce985413 (patch)
treefbf92a354468e857abd80d90278752d14cf0b608
parente8472375ec4db62befaf87b1e85506234001e530 (diff)
downloadtxr-5a3b0bba720a3edf72a6c85010727078ce985413.tar.gz
txr-5a3b0bba720a3edf72a6c85010727078ce985413.tar.bz2
txr-5a3b0bba720a3edf72a6c85010727078ce985413.zip
New function: buf-put-buf.
* buf.c (buf_move_bytes): New static function. (buf_put_buf): New function. (buf_init): Register buf-put-buf intrinsic. * buf.h (buf_put_buf): Declared. * txr.1: Documented.
-rw-r--r--buf.c19
-rw-r--r--buf.h1
-rw-r--r--txr.129
3 files changed, 49 insertions, 0 deletions
diff --git a/buf.c b/buf.c
index 349f0870..e991b72a 100644
--- a/buf.c
+++ b/buf.c
@@ -362,6 +362,24 @@ val replace_buf(val buf, val items, val from, val to)
return buf;
}
+static void buf_move_bytes(val buf, val pos, mem_t *ptr, cnum size, val self)
+{
+ struct buf *b = buf_handle(buf, self);
+ cnum p = buf_check_index(b, pos, self);
+ val req_len = plus(num(p), num(size));
+ if (gt(req_len, b->len))
+ buf_do_set_len(buf, b, req_len, nil, self);
+ memmove(b->data + p, ptr, size);
+}
+
+val buf_put_buf(val dbuf, val sbuf, val pos)
+{
+ val self = lit("buf-put-buf");
+ struct buf *sb = buf_handle(sbuf, self);
+ buf_move_bytes(dbuf, pos, sb->data, c_num(sb->len), self);
+ return sbuf;
+}
+
static void buf_put_bytes(val buf, val pos, mem_t *ptr, cnum size, val self)
{
struct buf *b = buf_handle(buf, self);
@@ -1077,6 +1095,7 @@ void buf_init(void)
reg_fun(intern(lit("copy-buf"), user_package), func_n1(copy_buf));
reg_fun(intern(lit("sub-buf"), user_package), func_n3(sub_buf));
reg_fun(intern(lit("replace-buf"), user_package), func_n4(replace_buf));
+ reg_fun(intern(lit("buf-put-buf"), user_package), func_n3(buf_put_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 14302cf2..f6e6df1f 100644
--- a/buf.h
+++ b/buf.h
@@ -39,6 +39,7 @@ mem_t *buf_get(val buf, val self);
void buf_fill(val buf, mem_t *src, val self);
val sub_buf(val seq, val from, val to);
val replace_buf(val buf, val items, val from, val to);
+val buf_put_buf(val dbuf, val sbuf, val pos);
#if HAVE_I8
val buf_put_i8(val buf, val pos, val num);
diff --git a/txr.1 b/txr.1
index b6844b4a..f4e6d30a 100644
--- a/txr.1
+++ b/txr.1
@@ -23829,6 +23829,35 @@ The of the arguments, semantics and return value given for
apply to
.codn replace-buf .
+.coNP Function @ buf-put-buf
+.synb
+.mets (buf-put-buf < dst-buf < pos << src-buf )
+.syne
+.desc
+The
+.code buf-put-buf
+function stores a copy of buffer
+.meta src-buf
+into
+.meta dst-buf
+at the offset indicated by
+.metn pos .
+
+The source and destination memory regions may overlap.
+
+The return value is
+.metn src-buf .
+
+Note: the effect of a
+.code buf-put-buf
+operation may also be performed by a suitable call to
+.codn replace-buf ;
+however,
+.code buf-put-buf
+is less general: it doesn't insert or delete by replacing
+destination ranges with data of differing length,
+and requires a source operand of buffer type.
+
.coNP Function @ buf-put-i8
.synb
.mets (buf-put-i8 < buf < pos << val )