Skip to content

Commit a0b41f3

Browse files
vouillonOlivierNicole
authored andcommitted
Stdlib additions
1 parent e03d3aa commit a0b41f3

File tree

1 file changed

+58
-5
lines changed

1 file changed

+58
-5
lines changed

compiler/lib/stdlib.ml

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,28 @@ module Int32 = struct
341341
n
342342
end
343343

344+
module Int31 = struct
345+
let wrap i = Int32.(shift_right (shift_left i 1) 1)
346+
347+
let of_int_warning_on_overflow i =
348+
Int32.convert_warning_on_overflow
349+
~to_int32:(fun i -> wrap (Int32.of_int i))
350+
~of_int32:Int32.to_int
351+
~equal:Int_replace_polymorphic_compare.( = )
352+
~to_dec:(Printf.sprintf "%d")
353+
~to_hex:(Printf.sprintf "%x")
354+
i
355+
356+
let of_nativeint_warning_on_overflow n =
357+
Int32.convert_warning_on_overflow
358+
~to_int32:(fun i -> wrap (Nativeint.to_int32 i))
359+
~of_int32:Nativeint.of_int32
360+
~equal:Nativeint.equal
361+
~to_dec:(Printf.sprintf "%nd")
362+
~to_hex:(Printf.sprintf "%nx")
363+
n
364+
end
365+
344366
module Option = struct
345367
let map ~f x =
346368
match x with
@@ -571,6 +593,20 @@ module Bytes = struct
571593
include BytesLabels
572594

573595
let sub_string b ~pos:ofs ~len = unsafe_to_string (Bytes.sub b ofs len)
596+
597+
let fold_left ~f ~init b =
598+
let r = ref init in
599+
for i = 0 to length b - 1 do
600+
r := f !r (unsafe_get b i)
601+
done;
602+
!r
603+
604+
let fold_right ~f b ~init =
605+
let r = ref init in
606+
for i = length b - 1 downto 0 do
607+
r := f (unsafe_get b i) !r
608+
done;
609+
!r
574610
end
575611

576612
module String = struct
@@ -986,6 +1022,20 @@ module String = struct
9861022
| _ -> false
9871023
in
9881024
loop (length b - 1) b 0
1025+
1026+
let fold_left ~f ~init s =
1027+
let r = ref init in
1028+
for i = 0 to length s - 1 do
1029+
r := f !r (unsafe_get s i)
1030+
done;
1031+
!r
1032+
1033+
let fold_right ~f s ~init =
1034+
let r = ref init in
1035+
for i = length s - 1 downto 0 do
1036+
r := f (unsafe_get s i) !r
1037+
done;
1038+
!r
9891039
end
9901040

9911041
module Utf8_string : sig
@@ -1166,13 +1216,16 @@ module Filename = struct
11661216
in
11671217
try
11681218
let ch = open_out_bin f_tmp in
1169-
(try f ch
1170-
with e ->
1171-
close_out ch;
1172-
raise e);
1219+
let res =
1220+
try f ch
1221+
with e ->
1222+
close_out ch;
1223+
raise e
1224+
in
11731225
close_out ch;
11741226
(try Sys.remove file with Sys_error _ -> ());
1175-
Sys.rename f_tmp file
1227+
Sys.rename f_tmp file;
1228+
res
11761229
with exc ->
11771230
Sys.remove f_tmp;
11781231
raise exc

0 commit comments

Comments
 (0)