Skip to content

Commit

Permalink
Slight refactoring for hopefully more readability
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonGross committed Jan 24, 2023
1 parent 2262907 commit a3b57d4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
23 changes: 10 additions & 13 deletions src/helpers.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,24 @@ open Base

let f = Printf.sprintf

let string_match ~regexp ?(pos = 0) string =
try
let (_ : int) = Str.search_forward (Str.regexp regexp) string pos in
true
with Stdlib.Not_found -> false

let rec fold_string_matches ~regexp ~f ~init ?(pos = 0) string =
( try
let _ = Str.search_forward (Str.regexp regexp) string pos in
true
with Stdlib.Not_found -> false )
|> function
| true ->
let pos = Str.match_end () in
f (fun () -> fold_string_matches ~regexp ~f ~init ~pos string)
| false ->
init
if string_match ~regexp ~pos string then
let pos = Str.match_end () in
f (fun () -> fold_string_matches ~regexp ~f ~init ~pos string)
else init

let map_string_matches ~regexp ~f string =
fold_string_matches ~regexp ~f:(fun rest -> f () :: rest ()) ~init:[] string

let iter_string_matches ~regexp ~f string =
fold_string_matches ~regexp ~f:(fun rest -> f () ; rest ()) ~init:() string

let string_match ~regexp string =
fold_string_matches ~regexp ~f:(fun _rest -> true) ~init:false string

let pr_from_branch branch =
if string_match ~regexp:"^pr-\\([0-9]*\\)$" branch then
(Some (Str.matched_group 1 branch |> Int.of_string), "pull request")
Expand Down
4 changes: 2 additions & 2 deletions src/helpers.mli
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
val f : ('a, unit, string) format -> 'a

val string_match : regexp:string -> ?pos:int -> string -> bool

val fold_string_matches :
regexp:string -> f:((unit -> 'a) -> 'a) -> init:'a -> ?pos:int -> string -> 'a

val map_string_matches : regexp:string -> f:(unit -> 'a) -> string -> 'a list

val iter_string_matches : regexp:string -> f:(unit -> unit) -> string -> unit

val string_match : regexp:string -> string -> bool

val pr_from_branch : string -> int option * string

val first_line_of_string : string -> string
Expand Down

0 comments on commit a3b57d4

Please sign in to comment.