mailto:srfi-66@srfi.schemers.org
.
See instructions
here to subscribe to the list. You can access previous messages via
the
archive of the mailing list.
This SRFI defines a set of procedures for creating, accessing, and manipulating uniform vectors of bytes.
A number of applications deal with sequences of bytes, most prominently interfaces to C and I/O. Vectors are typically too space-costly and too slow to work well in these circumstance. This justifies having a separate type for byte vectors.
This SRFI is related to SRFI 4 (Homogeneous numeric vector datatypes), which also provides vectors of bytes. However, the extension described here does not require any extensions to the syntax of the underlying Scheme system.
In Scheme systems implementing both SRFI 4 and this SRFI, it is strongly recommended that both use the same type for u8vector
and byte vectors as specified here.
Byte vectors are objects of a new type. This type may or may not be disjoint from the type of regular vectors.
As with vectors, the length of a byte vector is the number of elements it contains. This number is fixed. A valid index into a byte vector is an exact, non-negative integer. The first element of a byte vector has index 0, the last element has an index one less than the length of the vector.
(byte-vector?
obj)
Returns #t
if obj is a vector, otherwise returns #f
. Analogous to vector?
.
(make-byte-vector
k fill)
Returns a newly allocated byte vector of k elements. Each element is initialized to fill. Fill must be an exact integer in the range [0,255]. Analogous to make-vector
.
(byte-vector
byte ...)
Returns a newly allocated byte vector whose elements contain the given arguments, which must all be exact integers in the range [0,255]. Analogous to vector
.
(byte-vector-length
byte-vector)
Returns the number of elements in byte-vector as an exact integer. Analogous to vector-length
.
(byte-vector-ref
byte-vector k)
k must be a valid index of byte-vector. Byte-vector-ref
returns the contents of element k of byte-vector. Analogous to vector-ref
.
(byte-vector-set!
byte-vector k byte)
k must be a valid index of byte-vector. Byte-vector-set!
stores byte in element k of byte-vector. The number of return values and the return values are unspecified. However, the number of return values is such that it is accepted by a continuation created by begin
. Analogous to vector-ref
.
(byte-vector=?
byte-vector-1 byte-vector-2)
Returns #t if byte-vector-1 and byte-vector-2 are equal---that is, if they have the same length and equal elements at all valid indices.
(byte-vector-copy!
source source-start target target-start n)
Copies data from byte vector source to byte vector target. Source-start, target-start, and n must be non-negative exact integers that satisfy
0 <= source-start <= source-start + n <= (byte-vector-length source)
0 <= target-start <= target-start + n <= (byte-vector-length target)
This copies the bytes from source at indices [source-start, source-start + n) to consecutive indices in target starting at target-index.
The number of return values and the return values are unspecified. However, the number of return values is such that it is accepted by a continuation created by begin
. Analogous to vector-ref
.
(byte-vector-copy
byte-vector)
Returns a newly allocated copy of byte vector byte-vector.
This reference implementation makes use of SRFI 9 (Defining Record Types) and SRFI 23 (Error reporting mechanism) .
(define-record-type :byte-vector (really-make-byte-vector elements) byte-vector? (elements byte-vector-elements)) (define (ensure-byte thing) (if (not (and (integer? thing) (exact? thing) (>= thing 0) (<= thing 255))) (error "not a byte" thing))) (define (make-byte-vector k fill) (ensure-byte fill) (really-make-byte-vector (make-vector k fill))) (define (byte-vector . bytes) (for-each ensure-byte bytes) (really-make-byte-vector (apply vector bytes))) (define (byte-vector-length byte-vector) (vector-length (byte-vector-elements byte-vector))) (define (byte-vector-ref byte-vector k) (vector-ref (byte-vector-elements byte-vector) k)) (define (byte-vector-set! byte-vector k byte) (ensure-byte byte) (vector-set! (byte-vector-elements byte-vector) k byte)) (define (byte-vector-copy! source source-start target target-start count) (let loop ((index 0)) (if (< index count) (begin (byte-vector-set! target (+ target-start index) (byte-vector-ref source (+ source-start index))) (loop (+ 1 index)))))) (define (byte-vector-copy byte-vector) (let* ((size (byte-vector-length byte-vector)) (copy (make-byte-vector size 0))) (byte-vector-copy! byte-vector 0 copy 0 size) copy)) (define (byte-vector=? byte-vector-1 byte-vector-2) (let ((size (byte-vector-length byte-vector-1))) (and (= size (byte-vector-length byte-vector-2)) (let loop ((i 0)) (or (>= i size) (and (= (byte-vector-ref byte-vector-1) (byte-vector-ref byte-vector-2)) (loop (+ 1 i))))))))
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.