From 48c6bba132baa31a418bd3f119ecfbef1818b301 Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Mon, 2 Jun 2014 16:30:12 -0700 Subject: [PATCH] io.binary.fast: adding fast alien version of >le and >be. --- basis/endian/endian.factor | 2 +- extra/io/binary/fast/fast.factor | 29 ++++++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/basis/endian/endian.factor b/basis/endian/endian.factor index 4dd5ecf407..4956d341d3 100644 --- a/basis/endian/endian.factor +++ b/basis/endian/endian.factor @@ -7,7 +7,7 @@ IN: endian SINGLETONS: big-endian little-endian ; : compute-native-endianness ( -- class ) - 1 int char deref 0 = big-endian little-endian ? ; + 1 int char deref 0 = big-endian little-endian ? ; foldable SYMBOL: native-endianness native-endianness [ compute-native-endianness ] initialize diff --git a/extra/io/binary/fast/fast.factor b/extra/io/binary/fast/fast.factor index 4773434608..445f3a9bb1 100644 --- a/extra/io/binary/fast/fast.factor +++ b/extra/io/binary/fast/fast.factor @@ -1,10 +1,13 @@ ! Copyright (C) 2011 Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. -USING: combinators combinators.smart fry kernel macros math -math.ranges sequences sequences.generalizations io.binary -locals ; +USING: alien.data combinators combinators.smart endian fry +io.binary kernel locals macros math math.ranges sequences +sequences.generalizations ; +QUALIFIED-WITH: alien.c-types c RENAME: be> io.binary => slow-be> RENAME: le> io.binary => slow-le> +RENAME: >be io.binary => >slow-be +RENAME: >le io.binary => >slow-le IN: io.binary.fast ERROR: bad-length bytes n ; @@ -57,3 +60,23 @@ MACRO: reassemble-le ( n -- quot ) le-range reassemble-bytes ; { 8 [ 8le> ] } [ drop slow-le> ] } case ; + +: >le ( x n -- bytes ) + compute-native-endianness little-endian = [ + { + { 2 [ c:short ] } + { 4 [ c:int ] } + { 8 [ c:longlong ] } + [ >slow-le ] + } case + ] [ >slow-le ] if ; + +: >be ( x n -- bytes ) + compute-native-endianness big-endian = [ + { + { 2 [ c:short ] } + { 4 [ c:int ] } + { 8 [ c:longlong ] } + [ >slow-be ] + } case + ] [ >slow-be ] if ;