summaryrefslogtreecommitdiff
path: root/ebus-racket/3rdparty/bzlib/parseq/util.ss
blob: 822ce3c7303e0022bab6049483691641b7a818fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#lang scheme/base
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; PARSEQ.PLT 
;; A Parser Combinator library.
;; 
;; Bonzai Lab, LLC.  All rights reserved.
;; 
;; Licensed under LGPL.
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; util.ss - an utility module... the code might be moved out of here... 
;; yc 12/31/2009 - first version 

(require mzlib/etc
         )

;; the-number
;; makes a function that returns a particular number no matter what 
;; args are passed in
(define (the-number n)
  (lambda args n))

;; bits->byte
;; convert a list of bits into its corresponding byte (or integer...) 
;; note the byte can be greater than 255
(define (bits->byte bits)
  (define (->i bit)
    (case bit
      ((0 #f) 0)
      ((1 #t) 1)))
  (apply + 
         (map (lambda (bit exponent)
                (* (->i bit) (expt 2 exponent)))
              bits
              (reverse (build-list (length bits) identity)))))

;; byte->bits 
;; the reverse of converting byte to bits... 
(define (byte->bits b) 
  (define (helper q acc) 
    (cond ((= 0 q) acc)
          (else
           (let-values (((q r)
                         (quotient/remainder q 2)))
             (helper q (cons r acc))))))
  (helper b '()))

;; string-bytes/utf-8-length 
;; return the bytes length for a string (instead of character length) 
(define (string-bytes/utf-8-length s)
  (bytes-length (string->bytes/utf-8 s)))

(provide (all-defined-out))