From 5244b2f45dbe1973f8b1b04ff28248377c020ed8 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Tue, 22 Jul 2014 13:55:35 +0900 Subject: [PATCH] rewrite bytevector-copy in c --- piclib/prelude.scm | 29 ++---------------------- src/blob.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 27 deletions(-) diff --git a/piclib/prelude.scm b/piclib/prelude.scm index 6145b9dd..e2aa17d5 100644 --- a/piclib/prelude.scm +++ b/piclib/prelude.scm @@ -668,31 +668,6 @@ v) (bytevector-u8-set! v i (car l)))))) -(define (bytevector-copy! to at from . opts) - (let* ((start (if (pair? opts) (car opts) 0)) - (end (if (>= (length opts) 2) - (cadr opts) - (bytevector-length from))) - (vs #f)) - (if (eq? from to) - (begin - (set! vs (make-bytevector (- end start))) - (bytevector-copy! vs 0 from start end) - (bytevector-copy! to at vs)) - (do ((i at (+ i 1)) - (j start (+ j 1))) - ((= j end)) - (bytevector-u8-set! to i (bytevector-u8-ref from j)))))) - -(define (bytevector-copy v . opts) - (let ((start (if (pair? opts) (car opts) 0)) - (end (if (>= (length opts) 2) - (cadr opts) - (bytevector-length v)))) - (let ((res (make-bytevector (- end start)))) - (bytevector-copy! res 0 v start end) - res))) - (define (bytevector-append . vs) (define (bytevector-append-2-inv w v) (let ((res (make-bytevector (+ (bytevector-length v) (bytevector-length w))))) @@ -726,8 +701,8 @@ (list->bytevector (map char->integer (string->list s start end))))) (export bytevector - bytevector-copy! - bytevector-copy + bytevector->list + list->bytevector bytevector-append utf8->string string->utf8) diff --git a/src/blob.c b/src/blob.c index efb33ff2..8355e69b 100644 --- a/src/blob.c +++ b/src/blob.c @@ -100,6 +100,60 @@ pic_blob_bytevector_u8_set(pic_state *pic) return pic_none_value(); } +static pic_value +pic_blob_bytevector_copy_i(pic_state *pic) +{ + pic_blob *to, *from; + int n, at, start, end; + + n = pic_get_args(pic, "bib|ii", &to, &at, &from, &start, &end); + + switch (n) { + case 3: + start = 0; + case 4: + end = from->len; + } + + if (to == from && (start <= at && at < end)) { + /* copy in reversed order */ + at += end - start; + while (start < end) { + to->data[--at] = from->data[--end]; + } + return pic_none_value(); + } + + while (start < end) { + to->data[at++] = from->data[start++]; + } + + return pic_none_value(); +} + +static pic_value +pic_blob_bytevector_copy(pic_state *pic) +{ + pic_blob *from, *to; + int n, start, end, i = 0; + + n = pic_get_args(pic, "b|ii", &from, &start, &end); + + switch (n) { + case 1: + start = 0; + case 2: + end = from->len; + } + + to = pic_blob_new(pic, end - start); + while (start < end) { + to->data[i++] = from->data[start++]; + } + + return pic_obj_value(to); +} + void pic_init_blob(pic_state *pic) { @@ -108,4 +162,6 @@ pic_init_blob(pic_state *pic) pic_defun(pic, "bytevector-length", pic_blob_bytevector_length); pic_defun(pic, "bytevector-u8-ref", pic_blob_bytevector_u8_ref); pic_defun(pic, "bytevector-u8-set!", pic_blob_bytevector_u8_set); + pic_defun(pic, "bytevector-copy!", pic_blob_bytevector_copy_i); + pic_defun(pic, "bytevector-copy", pic_blob_bytevector_copy); }