Files
mercury/tests/hard_coded/string_append_ooi_ilseq.m
Peter Wang 78da14c581 Add string indexing predicates that indicate a code unit was replaced.
library/string.m:
    Add index_next_repl, unsafe_index_next_repl, prev_index_repl,
    unsafe_prev_index_repl predicates that return an indication if a
    replacement character was returned because an ill-formed code unit
    sequence was encountered.

    Add more pragma inlines for indexing predicates.

    Remove may_not_duplicate attribute on the Erlang version of
    unsafe_prev_index_repl, which would conflict with the pragma inline
    declaration. This requires the helper function do_unsafe_prev_index
    to be exported.

tests/hard_coded/string_append_ooi_ilseq.m:
tests/hard_coded/string_set_char_ilseq.m:
    Use index_next_repl in test cases.

NEWS:
    Announce additions.
2019-11-19 14:23:15 +11:00

83 lines
2.4 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_append_ooi_ilseq.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is cc_multi.
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
:- implementation.
:- import_module char.
:- import_module int.
:- import_module list.
:- import_module pair.
:- import_module solutions.
:- import_module string.
:- import_module uint8.
%---------------------------------------------------------------------------%
main(!IO) :-
S0 = "😀",
S1 = string.between(S0, 0, count_code_units(S0) - 1),
S = S0 ++ S1 ++ "z",
unsorted_aggregate(test_append_ooi(S), write_result, !IO).
:- pred test_append_ooi(string::in, pair(string, string)::out) is multi.
test_append_ooi(S, L - R) :-
string.append(L, R, S).
:- pred write_result(pair(string, string)::in, io::di, io::uo) is det.
write_result(L - R, !IO) :-
io.write_string("L: ", !IO),
write_string_debug(L, !IO),
io.write_string("\n", !IO),
io.write_string("R: ", !IO),
write_string_debug(R, !IO),
io.write_string("\n\n", !IO).
:- pred write_string_debug(string::in, io::di, io::uo) is det.
write_string_debug(S, !IO) :-
write_string_debug_loop(S, 0, !IO).
:- pred write_string_debug_loop(string::in, int::in, io::di, io::uo) is det.
write_string_debug_loop(S, Index, !IO) :-
( if string.index_next_repl(S, Index, NextIndex, Char, MaybeReplaced) then
(
MaybeReplaced = replaced_code_unit(CodeUnit),
write_hex(uint8.to_int(CodeUnit), !IO)
;
MaybeReplaced = not_replaced,
( if is_surrogate(Char) then
write_hex(char.to_int(Char), !IO)
else
io.write_char(Char, !IO)
)
),
io.write_char(' ', !IO),
write_string_debug_loop(S, NextIndex, !IO)
else
true
).
:- pred write_hex(int::in, io::di, io::uo) is det.
write_hex(I, !IO) :-
io.format("%#x", [i(I)], !IO).