Skip to content

Commit

Permalink
Merge pull request #21 from OlivierNicole/fix-14
Browse files Browse the repository at this point in the history
Add Stdlib.String.fold_{left,right} to build on OCaml < 4.13
  • Loading branch information
vouillon authored Feb 19, 2024
2 parents b20bbd6 + 3f13a25 commit 0e151ea
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions compiler/lib/stdlib.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0e151ea

Please sign in to comment.