aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAdelyn Breelove <[email protected]>2019-01-29 16:01:19 -0700
committerAdelyn Breelove <[email protected]>2019-01-29 16:01:19 -0700
commit4bf4bef691b27ab047d091dde69cd53d4fbce69e (patch)
tree67268b218e2e981400cb82a0a20dd1ac053e2356 /lib
parentMake a few things more legible, update build and readme (diff)
downloaddisml-4bf4bef691b27ab047d091dde69cd53d4fbce69e.tar.xz
disml-4bf4bef691b27ab047d091dde69cd53d4fbce69e.zip
Remove some silly internals
Diffstat (limited to 'lib')
-rw-r--r--lib/sharder.ml30
1 files changed, 14 insertions, 16 deletions
diff --git a/lib/sharder.ml b/lib/sharder.ml
index ddd419d..157967d 100644
--- a/lib/sharder.ml
+++ b/lib/sharder.ml
@@ -9,28 +9,26 @@ exception Inflate_error of Zlib_inflate.error
let window = Window.create ~witness:B.bytes
-let decompress data =
+let decompress src =
let in_buf = Bytes.create 0xFFFF in
let out_buf = Bytes.create 0xFFFF in
let window = Window.reset window in
let pos = ref 0 in
- let res = Buffer.create (String.length data) in
+ let src_len = String.length src in
+ let res = Buffer.create (src_len) in
Zlib_inflate.bytes in_buf out_buf
- (fun in_buf ->
- let n = min 0xFFFF (String.length data - !pos) in
- Caml.Bytes.blit_string data !pos in_buf 0 n;
- pos := !pos + n;
- n)
- (fun out_buf len ->
- Buffer.add_subbytes res out_buf 0 len; 0xFFFF)
+ (fun dst ->
+ let len = min 0xFFFF (src_len - !pos) in
+ Caml.Bytes.blit_string src !pos dst 0 len;
+ pos := !pos + len;
+ len)
+ (fun obuf len ->
+ Buffer.add_subbytes res obuf 0 len; 0xFFFF)
(Zlib_inflate.default ~witness:B.bytes window)
|> function
| Ok _ -> Buffer.contents res
| Error exn -> raise (Inflate_error exn)
-let try_decompress data = try decompress data
- with Inflate_error _ -> data
-
module Shard = struct
type shard = {
compress: bool;
@@ -57,10 +55,10 @@ module Shard = struct
| `Ok s -> begin
let open Frame.Opcode in
match s.opcode with
- | Text | Binary ->
- let s = if compress then try_decompress s.content
- else s.content in
- Some (Yojson.Safe.from_string s)
+ | Text -> Some (Yojson.Safe.from_string s.content)
+ | Binary ->
+ if compress then Some (decompress s.content |> Yojson.Safe.from_string)
+ else None
| _ -> None
end
| `Eof -> None