io.binary.fast: easier to read using stdint types.

db4
John Benediktsson 2015-05-31 08:09:05 -07:00
parent 0eea157d90
commit ddb636c4c0
1 changed files with 15 additions and 16 deletions

View File

@ -1,9 +1,8 @@
! 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
@ -49,13 +48,13 @@ HINTS: n-le> { byte-array object } ;
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 {
@ -66,13 +65,13 @@ PRIVATE>
} 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 {
@ -85,9 +84,9 @@ PRIVATE>
: >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 ;
@ -95,9 +94,9 @@ PRIVATE>
: >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 ;