diff --git a/src/compilerlib/pb_parsing_util.ml b/src/compilerlib/pb_parsing_util.ml index a2c52ba5..19e96036 100644 --- a/src/compilerlib/pb_parsing_util.ml +++ b/src/compilerlib/pb_parsing_util.ml @@ -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 diff --git a/src/compilerlib/pb_util.ml b/src/compilerlib/pb_util.ml index 6b273dff..dbe15c86 100644 --- a/src/compilerlib/pb_util.ml +++ b/src/compilerlib/pb_util.ml @@ -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 diff --git a/src/compilerlib/pb_util.mli b/src/compilerlib/pb_util.mli index 55426f89..5dc120ba 100644 --- a/src/compilerlib/pb_util.mli +++ b/src/compilerlib/pb_util.mli @@ -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] @@ -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. *)