srfi-66.scm 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. ; Copyright (c) 1993-2007 by Richard Kelsey and Jonathan Rees. See file COPYING.
  2. ; SRFI 66: Octet vectors
  3. (define (make-u8vector k fill)
  4. (make-byte-vector k fill))
  5. (define u8vector? byte-vector?)
  6. (define (list->u8vector octets)
  7. (let* ((size (length octets))
  8. (v (make-byte-vector size 0)))
  9. (do ((i 0 (+ 1 i))
  10. (l octets (cdr l)))
  11. ((>= i size))
  12. (byte-vector-set! v i (car l)))
  13. v))
  14. (define (u8vector->list octets)
  15. (let loop ((n (byte-vector-length octets)) (r '()))
  16. (if (zero? n)
  17. r
  18. (loop (- n 1) (cons (byte-vector-ref octets (- n 1)) r)))))
  19. (define u8vector byte-vector)
  20. (define u8vector-length byte-vector-length)
  21. (define u8vector-ref byte-vector-ref)
  22. (define u8vector-set! byte-vector-set!)
  23. (define (u8vector-copy! source source-start target target-start count)
  24. (copy-bytes! source source-start target target-start count))
  25. (define (u8vector-copy u8vector)
  26. (let* ((size (byte-vector-length u8vector))
  27. (copy (make-byte-vector size 0)))
  28. (u8vector-copy! u8vector 0 copy 0 size)
  29. copy))
  30. (define (u8vector=? u8vector-1 u8vector-2)
  31. (let ((size (byte-vector-length u8vector-1)))
  32. (and (= size (byte-vector-length u8vector-2))
  33. (let loop ((i 0))
  34. (or (>= i size)
  35. (and (= (byte-vector-ref u8vector-1 i)
  36. (byte-vector-ref u8vector-2 i))
  37. (loop (+ 1 i))))))))
  38. (define (u8vector-compare u8vector-1 u8vector-2)
  39. (let ((length-1 (u8vector-length u8vector-1))
  40. (length-2 (u8vector-length u8vector-2)))
  41. (cond
  42. ((< length-1 length-2) -1)
  43. ((> length-1 length-2) 1)
  44. (else
  45. (let loop ((i 0))
  46. (if (= i length-1)
  47. 0
  48. (let ((elt-1 (u8vector-ref u8vector-1 i))
  49. (elt-2 (u8vector-ref u8vector-2 i)))
  50. (cond ((< elt-1 elt-2) -1)
  51. ((> elt-1 elt-2) 1)
  52. (else (loop (+ i 1)))))))))))