! Copyright (C) 2011 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
-USING: alien.data byte-arrays combinators combinators.smart
-endian fry hints kernel locals macros math math.ranges
-sequences sequences.generalizations ;
-QUALIFIED-WITH: alien.c-types c
+USING: alien.c-types alien.data byte-arrays combinators
+combinators.smart endian fry hints kernel locals macros math
+math.ranges sequences sequences.generalizations ;
RENAME: be> io.binary => slow-be>
RENAME: le> io.binary => slow-le>
RENAME: >be io.binary => >slow-be
PRIVATE>
: 2be> ( bytes -- x )
- big-endian [ c:ushort deref ] [ 2 n-be> ] if-endian ;
+ big-endian [ uint16_t deref ] [ 2 n-be> ] if-endian ;
: 4be> ( bytes -- x )
- big-endian [ c:uint deref ] [ 4 n-be> ] if-endian ;
+ big-endian [ uint32_t deref ] [ 4 n-be> ] if-endian ;
: 8be> ( bytes -- x )
- big-endian [ c:ulonglong deref ] [ 8 n-be> ] if-endian ;
+ big-endian [ uint64_t deref ] [ 8 n-be> ] if-endian ;
: be> ( bytes -- x )
dup length {
} case ;
: 2le> ( bytes -- x )
- little-endian [ c:ushort deref ] [ 2 n-le> ] if-endian ;
+ little-endian [ uint16_t deref ] [ 2 n-le> ] if-endian ;
: 4le> ( bytes -- x )
- little-endian [ c:uint deref ] [ 4 n-le> ] if-endian ;
+ little-endian [ uint32_t deref ] [ 4 n-le> ] if-endian ;
: 8le> ( bytes -- x )
- little-endian [ c:ulonglong deref ] [ 8 n-le> ] if-endian ;
+ little-endian [ uint64_t deref ] [ 8 n-le> ] if-endian ;
: le> ( bytes -- x )
dup length {
: >le ( x n -- bytes )
compute-native-endianness little-endian = [
{
- { 2 [ c:short <ref> ] }
- { 4 [ c:int <ref> ] }
- { 8 [ c:longlong <ref> ] }
+ { 2 [ int16_t <ref> ] }
+ { 4 [ int32_t <ref> ] }
+ { 8 [ int64_t <ref> ] }
[ >slow-le ]
} case
] [ >slow-le ] if ;
: >be ( x n -- bytes )
compute-native-endianness big-endian = [
{
- { 2 [ c:short <ref> ] }
- { 4 [ c:int <ref> ] }
- { 8 [ c:longlong <ref> ] }
+ { 2 [ int16_t <ref> ] }
+ { 4 [ int32_t <ref> ] }
+ { 8 [ int64_t <ref> ] }
[ >slow-be ]
} case
] [ >slow-be ] if ;