55 | `Undefined
66 | `Simple of int
77 | `Bool of bool
8- | `Int of int
8+ | `Int of int64
99 | `Float of float
1010 | `Bytes of string
1111 | `Text of string
@@ -19,7 +19,7 @@ let rec pp_diagnostic out (self : t) =
1919 | `Undefined -> Fmt. string out " undefined"
2020 | `Simple i -> Fmt. fprintf out " simple(%d)" i
2121 | `Bool b -> Fmt. bool out b
22- | `Int i -> Fmt. int out i
22+ | `Int i -> Fmt. int64 out i
2323 | `Float f -> Fmt. float out f
2424 | `Bytes b -> Fmt. fprintf out " h'%s'" (CCString. to_hex b)
2525 | `Text s -> Fmt. fprintf out " %S" s
@@ -49,6 +49,13 @@ let to_string_diagnostic (self : t) : string =
4949
5050exception Indefinite
5151
52+ let [@ inline] i64_to_int i =
53+ let j = Int64. to_int i in
54+ if Int64. (of_int j = i) then
55+ j
56+ else
57+ failwith " int64 does not fit in int"
58+
5259let decode_exn (s : string ) : t =
5360 let b = Bytes. unsafe_of_string s in
5461 let i = ref 0 in
@@ -87,14 +94,6 @@ let decode_exn (s : string) : t =
8794 j
8895 in
8996
90- let [@ inline] i64_to_int i =
91- let j = Int64. to_int i in
92- if Int64. (of_int j = i) then
93- j
94- else
95- failwith " int64 does not fit in int"
96- in
97-
9897 (* read integer value from least significant bits *)
9998 let read_int ~allow_indefinite low =
10099 match low with
@@ -153,10 +152,10 @@ let decode_exn (s : string) : t =
153152 let high = (c land 0b111_00000 ) lsr 5 in
154153 let low = c land 0b000_11111 in
155154 match high with
156- | 0 -> `Int (read_int ~allow_indefinite: false low |> i64_to_int )
155+ | 0 -> `Int (read_int ~allow_indefinite: false low)
157156 | 1 ->
158- let i = read_int ~allow_indefinite: false low |> i64_to_int in
159- `Int ( - 1 - i)
157+ let i = read_int ~allow_indefinite: false low in
158+ `Int Int64. (sub minus_one i)
160159 | 2 ->
161160 let s = read_bytes ~ty: `Bytes low in
162161 `Bytes s
@@ -255,22 +254,22 @@ let encode ?(buf = Buffer.create 32) (self : t) : string =
255254 let add_i64 (i : int64 ) = Buffer. add_int64_be buf i in
256255
257256 (* add unsigned integer, including first tag byte *)
258- let add_uint (high : int ) (x : int ) =
259- assert (x > = 0 );
260- if x < 24 then
261- add_byte high x
262- else if x < = 0xff then (
257+ let add_uint (high : int ) (x : int64 ) =
258+ assert (x > = 0L );
259+ if x < 24L then
260+ add_byte high (i64_to_int x)
261+ else if x < = 0xffL then (
263262 add_byte high 24 ;
264- Buffer. add_char buf (Char. unsafe_chr x )
265- ) else if x < = 0xff_ff then (
263+ Buffer. add_char buf (Char. unsafe_chr (i64_to_int x) )
264+ ) else if x < = 0xff_ffL then (
266265 add_byte high 25 ;
267- Buffer. add_uint16_be buf x
268- ) else if x < = 0xff_ff_ff_ff then (
266+ Buffer. add_uint16_be buf (i64_to_int x)
267+ ) else if x < = 0xff_ff_ff_ffL then (
269268 add_byte high 26 ;
270- Buffer. add_int32_be buf (Int32. of_int x)
269+ Buffer. add_int32_be buf (Int64. to_int32 x)
271270 ) else (
272271 add_byte high 27 ;
273- Buffer. add_int64_be buf ( Int64. of_int x)
272+ Buffer. add_int64_be buf x
274273 )
275274 in
276275
@@ -293,33 +292,33 @@ let encode ?(buf = Buffer.create 32) (self : t) : string =
293292 (* float 64 *)
294293 add_i64 (Int64. bits_of_float f)
295294 | `Array l ->
296- add_uint 4 (List. length l);
295+ add_uint 4 (Int64. of_int ( List. length l) );
297296 List. iter encode_val l
298297 | `Map l ->
299- add_uint 5 (List. length l);
298+ add_uint 5 (Int64. of_int ( List. length l) );
300299 List. iter
301300 (fun (k , v ) ->
302301 encode_val k;
303302 encode_val v)
304303 l
305304 | `Text s ->
306- add_uint 3 (String. length s);
305+ add_uint 3 (Int64. of_int ( String. length s) );
307306 Buffer. add_string buf s
308307 | `Bytes s ->
309- add_uint 2 (String. length s);
308+ add_uint 2 (Int64. of_int ( String. length s) );
310309 Buffer. add_string buf s
311310 | `Tag (t , v ) ->
312- add_uint 6 t ;
311+ add_uint 6 ( Int64. of_int t) ;
313312 encode_val v
314313 | `Int i ->
315- if i > = 0 then
314+ if i > = Int64. zero then
316315 add_uint 0 i
317- else if min_int + 2 > i then (
316+ else if Int64. (add min_int 2L ) > i then (
318317 (* large negative int, be careful. encode [(-i)-1] via int64. *)
319318 add_byte 1 27 ;
320- Buffer. add_int64_be buf Int64. (neg (add 1L (of_int i) ))
319+ Buffer. add_int64_be buf Int64. (neg (add 1L i ))
321320 ) else
322- add_uint 1 ( - i - 1 )
321+ add_uint 1 Int64. (sub (neg i) one )
323322 in
324323 encode_val self;
325324 Buffer. contents buf
0 commit comments