]> gitweb.factorcode.org Git - factor.git/blob - core/io/binary/binary.factor
build.sh: Don't copy the factor.image to factor.image.fresh if bootstrap fails
[factor.git] / core / io / binary / binary.factor
1 ! Copyright (C) 2003, 2007 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel math sequences ;
4 IN: io.binary
5
6 : le> ( seq -- x ) 0 [ 8 * shift + ] reduce-index ;
7
8 : be> ( seq -- x ) 0 [ [ 8 shift ] dip + ] reduce ;
9
10 : mask-byte ( x -- y ) 0xff bitand ; inline
11
12 : nth-byte ( x n -- b ) -8 * shift mask-byte ; inline
13
14 <PRIVATE
15
16 : map-bytes ( x seq -- byte-array )
17     [ nth-byte ] with B{ } map-as ; inline
18
19 PRIVATE>
20
21 : >le ( x n -- byte-array ) <iota> map-bytes ;
22
23 : >be ( x n -- byte-array ) <iota> <reversed> map-bytes ;
24
25 : d>w/w ( d -- w1 w2 )
26     [ 0xffffffff bitand ] [ -32 shift 0xffffffff bitand ] bi ;
27
28 : w>h/h ( w -- h1 h2 )
29     [ 0xffff bitand ] [ -16 shift 0xffff bitand ] bi ;
30
31 : h>b/b ( h -- b1 b2 )
32     [ mask-byte ] [ -8 shift mask-byte ] bi ;
33
34 <PRIVATE
35
36 : signed> ( x seq -- n )
37     length 8 * 2dup 1 - bit? [ 2^ - ] [ drop ] if ; inline
38
39 PRIVATE>
40
41 : signed-le> ( bytes -- x ) [ le> ] [ signed> ] bi ;
42
43 : signed-be> ( bytes -- x ) [ be> ] [ signed> ] bi ;