From fdfcb8dde978d5c3f933cf2de4ea3cf9427586ce Mon Sep 17 00:00:00 2001 From: Dimitri Fontaine Date: Fri, 22 Nov 2013 17:18:09 +0100 Subject: [PATCH] Fix the byte-vector-to-bytea transform function (handle nil, better type declaration). --- src/transforms.lisp | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/transforms.lisp b/src/transforms.lisp index a1e6e8b..a5c59b0 100644 --- a/src/transforms.lisp +++ b/src/transforms.lisp @@ -151,21 +151,22 @@ http://www.postgresql.org/docs/9.3/interactive/datatype-binary.html Note that we choose here the bytea Hex Format." - (declare (type simple-array vector)) - (let ((hex-digits "0123456789abcdef") - (bytea (make-array (+ 2 (* 2 (length vector))) - :initial-element #\0 - :element-type 'standard-char))) + (declare (type (or null (simple-array (unsigned-byte 8) (*))) vector)) + (when vector + (let ((hex-digits "0123456789abcdef") + (bytea (make-array (+ 2 (* 2 (length vector))) + :initial-element #\0 + :element-type 'standard-char))) - ;; The entire string is preceded by the sequence \x (to distinguish it - ;; from the escape format). - (setf (aref bytea 0) #\\) - (setf (aref bytea 1) #\x) + ;; The entire string is preceded by the sequence \x (to distinguish it + ;; from the escape format). + (setf (aref bytea 0) #\\) + (setf (aref bytea 1) #\x) - (loop for pos from 2 by 2 - for byte across vector - do (let ((high (ldb (byte 4 4) byte)) - (low (ldb (byte 4 0) byte))) - (setf (aref bytea pos) (aref hex-digits high)) - (setf (aref bytea (+ pos 1)) (aref hex-digits low))) - finally (return bytea)))) + (loop for pos from 2 by 2 + for byte across vector + do (let ((high (ldb (byte 4 4) byte)) + (low (ldb (byte 4 0) byte))) + (setf (aref bytea pos) (aref hex-digits high)) + (setf (aref bytea (+ pos 1)) (aref hex-digits low))) + finally (return bytea)))))