[m-rev.] for review: add string.contains_match/2

Peter Wang novalazy at gmail.com
Mon Jul 25 16:05:43 AEST 2022


On Mon, 25 Jul 2022 15:38:10 +1000 Julien Fischer <jfischer at opturion.com> wrote:
> 
> For review by anyone.
> 
> ---------------------
> 
> Add string.contains_match/2.
> 
> Add a new predicate that tests if string contains any characters that succeed
> for a given test predicate.
> 
> library/string.m:
>      Add the new predicate.
> 
> compiler/options.m:
>      Replace the predicate string_contains_whitespace/1 defined here
>      with a call to the new library predicate.
> 
> NEWS:
>      Announce the new predicate.
> 
> tests/hard_coded/Mmakefile:
> tests/hard_coded/string_contains_match.{m,exp}:
>      Add a test of the new predicate.
> 
> Julien.
> 

> @@ -3292,6 +3300,22 @@ all_match_loop(P, String, Cur) :-
> 
>   %---------------------%
> 
> +contains_match(P, String) :-
> +    contains_match_loop(P, String, 0).
> +
> +:- pred contains_match_loop(pred(char)::in(pred(in) is semidet), string::in,
> +    int::in) is semidet.
> +
> +contains_match_loop(P, String, Cur) :-
> +    unsafe_index_next_repl(String, Cur, Next, Char, not_replaced),
> +    ( if P(Char) then
> +        true
> +    else
> +        contains_match_loop(P, String, Next)
> +    ).

If unsafe_index_next_repl returns replaced_code_unit(_) then P should
not succeed, but I think the loop should continue to test the rest of
the string.

Otherwise, that's fine.

Peter


More information about the reviews mailing list