Skip to content

Commit 6a415e9

Browse files
committed
fix(cbor): use int64 as main int type
1 parent 484aa3a commit 6a415e9

File tree

4 files changed

+38
-39
lines changed

4 files changed

+38
-39
lines changed

Diff for: src/cbor/containers_cbor.ml

+32-33
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ type t =
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

5050
exception 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+
5259
let 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

Diff for: src/cbor/containers_cbor.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type t =
1616
| `Undefined
1717
| `Simple of int
1818
| `Bool of bool
19-
| `Int of int
19+
| `Int of int64
2020
| `Float of float
2121
| `Bytes of string
2222
| `Text of string

Diff for: src/cbor/tests/t_appendix_a.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,11 @@ let run_test (c : count) (t : Test.t) : unit =
122122
try compare_cj (List.assoc (`Text k) l) v
123123
with Not_found -> false)
124124
l2
125-
| `Int i, `Int j -> i = j
125+
| `Int i, `Int j -> i = Int64.of_int j
126126
| `Text s1, `String s2 -> s1 = s2
127127
| `Array l1, `List l2 ->
128128
List.length l1 = List.length l2 && List.for_all2 compare_cj l1 l2
129-
| `Int i, `Intlit s -> string_of_int i = s
129+
| `Int i, `Intlit s -> Int64.to_string i = s
130130
| _, `Intlit "-18446744073709551617" ->
131131
(* skip bigint test*)
132132
true

Diff for: tests/core/t_cbor.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ let gen_c : Cbor.t Q.Gen.t =
1313
1, return `Null;
1414
1, return `Undefined;
1515
( 3,
16-
let+ x = int in
16+
let+ x = int >|= Int64.of_int in
1717
`Int x );
1818
( 1,
1919
let+ b = bool in
@@ -71,8 +71,8 @@ let rec shrink (c : Cbor.t) : Cbor.t Q.Iter.t =
7171
let+ i = Q.Shrink.int i in
7272
`Simple i
7373
| `Int i ->
74-
let+ i = Q.Shrink.int i in
75-
`Int i
74+
let+ i = Q.Shrink.int (Int64.to_int i) in
75+
`Int (Int64.of_int i)
7676
| `Tag (t, i) ->
7777
let+ i = shrink i in
7878
`Tag (t, i)

0 commit comments

Comments
 (0)