From 3f13a252e1e074ad5b2934e7731eb4d1ca3aeb59 Mon Sep 17 00:00:00 2001 From: Olivier Nicole Date: Wed, 31 Jan 2024 18:19:47 +0100 Subject: [PATCH] Add Stdlib.String.fold_{left,right} to build on OCaml < 4.13 --- compiler/lib/stdlib.ml | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/compiler/lib/stdlib.ml b/compiler/lib/stdlib.ml index a44be69d2..7afd1cd32 100644 --- a/compiler/lib/stdlib.ml +++ b/compiler/lib/stdlib.ml @@ -589,6 +589,20 @@ module Bytes = struct include BytesLabels let sub_string b ~pos:ofs ~len = unsafe_to_string (Bytes.sub b ofs len) + + let fold_left ~f ~init b = + let r = ref init in + for i = 0 to length b - 1 do + r := f !r (unsafe_get b i) + done; + !r + + let fold_right ~f b ~init = + let r = ref init in + for i = length b - 1 downto 0 do + r := f (unsafe_get b i) !r + done; + !r end module String = struct @@ -998,6 +1012,10 @@ module String = struct | _ -> false in loop (length b - 1) b 0 + + let fold_left ~f ~init s = Bytes.fold_left ~f ~init (Bytes.unsafe_of_string s) + + let fold_right ~f s ~init = Bytes.fold_right ~f ~init (Bytes.unsafe_of_string s) end module Utf8_string : sig