From 226290737594dc05f2759bc1abe7678e08b38d31 Mon Sep 17 00:00:00 2001 From: Jason Gross Date: Mon, 23 Jan 2023 14:15:27 -0500 Subject: [PATCH 1/3] Add some string matching utils --- src/helpers.ml | 23 +++++++++++++++++++---- src/helpers.mli | 7 +++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/helpers.ml b/src/helpers.ml index 20f91f12..fd62d2d4 100644 --- a/src/helpers.ml +++ b/src/helpers.ml @@ -2,11 +2,26 @@ open Base let f = Printf.sprintf +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 + +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 = - try - let _ = Str.search_forward (Str.regexp regexp) string 0 in - true - with Stdlib.Not_found -> false + 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 diff --git a/src/helpers.mli b/src/helpers.mli index f5e03878..77ccc9d4 100644 --- a/src/helpers.mli +++ b/src/helpers.mli @@ -1,5 +1,12 @@ val f : ('a, unit, string) format -> 'a +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 From a3b57d4b70b62e943884308c8a50c00641cb31a6 Mon Sep 17 00:00:00 2001 From: Jason Gross Date: Tue, 24 Jan 2023 02:11:25 -0500 Subject: [PATCH 2/3] 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 From 8d81a742c8a6bb3a161b5598687ac8adb104d6c9 Mon Sep 17 00:00:00 2001 From: Jason Gross Date: Tue, 24 Jan 2023 10:08:49 -0500 Subject: [PATCH 3/3] Fix execution order of `map_string_matches` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Gaƫtan Gilbert --- src/helpers.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helpers.ml b/src/helpers.ml index 3b65bbf5..b2ec671f 100644 --- a/src/helpers.ml +++ b/src/helpers.ml @@ -15,7 +15,7 @@ let rec fold_string_matches ~regexp ~f ~init ?(pos = 0) string = else init let map_string_matches ~regexp ~f string = - fold_string_matches ~regexp ~f:(fun rest -> f () :: rest ()) ~init:[] string + fold_string_matches ~regexp ~f:(fun rest -> let v = f () in v :: rest ()) ~init:[] string let iter_string_matches ~regexp ~f string = fold_string_matches ~regexp ~f:(fun rest -> f () ; rest ()) ~init:() string