Skip to content

Commit

Permalink
add Util_zlib.decompress_slice
Browse files Browse the repository at this point in the history
  • Loading branch information
c-cube committed Mar 21, 2024
1 parent 5d1cd5b commit 169cffd
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 5 deletions.
1 change: 0 additions & 1 deletion imandrakit.opam
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ build: [
]
]
dev-repo: "git+https://github.com/imandra-ai/imandrakit.git"

pin-depends: [
["moonpool.dev" "git+https://github.com/c-cube/moonpool#main"]
]
Expand Down
1 change: 0 additions & 1 deletion imandrakit.opam.template
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

pin-depends: [
["moonpool.dev" "git+https://github.com/c-cube/moonpool#main"]
]
Expand Down
10 changes: 7 additions & 3 deletions src/core/util_zlib.ml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ let compress ?(buf = Buffer.create 32) (s : bytes) : bytes =
let compress_str ?buf s : string =
compress ?buf (Bytes.unsafe_of_string s) |> Bytes.unsafe_to_string

let decompress ?(buf = Buffer.create 32) (s : bytes) : bytes =
let decompress_slice ?(buf = Buffer.create 32) (s : bytes) offset len : bytes =
if offset + len > Bytes.length s then invalid_arg "Util_zlib.decompress";
Buffer.clear buf;
let i = ref 0 in
let i = ref offset in
let last = offset + len in
Zlib.uncompress ~header:false
(fun bytes ->
let len = min (Bytes.length s - !i) (Bytes.length bytes) in
let len = min (last - !i) (Bytes.length bytes) in
if len > 0 then (
Bytes.blit s !i bytes 0 len;
i := !i + len
Expand All @@ -25,5 +27,7 @@ let decompress ?(buf = Buffer.create 32) (s : bytes) : bytes =
(fun bytes len -> Buffer.add_subbytes buf bytes 0 len);
Buffer.contents buf |> Bytes.unsafe_of_string

let decompress ?buf s : bytes = decompress_slice ?buf s 0 (Bytes.length s)

let decompress_str ?buf s : string =
decompress ?buf (Bytes.unsafe_of_string s) |> Bytes.unsafe_to_string
1 change: 1 addition & 0 deletions src/core/util_zlib.mli
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ val compress_str : ?buf:Buffer.t -> string -> string
val decompress : ?buf:Buffer.t -> bytes -> bytes
(** Decompress the bytes. *)

val decompress_slice : ?buf:Buffer.t -> bytes -> int -> int -> bytes
val decompress_str : ?buf:Buffer.t -> string -> string

0 comments on commit 169cffd

Please sign in to comment.