diff --git a/basis/pack/pack-tests.factor b/basis/pack/pack-tests.factor index 440d6a0369..b9137f77fc 100644 --- a/basis/pack/pack-tests.factor +++ b/basis/pack/pack-tests.factor @@ -36,6 +36,9 @@ IN: pack.tests "cstiq" [ pack-native ] keep unpack-native ] unit-test +{ B{ 1 2 3 4 5 0 0 0 } } [ { 1 2 3 4 5 } "4ci" pack-le ] unit-test +{ { 1 2 3 4 5 } } [ B{ 1 2 3 4 5 0 0 0 } "4ci" unpack-le ] unit-test + [ 9 ] [ "iic" packed-length ] unit-test [ "iii" read-packed-le ] must-infer [ "iii" read-packed-be ] must-infer diff --git a/basis/pack/pack.factor b/basis/pack/pack.factor index a330337c5e..5f15373790 100644 --- a/basis/pack/pack.factor +++ b/basis/pack/pack.factor @@ -1,11 +1,7 @@ ! Copyright (C) 2009 Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. -USING: alien alien.c-types arrays assocs byte-arrays io -io.binary io.streams.string kernel math math.parser namespaces -make parser quotations sequences strings vectors -words macros math.functions math.bitwise fry generalizations -combinators.smart io.streams.byte-array io.encodings.binary -math.vectors combinators multiline endian ; +USING: alien.c-types ascii assocs combinators combinators.smart +endian fry io kernel macros math math.vectors sequences strings ; IN: pack GENERIC: >n-byte-array ( obj n -- byte-array ) @@ -33,6 +29,13 @@ M: string >n-byte-array ( n string -- byte-array ) heap-size >n-byte-array ; ] bi* f swap ] if + ] { } map-as "" concat-as nip ; foldable + CONSTANT: pack-table H{ { CHAR: c s8>byte-array } @@ -90,6 +93,7 @@ CONSTANT: packed-length-table PRIVATE> MACRO: pack ( str -- quot ) + expand-pack-format [ pack-table at '[ _ execute ] ] { } map-as '[ [ [ _ spread ] input MACRO: unpack ( str -- quot ) + expand-pack-format [ [ ch>packed-length ] { } map-as start/end ] [ [ unpack-table at '[ @ ] ] { } map-as ] bi [ '[ [ _ _ ] dip @ ] ] 3map