SRFI 66: Byte Vectors

Title

Byte Vectors

Authors

Michael Sperber

Status

This SRFI is currently in ``draft'' status. To see an explanation of each status that a SRFI can hold, see here. It will remain in draft status until 2005/05/09, or as amended. To provide input on this SRFI, please 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.

Abstract

This SRFI defines a set of procedures for creating, accesing, and manipulating uniform vectors of bytes.

Rationale

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.

Specification

The interface described here is identical to that offered by the byte-vectors structure in Scheme 48.

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 size k elements. Each element is initialized to fill. Fill must be an exact integer in the range [0,255]. Analogous to make-byte-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 byte-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 is unspecified. However, the number of return values is such that it is accepted by a continuation created by begin. Analogous to vector-ref.

Reference Implementation

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))

References

Copyright

Copyright (C) Michael Sperber (2005). All Rights Reserved.

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.


Editor: David Van Horn