Skip to content

Commit

Permalink
Shim String.starts_with
Browse files Browse the repository at this point in the history
As it's only available since 4.13.
  • Loading branch information
Andrey Popp committed Jun 8, 2024
1 parent 7ad3718 commit 5fd865b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
17 changes: 17 additions & 0 deletions src/cmdliner_base.ml
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,20 @@ let env_bool_parse s = match String.lowercase_ascii s with
| s ->
let alts = alts_str ~quoted:true ["true"; "yes"; "false"; "no" ] in
`Error (err_invalid_val s alts)

let string_has_prefix ~prefix s =
let prefix_len = String.length prefix in
let s_len = String.length s in
if prefix_len > s_len then false else
let rec loop i =
if i = prefix_len then true
else if String.get prefix i = String.get s i then loop (i + 1)
else false
in
loop 0

let string_drop_prefix ~prefix s =
if string_has_prefix ~prefix s then
let drop = String.length prefix in
Some (String.sub s drop (String.length s - drop))
else None
4 changes: 4 additions & 0 deletions src/cmdliner_base.mli
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,8 @@ val t4 :
('a * 'b * 'c * 'd) conv

val env_bool_parse : bool parser

val is_space : char -> bool

val string_has_prefix : prefix:string -> string -> bool
val string_drop_prefix : prefix:string -> string -> string option
6 changes: 1 addition & 5 deletions src/cmdliner_cline.ml
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,7 @@ let hint_matching_opt optidx s =
let complete_prefix = "+cmdliner_complete:"

let maybe_complete_token s =
if String.starts_with ~prefix:complete_prefix s
then
let drop = String.length complete_prefix in
Some (String.sub s drop (String.length s - drop))
else None
Cmdliner_base.string_drop_prefix ~prefix:complete_prefix s

exception Completion_requested of
string * [ `Opt of Cmdliner_info.Arg.t | `Arg of Cmdliner_info.Arg.t | `Any ]
Expand Down
2 changes: 1 addition & 1 deletion src/cmdliner_eval.ml
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ module Complete = struct
Printf.ksprintf print_endline fmt

let item ~prefix (name, doc) =
if String.starts_with ~prefix name then (
if Cmdliner_base.string_has_prefix ~prefix name then (
print_endline "item";
print_endline name;
let doc = String.map (fun c -> if Cmdliner_base.is_space c then ' ' else c) doc in
Expand Down

0 comments on commit 5fd865b

Please sign in to comment.