compression.zlib: Just deal with raw bytes, no compressed object tuple.

db4
Doug Coleman 2015-05-02 10:41:52 -07:00
parent 0371fa3137
commit 501f990971
3 changed files with 11 additions and 49 deletions

View File

@ -3,36 +3,19 @@
USING: help.markup help.syntax kernel math strings byte-arrays ; USING: help.markup help.syntax kernel math strings byte-arrays ;
IN: compression.zlib IN: compression.zlib
HELP: <compressed>
{ $values
{ "data" byte-array } { "length" integer }
{ "compressed" compressed }
}
{ $description "Creates a new " { $link compressed } ", using the provided bytes as the compressed data and the provided length as the uncompressed length. You should almost always use " { $link compress } ", rather than using this constructor directly." } ;
HELP: compress HELP: compress
{ $values { $values
{ "byte-array" byte-array } { "byte-array" byte-array }
{ "compressed" compressed } { "byte-array'" byte-array }
} }
{ $description "Compresses the given byte-array, returning a Factor object holding the compressed data." } ; { $description "Returns a byte-array of compressed bytes." } ;
HELP: compressed
{ $class-description "The class used to hold compressed data." } ;
HELP: compressed-size
{ $values
{ "byte-array" byte-array }
{ "n" integer }
}
{ $description "Returns the maximum number of bytes required to store the compressed version of a byte array." } ;
HELP: uncompress HELP: uncompress
{ $values { $values
{ "compressed" compressed }
{ "byte-array" byte-array } { "byte-array" byte-array }
{ "byte-array'" byte-array }
} }
{ $description "Uncompresses a compressed object, returning a byte-array of the underlying data." } ; { $description "Takes a zlib-compressed byte-array and uncompresses it to another byte-array." } ;
ARTICLE: "compression.zlib" "Compression (ZLIB)" ARTICLE: "compression.zlib" "Compression (ZLIB)"
"The " { $vocab-link "compression.zlib" } " vocabulary provides support for ZLIB:" "The " { $vocab-link "compression.zlib" } " vocabulary provides support for ZLIB:"

View File

@ -7,6 +7,5 @@ IN: compression.zlib.tests
: compress-me ( -- byte-array ) B{ 1 2 3 4 5 } ; : compress-me ( -- byte-array ) B{ 1 2 3 4 5 } ;
[ t ] [ compress-me [ compress uncompress ] keep = ] unit-test [ t ] [ compress-me [ compress uncompress ] keep = ] unit-test
[ t ] [ compress-me compress compressed instance? ] unit-test
[ ffi:Z_DATA_ERROR zlib-error-message ] [ string>> "data error" = ] must-fail-with [ ffi:Z_DATA_ERROR zlib-error-message ] [ string>> "data error" = ] must-fail-with

View File

@ -6,13 +6,6 @@ fry kernel libc math math.functions math.ranges sequences system ;
QUALIFIED: compression.zlib.ffi QUALIFIED: compression.zlib.ffi
IN: compression.zlib IN: compression.zlib
TUPLE: compressed data length ;
: <compressed> ( data length -- compressed )
compressed new
swap >>length
swap >>data ;
ERROR: zlib-failed n string ; ERROR: zlib-failed n string ;
: zlib-error-message ( n -- * ) : zlib-error-message ( n -- * )
@ -34,26 +27,13 @@ ERROR: zlib-failed n string ;
: compressed-size ( byte-array -- n ) : compressed-size ( byte-array -- n )
length 1001/1000 * ceiling 12 + ; length 1001/1000 * ceiling 12 + ;
: compress ( byte-array -- compressed ) : compress ( byte-array -- byte-array' )
[ [
[ compressed-size
compressed-size [ <byte-vector> dup underlying>> ] keep ulong <ref>
[ <byte-vector> dup underlying>> ] keep ulong <ref> ] keep [
] keep [ dup length compression.zlib.ffi:compress zlib-error
dup length compression.zlib.ffi:compress zlib-error ] 2keep drop ulong deref >>length B{ } like ;
] 2keep drop ulong deref >>length B{ } like
] keep length <compressed> ;
GENERIC: uncompress ( obj -- byte-array )
M: compressed uncompress ( compressed -- byte-array )
[
length>> [ <byte-vector> dup underlying>> ] keep
ulong <ref>
] [
data>> dup length pick
[ compression.zlib.ffi:uncompress zlib-error ] dip
] bi ulong deref >>length B{ } like ;
: (uncompress) ( length byte-array -- byte-array ) : (uncompress) ( length byte-array -- byte-array )
[ [
@ -63,6 +43,6 @@ M: compressed uncompress ( compressed -- byte-array )
2drop ulong deref memory>byte-array 2drop ulong deref memory>byte-array
] with-destructors ; ] with-destructors ;
M: byte-array uncompress ( byte-array -- byte-array ) : uncompress ( byte-array -- byte-array' )
[ length 5 [0,b) [ 2^ * ] with map ] keep [ length 5 [0,b) [ 2^ * ] with map ] keep
'[ _ (uncompress) ] attempt-all ; '[ _ (uncompress) ] attempt-all ;