Files
mercury/tests/hard_coded/string_contains_char.m
Peter Wang 2ff6b119ef Fix some handling of ill-formed sequences in string module.
library/string.m:
    Fix string.all_match to fail if the string being tested contains
    any ill-formed code unit sequences.

    Fix the Mercury implementation of string.contains_char to continue
    searching for the character past any ill-formed code unit sequences.
    The foreign code implementations already did so.

    Fix unsafe_index_next_repl and unsafe_prev_index_repl in C grades.
    We indexed the C string with `ReplacedCodeUnit = Str[Index]' but
    since Mercury strings have the C type `char *', ReplacedCodeUnit
    could take on a negative value. When ReplacedCodeUnit == -1,
    the caller would assume there is a valid encoding of a code point
    beginning at Index, and therefore return `not_replaced' instead of
    `replaced_code_unit(255)'.

    Add casts to `unsigned char' in other places where we index C
    strings.

    Clarify documentation.

    Document that string.duplicate_char may throw an exception.

tests/hard_coded/string_all_match.m:
tests/hard_coded/string_all_match.exp:
tests/hard_coded/string_all_match.exp2:
    Add test for string.all_match.

tests/hard_coded/contains_char_2.m:
tests/hard_coded/contains_char_2.exp:
    Delete older test case for string.contains_char.

tests/hard_coded/string_contains_char.m:
tests/hard_coded/string_contains_char.exp:
tests/hard_coded/string_contains_char.exp2:
    Add better test for string.contains_char.

tests/hard_coded/Mmakefile:
    Enable the tests.

NEWS:
    Announce the changes.
2022-07-27 14:56:49 +10:00

64 lines
2.0 KiB
Mathematica

%---------------------------------------------------------------------------%
% vim: ts=4 sw=4 et ft=mercury
%---------------------------------------------------------------------------%
% A test of string.contains_char/2.
%
% The .exp file is for C and C# grades.
% The .exp2 file is for Java grades.
%
%---------------------------------------------------------------------------%
:- module string_contains_char.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
:- implementation.
:- import_module char.
:- import_module list.
:- import_module string.
%---------------------------------------------------------------------------%
main(!IO) :-
test_contains_char("", 'X', !IO),
test_contains_char("abc", 'X', !IO),
test_contains_char("abc", 'a', !IO),
test_contains_char("abc", 'b', !IO),
test_contains_char("abc", 'c', !IO),
test_contains_char("aßξ啕𐀀.", 'ß', !IO),
test_contains_char("aßξ啕𐀀.", 'ξ', !IO),
test_contains_char("aßξ啕𐀀.", '', !IO),
test_contains_char("aßξ啕𐀀.", '.', !IO),
test_contains_char("aßξ啕𐀀.", '', !IO),
Ilseq = string.between("😀", 0, 1),
S = "abc" ++ Ilseq ++ "123",
test_contains_char(S, 'c', !IO),
test_contains_char(S, '1', !IO),
test_contains_char(S, '2', !IO),
test_contains_char(S, '3', !IO),
test_contains_char(S, '\uFFFD', !IO).
:- pred test_contains_char(string::in, char::in, io::di, io::uo) is det.
test_contains_char(String, Char, !IO) :-
( if string.contains_char(String, Char) then
Result = "true"
else
Result = "false"
),
io.format("contains_char(%s, %s) ==> %s.\n",
[s(string(String)), s(string(Char)), s(Result)], !IO).
%---------------------------------------------------------------------------%
:- end_module string_contains_char.
%---------------------------------------------------------------------------%