Skip to content

Commit b4597ad

Browse files
committed
reuse Caml_int64.to_string
1 parent c1ed887 commit b4597ad

File tree

5 files changed

+55
-71
lines changed

5 files changed

+55
-71
lines changed

Diff for: jscomp/runtime/caml_format.ml

+6-17
Original file line numberDiff line numberDiff line change
@@ -378,11 +378,12 @@ let caml_format_int fmt i =
378378
will overflow signed integer in general
379379
*)
380380
let dec_of_pos_int64 x =
381-
let s = ref "" in
382-
let wbase = 10L in
383-
let cvtbl = "0123456789" in
381+
384382

385383
(if x < 0L then
384+
let s = ref "" in
385+
let wbase = 10L in
386+
let cvtbl = "0123456789" in
386387
let y = Caml_int64.discard_sign x in
387388
(* 2 ^ 63 + y `div_mod` 10 *)
388389

@@ -410,21 +411,9 @@ let dec_of_pos_int64 x =
410411
modulus .contents<- b;
411412
s .contents<- Caml_string_extern.get_string_unsafe cvtbl (Caml_int64_extern.to_int modulus.contents) ^ s.contents ;
412413
done;
413-
414+
s.contents
414415
else
415-
let a, b = Caml_int64.div_mod x wbase in
416-
let quotient = ref a in
417-
let modulus = ref b in
418-
s .contents<-
419-
Caml_string_extern.get_string_unsafe
420-
cvtbl ( Caml_int64_extern.to_int modulus.contents) ^ s.contents ;
421-
422-
while quotient.contents <> 0L do
423-
let a, b = Caml_int64.div_mod (quotient.contents) wbase in
424-
quotient .contents<- a;
425-
modulus .contents<- b;
426-
s .contents<- Caml_string_extern.get_string_unsafe cvtbl (Caml_int64_extern.to_int modulus.contents) ^ s.contents ;
427-
done); s.contents
416+
Caml_int64.to_string x)
428417

429418
let oct_of_int64 x =
430419
let s = ref "" in

Diff for: jscomp/test/int64_test.js

+28-13
Original file line numberDiff line numberDiff line change
@@ -2430,83 +2430,98 @@ function id(loc, x) {
24302430
}
24312431
}
24322432

2433-
eq("File \"int64_test.ml\", line 192, characters 5-12", Caml_int64.bits_of_float(0.3), /* int64 */[
2433+
eq("File \"int64_test.ml\", line 193, characters 5-12", Caml_int64.bits_of_float(0.3), /* int64 */[
24342434
/* hi */1070805811,
24352435
/* lo */858993459
24362436
]);
24372437

2438-
eq("File \"int64_test.ml\", line 193, characters 5-12", Caml_int64.float_of_bits(/* int64 */[
2438+
eq("File \"int64_test.ml\", line 194, characters 5-12", Caml_int64.float_of_bits(/* int64 */[
24392439
/* hi */1070805811,
24402440
/* lo */858993459
24412441
]), 0.3);
24422442

2443-
id("File \"int64_test.ml\", line 194, characters 5-12", /* int64 */[
2443+
id("File \"int64_test.ml\", line 195, characters 5-12", /* int64 */[
24442444
/* hi */-1,
24452445
/* lo */4294967295
24462446
]);
24472447

2448-
id("File \"int64_test.ml\", line 195, characters 5-12", /* int64 */[
2448+
id("File \"int64_test.ml\", line 196, characters 5-12", /* int64 */[
24492449
/* hi */-1,
24502450
/* lo */4294967196
24512451
]);
24522452

2453-
id("File \"int64_test.ml\", line 196, characters 5-12", /* int64 */[
2453+
id("File \"int64_test.ml\", line 197, characters 5-12", /* int64 */[
24542454
/* hi */0,
24552455
/* lo */4294967295
24562456
]);
24572457

2458-
id("File \"int64_test.ml\", line 197, characters 5-12", /* int64 */[
2458+
id("File \"int64_test.ml\", line 198, characters 5-12", /* int64 */[
24592459
/* hi */0,
24602460
/* lo */536870911
24612461
]);
24622462

2463-
id("File \"int64_test.ml\", line 198, characters 5-12", /* int64 */[
2463+
id("File \"int64_test.ml\", line 199, characters 5-12", /* int64 */[
24642464
/* hi */0,
24652465
/* lo */536870655
24662466
]);
24672467

2468-
eq("File \"int64_test.ml\", line 199, characters 5-12", Caml_int64.div(Int64.min_int, /* int64 */[
2468+
eq("File \"int64_test.ml\", line 200, characters 5-12", Caml_int64.div(Int64.min_int, /* int64 */[
24692469
/* hi */0,
24702470
/* lo */10
24712471
]), /* int64 */[
24722472
/* hi */-214748365,
24732473
/* lo */858993460
24742474
]);
24752475

2476-
eq("File \"int64_test.ml\", line 200, characters 5-12", Caml_int64.to_string(Caml_int64.div(Int64.min_int, /* int64 */[
2476+
eq("File \"int64_test.ml\", line 201, characters 5-12", Caml_int64.to_string(Caml_int64.div(Int64.min_int, /* int64 */[
24772477
/* hi */0,
24782478
/* lo */10
24792479
])), "-922337203685477580");
24802480

2481-
eq("File \"int64_test.ml\", line 201, characters 5-12", Caml_int64.mul(Int64.min_int, /* int64 */[
2481+
eq("File \"int64_test.ml\", line 202, characters 5-12", Caml_int64.mul(Int64.min_int, /* int64 */[
24822482
/* hi */0,
24832483
/* lo */10
24842484
]), /* int64 */[
24852485
/* hi */0,
24862486
/* lo */0
24872487
]);
24882488

2489-
eq("File \"int64_test.ml\", line 202, characters 5-12", Caml_int64.mul(/* int64 */[
2489+
eq("File \"int64_test.ml\", line 203, characters 5-12", Caml_int64.mul(/* int64 */[
24902490
/* hi */0,
24912491
/* lo */10
24922492
], Int64.min_int), /* int64 */[
24932493
/* hi */0,
24942494
/* lo */0
24952495
]);
24962496

2497-
eq("File \"int64_test.ml\", line 203, characters 5-12", Caml_int64.mul(/* int64 */[
2497+
eq("File \"int64_test.ml\", line 204, characters 5-12", Caml_int64.mul(/* int64 */[
24982498
/* hi */0,
24992499
/* lo */1
25002500
], Int64.min_int), Int64.min_int);
25012501

2502-
eq("File \"int64_test.ml\", line 204, characters 5-12", Caml_int64.mul(Int64.max_int, /* int64 */[
2502+
eq("File \"int64_test.ml\", line 205, characters 5-12", Caml_int64.mul(Int64.max_int, /* int64 */[
25032503
/* hi */0,
25042504
/* lo */10
25052505
]), /* int64 */[
25062506
/* hi */-1,
25072507
/* lo */4294967286
25082508
]);
25092509

2510+
eq("File \"int64_test.ml\", line 206, characters 5-12", Caml_int64.succ(Int64.max_int), Int64.min_int);
2511+
2512+
eq("File \"int64_test.ml\", line 207, characters 5-12", Caml_int64.succ(Int64.min_int), /* int64 */[
2513+
/* hi */-2147483648,
2514+
/* lo */1
2515+
]);
2516+
2517+
eq("File \"int64_test.ml\", line 208, characters 5-12", Caml_int64.succ(/* int64 */[
2518+
/* hi */0,
2519+
/* lo */4294967295
2520+
]), /* int64 */[
2521+
/* hi */1,
2522+
/* lo */0
2523+
]);
2524+
25102525
Mt.from_pair_suites("Int64_test", suites$1.contents);
25112526

25122527
exports.f = f;

Diff for: jscomp/test/int64_test.ml

+5-1
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ let id loc (x : int64) =
189189

190190

191191
let () =
192+
let open Int64 in
192193
eq __LOC__ (Int64.bits_of_float 0.3) 4599075939470750515L;
193194
eq __LOC__ (Int64.float_of_bits 4599075939470750515L) 0.3;
194195
id __LOC__ (-1L);
@@ -201,5 +202,8 @@ let () =
201202
eq __LOC__ Int64.(mul min_int 10L) 0L;
202203
eq __LOC__ Int64.(mul 10L min_int) 0L;
203204
eq __LOC__ Int64.(mul 1L min_int) min_int;
204-
eq __LOC__ Int64.(mul max_int 10L) (-10L)
205+
eq __LOC__ Int64.(mul max_int 10L) (-10L);
206+
eq __LOC__ Int64.(succ max_int) (min_int);
207+
eq __LOC__ Int64.(succ min_int) (-9223372036854775807L);
208+
eq __LOC__ (succ 0xffff_ffffL) 0x1_0000_0000L
205209
;; Mt.from_pair_suites __MODULE__ !suites

Diff for: lib/es6/caml_format.js

+8-20
Original file line numberDiff line numberDiff line change
@@ -577,16 +577,16 @@ function caml_format_int(fmt, i) {
577577
}
578578

579579
function dec_of_pos_int64(x) {
580-
var s = "";
581-
var wbase = /* int64 */[
582-
/* hi */0,
583-
/* lo */10
584-
];
585-
var cvtbl = "0123456789";
586580
if (Caml_int64.lt(x, /* int64 */[
587581
/* hi */0,
588582
/* lo */0
589583
])) {
584+
var s = "";
585+
var wbase = /* int64 */[
586+
/* hi */0,
587+
/* lo */10
588+
];
589+
var cvtbl = "0123456789";
590590
var y = Caml_int64.discard_sign(x);
591591
var match = Caml_int64.div_mod(y, wbase);
592592
var match$1 = Caml_int64.div_mod(Caml_int64.add(/* int64 */[
@@ -608,22 +608,10 @@ function dec_of_pos_int64(x) {
608608
modulus = match$2[1];
609609
s = cvtbl[Caml_int64.to_int32(modulus)] + s;
610610
};
611+
return s;
611612
} else {
612-
var match$3 = Caml_int64.div_mod(x, wbase);
613-
var quotient$1 = match$3[0];
614-
var modulus$1 = match$3[1];
615-
s = cvtbl[Caml_int64.to_int32(modulus$1)] + s;
616-
while(Caml_int64.neq(quotient$1, /* int64 */[
617-
/* hi */0,
618-
/* lo */0
619-
])) {
620-
var match$4 = Caml_int64.div_mod(quotient$1, wbase);
621-
quotient$1 = match$4[0];
622-
modulus$1 = match$4[1];
623-
s = cvtbl[Caml_int64.to_int32(modulus$1)] + s;
624-
};
613+
return Caml_int64.to_string(x);
625614
}
626-
return s;
627615
}
628616

629617
function oct_of_int64(x) {

Diff for: lib/js/caml_format.js

+8-20
Original file line numberDiff line numberDiff line change
@@ -577,16 +577,16 @@ function caml_format_int(fmt, i) {
577577
}
578578

579579
function dec_of_pos_int64(x) {
580-
var s = "";
581-
var wbase = /* int64 */[
582-
/* hi */0,
583-
/* lo */10
584-
];
585-
var cvtbl = "0123456789";
586580
if (Caml_int64.lt(x, /* int64 */[
587581
/* hi */0,
588582
/* lo */0
589583
])) {
584+
var s = "";
585+
var wbase = /* int64 */[
586+
/* hi */0,
587+
/* lo */10
588+
];
589+
var cvtbl = "0123456789";
590590
var y = Caml_int64.discard_sign(x);
591591
var match = Caml_int64.div_mod(y, wbase);
592592
var match$1 = Caml_int64.div_mod(Caml_int64.add(/* int64 */[
@@ -608,22 +608,10 @@ function dec_of_pos_int64(x) {
608608
modulus = match$2[1];
609609
s = cvtbl[Caml_int64.to_int32(modulus)] + s;
610610
};
611+
return s;
611612
} else {
612-
var match$3 = Caml_int64.div_mod(x, wbase);
613-
var quotient$1 = match$3[0];
614-
var modulus$1 = match$3[1];
615-
s = cvtbl[Caml_int64.to_int32(modulus$1)] + s;
616-
while(Caml_int64.neq(quotient$1, /* int64 */[
617-
/* hi */0,
618-
/* lo */0
619-
])) {
620-
var match$4 = Caml_int64.div_mod(quotient$1, wbase);
621-
quotient$1 = match$4[0];
622-
modulus$1 = match$4[1];
623-
s = cvtbl[Caml_int64.to_int32(modulus$1)] + s;
624-
};
613+
return Caml_int64.to_string(x);
625614
}
626-
return s;
627615
}
628616

629617
function oct_of_int64(x) {

0 commit comments

Comments
 (0)