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

Julien Fischer jfischer at opturion.com
Mon Jul 25 17:01:46 AEST 2022


On Mon, 25 Jul 2022, Peter Wang wrote:

> On Mon, 25 Jul 2022 16:14:55 +1000 Julien Fischer <jfischer at opturion.com> wrote:
>>
>> On Mon, 25 Jul 2022, Peter Wang wrote:
>>
>>> On Mon, 25 Jul 2022 15:38:10 +1000 Julien Fischer <jfischer at opturion.com> wrote:
>>
>>>>   %---------------------%
>>>>
>>>> +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.
>>
>> I don't have any objections to doing that, however it would not be
>> consistent with how all_match/2 and contains_char/2 treat
>> replaced_code_unit().
>
> Hmm, I don't know what I was thinking when I updated those predicates,
> if I was thinking at all.

Maybe you were thinking that they are only intended to be called on
strings that do not contain ill-formed sequences?

> It seems clear that:
>  - all_match should fail if the string contains any ill-formed sequences
>  - contains_char should succeed as long as the string contains the char
>    being tested for, even if it contains an ill-formed sequence or
>    anything else

Ok, but what if I am checking if the string contains the replacment
character (i.e. should these test predicates treat ill-formed sequences
as a replacment character)?

For now I will alter my change to keep on searching after it
encounters an ill-formed sequence.

Julien.


More information about the reviews mailing list