Files
mercury/tests/hard_coded/string_contains_match.m
Julien Fischer ae0525af53 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.
2022-07-25 19:38:01 +10:00

62 lines
2.3 KiB
Mathematica

%---------------------------------------------------------------------------%
% vim: ts=4 sw=4 et ft=mercury
%---------------------------------------------------------------------------%
% A test of string.contains_match/2.
%---------------------------------------------------------------------------%
:- module string_contains_match.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
:- implementation.
:- import_module char.
:- import_module int.
:- import_module list.
:- import_module string.
%---------------------------------------------------------------------------%
main(!IO) :-
test_contains_match(is_whitespace, "is_whitespace", "", !IO),
test_contains_match(is_whitespace, "is_whitespace", "abc", !IO),
test_contains_match(is_whitespace, "is_whitespace", " abc", !IO),
test_contains_match(is_whitespace, "is_whitespace", "a bc", !IO),
test_contains_match(is_whitespace, "is_whitespace", "abc ", !IO),
test_contains_match(is_whitespace, "is_whitespace",
"μῆνιν ἄειδε θεὰ Πηληϊάδεω Ἀχιλῆος", !IO),
test_contains_match(is_chess_piece, "is_chess_piece",
"♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜", !IO),
test_contains_match(is_chess_piece, "is_chess_piece",
"abc♜ def", !IO),
test_contains_match(is_chess_piece, "is_chess_piece",
"no chess pieces here!", !IO).
:- pred test_contains_match(pred(char)::in(pred(in) is semidet),
string::in, string::in, io::di, io::uo) is det.
test_contains_match(Pred, PredName, String, !IO) :-
( if string.contains_match(Pred, String) then
Result = "true"
else
Result = "false"
),
io.format("contains_match(%s, %s) ==> %s.\n",
[s(PredName), s(string(String)), s(Result)], !IO).
:- pred is_chess_piece(char::in) is semidet.
is_chess_piece(Char) :-
char.to_int(Char, CodePoint),
CodePoint >= 0x2654, CodePoint =< 0x265F.
%---------------------------------------------------------------------------%
:- end_module string_contains_match.
%---------------------------------------------------------------------------%