Files
mercury/tests/hard_coded/string_well_formed.m
Peter Wang ee001d38b4 Add string.is_well_formed predicate.
library/string.m:
    Add predicate to test if a string is in UTF-8 or UTF-16,
    depending on the target language.

NEWS:
    Announce the addition.

tests/hard_coded/string_well_formed.exp:
tests/hard_coded/string_well_formed.m:
    Add basic test case.

tests/hard_coded/string_well_formed_utf8.exp:
tests/hard_coded/string_well_formed_utf8.exp2:
tests/hard_coded/string_well_formed_utf8.exp3:
tests/hard_coded/string_well_formed_utf8.inp:
tests/hard_coded/string_well_formed_utf8.m:
    Add more thorough test for UTF-8. The input file is from
    https://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt

tests/hard_coded/Mmakefile:
    Enable the tests.
2019-09-13 15:51:02 +10:00

44 lines
1.3 KiB
Mathematica

%---------------------------------------------------------------------------%
% vim: ts=4 sw=4 et ft=mercury
%---------------------------------------------------------------------------%
:- module string_well_formed.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
:- implementation.
:- import_module int.
:- import_module string.
%---------------------------------------------------------------------------%
main(!IO) :-
Empty = "",
Good = "\U0001F600", % 4 UTF-8 code units, 2 UTF-16 code units
Bad1 = string.between(Good, 0, 1),
Bad2 = string.between(Good, 0, length(Good) - 1),
Bad3 = Bad1 ++ Good,
Bad4 = Good ++ Bad2,
test_well_formed("Empty", Empty, !IO),
test_well_formed("Good", Good, !IO),
test_well_formed("Bad1", Bad1, !IO),
test_well_formed("Bad2", Bad2, !IO),
test_well_formed("Bad3", Bad3, !IO),
test_well_formed("Bad4", Bad4, !IO).
:- pred test_well_formed(string::in, string::in, io::di, io::uo) is det.
test_well_formed(Label, S, !IO) :-
( if string.is_well_formed(S) then
io.write_string(Label ++ " is well-formed\n", !IO)
else
io.write_string(Label ++ " is not well-formed\n", !IO)
).