Files
mercury/tests/hard_coded/string_index_ilseq.m
Peter Wang 47d0f70ea4 Define behaviour of string.index on ill-formed sequences.
library/string.m:
    Make string.index/3 and string.unsafe_index/3
    return either U+FFFD or an unpaired surrogate code point
    when an ill-formed code unit sequence is detected.

tests/hard_coded/Mmakefile:
tests/hard_coded/string_index_ilseq.exp:
tests/hard_coded/string_index_ilseq.exp2:
tests/hard_coded/string_index_ilseq.m:
    Add test case.
2019-10-24 09:14:46 +11:00

43 lines
1.2 KiB
Mathematica

%---------------------------------------------------------------------------%
% vim: ts=4 sw=4 et ft=mercury
%---------------------------------------------------------------------------%
%
% The .exp file is for backends using UTF-8 string encoding.
% The .exp2 file is for backends using UTF-16 string encoding.
%
%---------------------------------------------------------------------------%
:- module string_index_ilseq.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
%---------------------------------------------------------------------------%
:- implementation.
:- import_module char.
:- import_module list.
:- import_module string.
%---------------------------------------------------------------------------%
main(!IO) :-
S0 = "😀",
S1 = string.between(S0, 0, 1),
S = S0 ++ S1 ++ S0,
list.foldl(test_index(S), 0 .. count_code_units(S), !IO).
:- pred test_index(string::in, int::in, io::di, io::uo) is det.
test_index(S, Index, !IO) :-
( if string.index(S, Index, Char) then
io.format("string.index(S, %d, %#x)\n",
[i(Index), i(char.to_int(Char))], !IO)
else
io.format("string.index(S, %d, _) failed\n",
[i(Index)], !IO)
).