5
5
| `Undefined
6
6
| `Simple of int
7
7
| `Bool of bool
8
- | `Int of int
8
+ | `Int of int64
9
9
| `Float of float
10
10
| `Bytes of string
11
11
| `Text of string
@@ -19,7 +19,7 @@ let rec pp_diagnostic out (self : t) =
19
19
| `Undefined -> Fmt. string out " undefined"
20
20
| `Simple i -> Fmt. fprintf out " simple(%d)" i
21
21
| `Bool b -> Fmt. bool out b
22
- | `Int i -> Fmt. int out i
22
+ | `Int i -> Fmt. int64 out i
23
23
| `Float f -> Fmt. float out f
24
24
| `Bytes b -> Fmt. fprintf out " h'%s'" (CCString. to_hex b)
25
25
| `Text s -> Fmt. fprintf out " %S" s
@@ -49,6 +49,13 @@ let to_string_diagnostic (self : t) : string =
49
49
50
50
exception Indefinite
51
51
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
+
52
59
let decode_exn (s : string ) : t =
53
60
let b = Bytes. unsafe_of_string s in
54
61
let i = ref 0 in
@@ -87,14 +94,6 @@ let decode_exn (s : string) : t =
87
94
j
88
95
in
89
96
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
-
98
97
(* read integer value from least significant bits *)
99
98
let read_int ~allow_indefinite low =
100
99
match low with
@@ -153,10 +152,10 @@ let decode_exn (s : string) : t =
153
152
let high = (c land 0b111_00000 ) lsr 5 in
154
153
let low = c land 0b000_11111 in
155
154
match high with
156
- | 0 -> `Int (read_int ~allow_indefinite: false low |> i64_to_int )
155
+ | 0 -> `Int (read_int ~allow_indefinite: false low)
157
156
| 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)
160
159
| 2 ->
161
160
let s = read_bytes ~ty: `Bytes low in
162
161
`Bytes s
@@ -255,22 +254,22 @@ let encode ?(buf = Buffer.create 32) (self : t) : string =
255
254
let add_i64 (i : int64 ) = Buffer. add_int64_be buf i in
256
255
257
256
(* 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 (
263
262
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 (
266
265
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 (
269
268
add_byte high 26 ;
270
- Buffer. add_int32_be buf (Int32. of_int x)
269
+ Buffer. add_int32_be buf (Int64. to_int32 x)
271
270
) else (
272
271
add_byte high 27 ;
273
- Buffer. add_int64_be buf ( Int64. of_int x)
272
+ Buffer. add_int64_be buf x
274
273
)
275
274
in
276
275
@@ -293,33 +292,33 @@ let encode ?(buf = Buffer.create 32) (self : t) : string =
293
292
(* float 64 *)
294
293
add_i64 (Int64. bits_of_float f)
295
294
| `Array l ->
296
- add_uint 4 (List. length l);
295
+ add_uint 4 (Int64. of_int ( List. length l) );
297
296
List. iter encode_val l
298
297
| `Map l ->
299
- add_uint 5 (List. length l);
298
+ add_uint 5 (Int64. of_int ( List. length l) );
300
299
List. iter
301
300
(fun (k , v ) ->
302
301
encode_val k;
303
302
encode_val v)
304
303
l
305
304
| `Text s ->
306
- add_uint 3 (String. length s);
305
+ add_uint 3 (Int64. of_int ( String. length s) );
307
306
Buffer. add_string buf s
308
307
| `Bytes s ->
309
- add_uint 2 (String. length s);
308
+ add_uint 2 (Int64. of_int ( String. length s) );
310
309
Buffer. add_string buf s
311
310
| `Tag (t , v ) ->
312
- add_uint 6 t ;
311
+ add_uint 6 ( Int64. of_int t) ;
313
312
encode_val v
314
313
| `Int i ->
315
- if i > = 0 then
314
+ if i > = Int64. zero then
316
315
add_uint 0 i
317
- else if min_int + 2 > i then (
316
+ else if Int64. (add min_int 2L ) > i then (
318
317
(* large negative int, be careful. encode [(-i)-1] via int64. *)
319
318
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 ))
321
320
) else
322
- add_uint 1 ( - i - 1 )
321
+ add_uint 1 Int64. (sub (neg i) one )
323
322
in
324
323
encode_val self;
325
324
Buffer. contents buf
0 commit comments