]> gitweb.factorcode.org Git - factor.git/commitdiff
compression.zstd: simplify with an accumulator
authorJohn Benediktsson <mrjbq7@gmail.com>
Tue, 16 May 2023 02:16:41 +0000 (19:16 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Tue, 16 May 2023 02:16:41 +0000 (19:16 -0700)
basis/compression/zstd/zstd.factor

index 58e3c269101977a0db0b6f92678179bc1b89c7ff..8c0663f86352afcb26c9f3494311c47492533b2d 100644 (file)
@@ -49,40 +49,40 @@ PRIVATE>
     in-size <byte-vector> :> in
     out-size <byte-array> :> out
 
+    out-size <byte-vector> :> accum
+
     [
+        ZSTD_createDCtx &ZSTD_freeDCtx :> dctx
+        0 size_t <ref> :> in-pos
+        0 size_t <ref> :> out-pos
+
+        in [ underlying>> read-into drop length ] [ set-length ] bi
+
         [
-            ZSTD_createDCtx &ZSTD_freeDCtx :> dctx
-            0 size_t <ref> :> in-pos
-            0 size_t <ref> :> out-pos
-
-            in [ underlying>> read-into drop length ] [ set-length ] bi
-
-            [
-                dctx
-                out out-size out-pos
-                in in length in-pos
-                ZSTD_decompressStream_simpleArgs check-zstd-error
-
-                out out-pos size_t deref head-slice %
-
-                in-pos size_t deref in-size = [
-                    in [ underlying>> read-into drop length ] [ set-length ] bi
-                    0 in-pos 0 size_t set-alien-value
-                ] when
-
-                zero? [
-                    ! 0 is only seen when a frame is fully
-                    ! decoded *and* fully flushed. But there may
-                    ! be extra input data
-                    f
-                ] [
-                    ! We're not at the end of the frame *or*
-                    ! we're not fully flushed.
-                    in-pos size_t deref in-size =
-                    out-pos size_t deref out-size < and not
-
-                    0 out-pos 0 size_t set-alien-value
-                ] if
-            ] loop
-        ] B{ } make
+            dctx
+            out out-size out-pos
+            in in length in-pos
+            ZSTD_decompressStream_simpleArgs check-zstd-error
+
+            out out-pos size_t deref head-slice accum push-all
+
+            in-pos size_t deref in-size = [
+                in [ underlying>> read-into drop length ] [ set-length ] bi
+                0 in-pos 0 size_t set-alien-value
+            ] when
+
+            zero? [
+                ! 0 is only seen when a frame is fully
+                ! decoded *and* fully flushed. But there may
+                ! be extra input data
+                f
+            ] [
+                ! We're not at the end of the frame *or*
+                ! we're not fully flushed.
+                in-pos size_t deref in-size =
+                out-pos size_t deref out-size < and not
+
+                0 out-pos 0 size_t set-alien-value
+            ] if
+        ] loop accum B{ } like
     ] with-destructors ;