-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathencode.ml
67 lines (60 loc) · 1.09 KB
/
encode.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
open Msgpack__MsgpackBase
type t = Msgpack.Serialize.t
let int n =
if 0 <= n then begin
(* positive *)
if n <= 127 then
`PFixnum n
else if n <= 0xFF then
`Uint8 n
else if n <= 0xFFFF then
`Uint16 n
else if (Int64.of_int n) <= 0xFFFF_FFFFL then
`Uint32 (Int64.of_int n)
else
`Uint64 (Big_int.big_int_of_int n)
end else begin
(* negative *)
if -32 <= n then
`NFixnum n
else if -127 <= n then
`Int8 n
else if -32767 <= n then
`Int16 n
else if -2147483647l <= (Int32.of_int n) then
`Int32 (Int32.of_int n)
else
`Int64 (Int64.of_int n)
end
let raw xs =
let n =
List.length xs
in
if n < 32 then
`FixRaw xs
else if n <= 0xFFFF then
`Raw16 xs
else
`Raw32 xs
let str s =
raw @@ explode s
let array xs =
let n =
List.length xs
in
if n < 15 then
`FixArray xs
else if n <= 0xFFFF then
`Array16 xs
else
`Array32 xs
let map xs =
let n =
List.length xs
in
if n < 32 then
`FixMap xs
else if n <= 0xFFFF then
`Map16 xs
else
`Map32 xs