diff --git a/src/dune/lib_name.ml b/src/dune/lib_name.ml index 9f19a41bc6fc..7687e05326b1 100644 --- a/src/dune/lib_name.ml +++ b/src/dune/lib_name.ml @@ -31,17 +31,15 @@ module Local = struct let hint_valid = Some (fun name -> - String.to_seq name - |> Seq.filter_map ~f:(fun c -> - if valid_char c then - Some c - else - match c with - | '.' - | '-' -> - Some '_' - | _ -> None) - |> String.of_seq) + String.filter_map name ~f:(fun c -> + if valid_char c then + Some c + else + match c with + | '.' + | '-' -> + Some '_' + | _ -> None)) let of_string_opt (name : string) = match name with diff --git a/src/dune/module_name.ml b/src/dune/module_name.ml index 6376dff73d82..fda93d53a737 100644 --- a/src/dune/module_name.ml +++ b/src/dune/module_name.ml @@ -28,18 +28,15 @@ include Stringlike.Make (struct let hint_valid = Some (fun name -> - String.to_seq name - |> Seq.filter_map ~f:(fun c -> - if valid_char c then - Some c - else - match c with - | '.' - | '-' -> - Some '_' - | _ -> None - ) - |> String.of_seq) + String.filter_map name ~f:(fun c -> + if valid_char c then + Some c + else + match c with + | '.' + | '-' -> + Some '_' + | _ -> None)) let is_valid_module_name name = match name with diff --git a/src/stdune/string.ml b/src/stdune/string.ml index f2b9fe6563a6..42a8da2243eb 100644 --- a/src/stdune/string.ml +++ b/src/stdune/string.ml @@ -305,3 +305,7 @@ let of_list chars = let s = Bytes.make (List.length chars) '0' in List.iteri chars ~f:(fun i c -> Bytes.set s i c); Bytes.to_string s + +let filter_map t ~f = + (* TODO more efficient implementation *) + to_seq t |> Seq.filter_map ~f |> of_seq diff --git a/src/stdune/string.mli b/src/stdune/string.mli index ab3ebfcbb1c7..429bf261377b 100644 --- a/src/stdune/string.mli +++ b/src/stdune/string.mli @@ -128,3 +128,5 @@ val need_quoting : string -> bool (** [quote_for_shell s] quotes [s] using [Filename.quote] if [need_quoting s] is [true] *) val quote_for_shell : string -> string + +val filter_map : string -> f:(char -> char option) -> string