From 7352cf8bcf23f5aa813b2ae1a3b13f5184d84483 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Mon, 18 Nov 2013 01:40:57 +0900 Subject: [PATCH] add missing bytevector functions --- README.md | 2 +- piclib/built-in.scm | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e48acb46..5050b641 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ | 6.6 Characters | yes | | | 6.7 Strings | yes | `substring` is not provided | | 6.8 Vectors | yes | | -| 6.9 Bytevectors | incomplete | TODO: string<->utf8 conversion, etc | +| 6.9 Bytevectors | incomplete | TODO: string<->utf8 conversion | | 6.10 Control features | incomplete | TODO: `string-map`, `vector-map`, ...etc | | 6.11 Exceptions | yes | TODO: native error handling | | 6.12 Environments and evaluation | N/A | | diff --git a/piclib/built-in.scm b/piclib/built-in.scm index cb60bc09..ca27563d 100644 --- a/piclib/built-in.scm +++ b/piclib/built-in.scm @@ -519,3 +519,30 @@ ((< i len) 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)))) + (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 (bytevector-length v)))) + (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))))) + (bytevector-copy! res 0 v) + (bytevector-copy! res (bytevector-length v) w) + res)) + (fold bytevector-append-2-inv #() vs))