Skip to content

Commit

Permalink
String.starts_with...
Browse files Browse the repository at this point in the history
  • Loading branch information
Lupus committed Mar 8, 2024
1 parent 86dd22c commit 5f3beb0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/compilerlib/pb_parsing_util.ml
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ let option_list items = Pb_option.List_literal items

let option_name_of_ident ident =
let ident =
if String.starts_with ~prefix:"." ident then
if Pb_util.String.starts_with ~prefix:"." ident then
String.sub ident 1 (String.length ident - 1)
else
ident
Expand Down
16 changes: 16 additions & 0 deletions src/compilerlib/pb_util.ml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ let string_fold_lefti f e0 s =
in
loop e0 0

module String = struct
include String

let starts_with ~prefix s =
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
end

module Option = struct
let default x = function
| Some y -> y
Expand Down
9 changes: 9 additions & 0 deletions src/compilerlib/pb_util.mli
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ val string_fold_lefti : ('a -> int -> char -> 'a) -> 'a -> string -> 'a
val indentation_prefix : int -> string
(** [indentation_prefix level] returns a string of [2 * level] spaces *)

module String : sig
val starts_with :
prefix:(* comment thwarts tools/sync_stdlib_docs *) string -> string -> bool
(** [starts_with ][~prefix s] is [true] if and only if [s] starts with
[prefix].
*)
end

module Option : sig
val default : 'a -> 'a option -> 'a
(** [option_default x o] returns [x] is [o] is [None] otherwise [y]
Expand Down Expand Up @@ -93,6 +101,7 @@ module List : sig
Note: the [eq] function may be called even if the
lists have different length. If you know your equality
function is costly, you may want to check {!compare_lengths}
first.
*)
Expand Down

0 comments on commit 5f3beb0

Please sign in to comment.