Skip to content
Closed
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
27 changes: 17 additions & 10 deletions src/lib.ml
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
module String = struct
let is_prefix ~prefix str =
let pl = String.length prefix in
if String.length str < pl then
false
else
String.sub str 0 (String.length prefix) = prefix
let len_s = String.length str
and len_pre = String.length prefix in
let rec aux i =
if i = len_pre then true
else if String.unsafe_get str i <> String.unsafe_get prefix i then false
else aux (i + 1)
in
len_s >= len_pre && aux 0

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
len_s >= len_pre && aux 0
len_s >= (len_pre : int) && aux 0


let is_suffix ~suffix str =
let pl = String.length suffix in
if String.length str < pl then
false
else
String.sub str (String.length str - pl) pl = suffix
let len_s = String.length str
and len_suf = String.length suffix in
let diff = len_s - len_suf in
let rec aux i =

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this allocates a closure for str, diff, suffix and len_suf

if i = len_suf then true

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if i = len_suf then true
if Int.equal i len_suf then true

else if String.unsafe_get str (diff + i) <> String.unsafe_get suffix i then false

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
else if String.unsafe_get str (diff + i) <> String.unsafe_get suffix i then false
else if not (Char.equal (String.unsafe_get str (diff + i)) (String.unsafe_get suffix i) then false

else aux (i + 1)
in
diff >= 0 && aux 0

let cut sep str =
try
Expand Down