-
Notifications
You must be signed in to change notification settings - Fork 5
improve the memory footprint of is_prefix and is_suffix #36
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||||||
|
|
||||||
| 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 = | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this allocates a closure for |
||||||
| if i = len_suf then true | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| else aux (i + 1) | ||||||
| in | ||||||
| diff >= 0 && aux 0 | ||||||
|
|
||||||
| let cut sep str = | ||||||
| try | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.