Skip to content

Commit 7e33936

Browse files
committed
Serialize positive and negative parts of ints
Rather than using the preprocessor to determine the sign of a value, we store both `x` and `-x`. One of them has an all-digit representation. Closes ocaml#1720 Signed-off-by: Etienne Millon <[email protected]>
1 parent 15fff72 commit 7e33936

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/configurator/v1.ml

+18-2
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ module C_define = struct
350350
#define D7(x) ('0'+(x/10000000 )%%10), D6(x)
351351
#define D8(x) ('0'+(x/100000000 )%%10), D7(x)
352352
#define D9(x) ('0'+(x/1000000000)%%10), D8(x)
353+
#define S9(x) D9(x), D9(-x)
353354
|}
354355
);
355356
List.iteri vars ~f:(fun i (name, t) ->
@@ -366,7 +367,7 @@ module C_define = struct
366367
pr {|
367368
const char s%i[] = {
368369
'B', 'E', 'G', 'I', 'N', '-', %s'-',
369-
D9((%s)),
370+
S9((%s)),
370371
'-', 'E', 'N', 'D'
371372
};
372373
|} i c_arr_i name
@@ -383,6 +384,21 @@ const char *s%i = "BEGIN-%i-false-END";
383384
);
384385
Buffer.contents buf
385386

387+
let is_digit = function
388+
| '0'..'9' -> true
389+
| _ -> false
390+
391+
(** The generated string has two parts of equal length: one for x and one for -x.
392+
Since digits are computed with ['0' + something % 10], either:
393+
- the first part is all digits correct (meaning that the number is positive)
394+
- or the second is (meaning that the number is negative). *)
395+
let signed_int_of_string s =
396+
let pos_part, neg_part = String.split_n s 10 in
397+
if String.for_all pos_part ~f:is_digit then
398+
int_of_string pos_part
399+
else
400+
- int_of_string neg_part
401+
386402
let extract_values obj_file vars =
387403
let values =
388404
Io.with_lexbuf_from_file obj_file ~f:(Extract_obj.extract [])
@@ -396,7 +412,7 @@ const char *s%i = "BEGIN-%i-false-END";
396412
| Some v -> v in
397413
match t with
398414
| Type.Switch -> Value.Switch (bool_of_string raw_val)
399-
| Int -> Int (int_of_string raw_val)
415+
| Int -> Int (signed_int_of_string raw_val)
400416
| String -> String raw_val in
401417
(name, value))
402418

0 commit comments

Comments
 (0)