From a3b57d4b70b62e943884308c8a50c00641cb31a6 Mon Sep 17 00:00:00 2001 From: Jason Gross Date: Tue, 24 Jan 2023 02:11:25 -0500 Subject: [PATCH] Slight refactoring for hopefully more readability --- src/helpers.ml | 23 ++++++++++------------- src/helpers.mli | 4 ++-- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/src/helpers.ml b/src/helpers.ml index fd62d2d4..3b65bbf5 100644 --- a/src/helpers.ml +++ b/src/helpers.ml @@ -2,17 +2,17 @@ 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 @@ -20,9 +20,6 @@ let map_string_matches ~regexp ~f 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") diff --git a/src/helpers.mli b/src/helpers.mli index 77ccc9d4..0af6284e 100644 --- a/src/helpers.mli +++ b/src/helpers.mli @@ -1,5 +1,7 @@ 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 @@ -7,8 +9,6 @@ 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