Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use efficient String.starts_with implementation #912

Merged
merged 1 commit into from
Oct 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/lib/util/compat.ml
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
(* Locally restore the default polymorphic operators of the Stdlib, to make it
easier to copy code from there without modifications. *)
open Stdlib

module String = struct
open Stdlib.String

let starts_with ~prefix s =
length s >= length prefix &&
equal (sub s 0 (length prefix)) prefix
(* Copied from stdlib/string.ml in OCaml 5.0 *)
let len_s = length s
and len_pre = length prefix in
let rec aux i =
if i = len_pre then true
else if unsafe_get s i <> unsafe_get prefix i then false
else aux (i + 1)
in len_s >= len_pre && aux 0

let fold_left f x a =
(* Copied from stdlib/bytes.ml in OCaml 5.0 *)
let r = ref x in
for i = 0 to length a - 1 do
r := f !r (unsafe_get a i)
Expand Down
Loading