Formatting.

This commit is contained in:
Kyle 2015-04-13 09:00:44 -07:00
parent 22c6356d2c
commit d5d4624a2d
1 changed files with 24 additions and 24 deletions

View File

@ -21,10 +21,10 @@
(defun uint-from-bytes (bin &key (endian :little)) (defun uint-from-bytes (bin &key (endian :little))
"Produce an unsigned integer from the binary array input." "Produce an unsigned integer from the binary array input."
(let ((bin (cond (let ((bin (cond
((eql endian :little) bin) ((eql endian :little) bin)
((eql endian :big) (reverse bin)) ((eql endian :big) (reverse bin))
(t (error "Invalid endian specification.")))) (t (error "Invalid endian specification."))))
(n 0)) (n 0))
(dotimes (i (length bin)) (dotimes (i (length bin))
(setf n (+ n (ash (aref bin i) (* i 8))))) (setf n (+ n (ash (aref bin i) (* i 8)))))
n)) n))
@ -35,7 +35,7 @@ provided."
(let ((bin (make-array size :element-type '(unsigned-byte 8)))) (let ((bin (make-array size :element-type '(unsigned-byte 8))))
(dotimes (i size) (dotimes (i size)
(setf (aref bin i) (setf (aref bin i)
(logand 255 (ash n (- 0 (* i 8)))))) (logand 255 (ash n (- 0 (* i 8))))))
(cond (cond
((eql endian :little) bin) ((eql endian :little) bin)
((eql endian :big) (nreverse bin)) ((eql endian :big) (nreverse bin))
@ -49,21 +49,21 @@ provided."
(defmacro define-unsigned-reader (const-name) (defmacro define-unsigned-reader (const-name)
(let ((docstring (let ((docstring
(format nil (format nil
"Read an unsigned ~A-bit integer from a stream." "Read an unsigned ~A-bit integer from a stream."
(subseq (format nil "~A" const-name) 1)))) (subseq (format nil "~A" const-name) 1))))
`(defun ,(intern (format nil "READ-~A" const-name)) `(defun ,(intern (format nil "READ-~A" const-name))
(stream &key (endian :little)) (stream &key (endian :little))
,docstring ,docstring
(read-uint stream ,const-name :endian endian)))) (read-uint stream ,const-name :endian endian))))
(defmacro define-unsigned-writer (const-name) (defmacro define-unsigned-writer (const-name)
(let ((docstring (let ((docstring
(format nil (format nil
"Write an unsigned ~A-bit integer to a stream." "Write an unsigned ~A-bit integer to a stream."
(subseq (format nil "~A" const-name) 1)))) (subseq (format nil "~A" const-name) 1))))
`(defun ,(intern (format nil "WRITE-~A" const-name)) `(defun ,(intern (format nil "WRITE-~A" const-name))
(stream n &key (endian :little)) (stream n &key (endian :little))
,docstring ,docstring
(write-uint stream n ,const-name :endian endian)))) (write-uint stream n ,const-name :endian endian))))
@ -84,7 +84,7 @@ provided."
(defun int-from-bytes (bin &key (endian :little)) (defun int-from-bytes (bin &key (endian :little))
"Produce a signed integer from the binary array input." "Produce a signed integer from the binary array input."
(twos-complement (uint-from-bytes bin :endian endian) (twos-complement (uint-from-bytes bin :endian endian)
(length bin))) (length bin)))
(defun int-to-bytes (n size &key (endian :little)) (defun int-to-bytes (n size &key (endian :little))
"Produce a binary array of size bytes from the provided signed "Produce a binary array of size bytes from the provided signed
@ -93,29 +93,29 @@ integer."
(defun read-int (stream size &key (endian :little)) (defun read-int (stream size &key (endian :little))
(int-from-bytes (octets stream size) (int-from-bytes (octets stream size)
:endian endian)) :endian endian))
(defun write-int (stream n size &key (endian :little)) (defun write-int (stream n size &key (endian :little))
(write-sequence (int-to-bytes n size :endian endian) (write-sequence (int-to-bytes n size :endian endian)
stream)) stream))
(defmacro define-signed-reader (const-name) (defmacro define-signed-reader (const-name)
(let ((docstring (let ((docstring
(format nil (format nil
"Read a signed ~A-bit integer from a stream." "Read a signed ~A-bit integer from a stream."
(subseq (format nil "~A" const-name) 1)))) (subseq (format nil "~A" const-name) 1))))
`(defun ,(intern (format nil "READ-~A" const-name)) `(defun ,(intern (format nil "READ-~A" const-name))
(stream &key (endian :little)) (stream &key (endian :little))
,docstring ,docstring
(read-int stream ,const-name :endian endian)))) (read-int stream ,const-name :endian endian))))
(defmacro define-signed-writer (const-name) (defmacro define-signed-writer (const-name)
(let ((docstring (let ((docstring
(format nil (format nil
"Write a signed ~A-bit integer to a stream." "Write a signed ~A-bit integer to a stream."
(subseq (format nil "~A" const-name) 1)))) (subseq (format nil "~A" const-name) 1))))
`(defun ,(intern (format nil "WRITE-~A" const-name)) `(defun ,(intern (format nil "WRITE-~A" const-name))
(stream n &key (endian :little)) (stream n &key (endian :little))
,docstring ,docstring
(write-int stream n ,const-name :endian endian)))) (write-int stream n ,const-name :endian endian))))