pack uses endian library now

db4
Doug Coleman 2009-02-06 23:37:38 -06:00
parent b073fe5eee
commit 1979fbc61a
1 changed files with 7 additions and 31 deletions

View File

@ -5,33 +5,9 @@ io.binary io.streams.string kernel math math.parser namespaces
make parser prettyprint quotations sequences strings vectors make parser prettyprint quotations sequences strings vectors
words macros math.functions math.bitwise fry generalizations words macros math.functions math.bitwise fry generalizations
combinators.smart io.streams.byte-array io.encodings.binary combinators.smart io.streams.byte-array io.encodings.binary
math.vectors combinators multiline ; math.vectors combinators multiline endian ;
IN: pack IN: pack
SYMBOL: big-endian
: big-endian? ( -- ? )
1 <int> *char zero? ;
<PRIVATE
: set-big-endian ( -- )
big-endian? big-endian set ; inline
PRIVATE>
: >signed ( x n -- y )
2dup neg 1+ shift 1 = [ 2^ - ] [ drop ] if ;
: >endian ( obj n -- str )
big-endian get [ >be ] [ >le ] if ; inline
: unsigned-endian> ( obj -- str )
big-endian get [ be> ] [ le> ] if ; inline
: signed-endian> ( obj n -- str )
[ unsigned-endian> ] dip >signed ;
GENERIC: >n-byte-array ( obj n -- byte-array ) GENERIC: >n-byte-array ( obj n -- byte-array )
M: integer >n-byte-array ( m n -- byte-array ) >endian ; M: integer >n-byte-array ( m n -- byte-array ) >endian ;
@ -124,13 +100,13 @@ PRIVATE>
[ ch>packed-length ] sigma ; [ ch>packed-length ] sigma ;
: pack-native ( seq str -- seq ) : pack-native ( seq str -- seq )
[ set-big-endian pack ] with-scope ; inline '[ _ _ pack ] with-native-endian ; inline
: pack-be ( seq str -- seq ) : pack-be ( seq str -- seq )
[ big-endian on pack ] with-scope ; inline '[ _ _ pack ] with-big-endian ; inline
: pack-le ( seq str -- seq ) : pack-le ( seq str -- seq )
[ big-endian off pack ] with-scope ; inline '[ _ _ pack ] with-little-endian ; inline
<PRIVATE <PRIVATE
@ -146,13 +122,13 @@ MACRO: unpack ( str -- quot )
PRIVATE> PRIVATE>
: unpack-native ( seq str -- seq ) : unpack-native ( seq str -- seq )
[ set-big-endian unpack ] with-scope ; inline '[ _ _ unpack ] with-native-endian ; inline
: unpack-be ( seq str -- seq ) : unpack-be ( seq str -- seq )
[ big-endian on unpack ] with-scope ; inline '[ _ _ unpack ] with-big-endian ; inline
: unpack-le ( seq str -- seq ) : unpack-le ( seq str -- seq )
[ big-endian off unpack ] with-scope ; inline '[ _ _ unpack ] with-little-endian ; inline
ERROR: packed-read-fail str bytes ; ERROR: packed-read-fail str bytes ;