mirror of
https://github.com/Mercury-Language/mercury.git
synced 2026-04-17 18:33:58 +00:00
Currently, the Mercury implementation of string formatting handles uints by
casting them to ints and then using the code for formatting signed integers as
unsigned values. Add an implementation that works directly on uints and make
the code that formats signed integers as unsigned integers use that instead.
The new implementation is simpler and avoids unnecessary conversions to
arbitrary precision integers.
Add new functions for converting uint values directly to octal and hexadecimal
strings that use functionality provided by the underlying platforms; replace
the Mercury code that previously did that with calls to these new functions.
library/string.m:
Add the functions uint_to_hex_string/1, uint_to_uc_hex_string/1 and
uint_to_octal_string/1.
library/string.format.m:
Make format_uint/6 operate directly on uints instead of casting the value
to a signed int and calling format_unsigned_int/6.
Make format_unsigned_int/6 cast the int value to a uint and then call
format_uint/6.
Delete predicates and functions used to convert ints to octal and
hexadecimal strings. We now just use the functions exported by
the string module.
NEWS:
Announce the additions to the string module.
tests/hard_coded/Mmakefile:
tests/hard_coded/uint_string_conv.{m,exp*}:
Add a test of uint string conversion.
72 lines
1.5 KiB
Mathematica
72 lines
1.5 KiB
Mathematica
%---------------------------------------------------------------------------%
|
|
% vim: ft=mercury ts=4 sw=4 et
|
|
%---------------------------------------------------------------------------%
|
|
% Test conversion of uints to strings.
|
|
% The .exp file is from systems where uint is 32 bit.
|
|
% The .exp2 file is for systems wwhere uint is 64 bit.
|
|
%---------------------------------------------------------------------------%
|
|
|
|
:- module uint_string_conv.
|
|
:- interface.
|
|
|
|
:- import_module io.
|
|
|
|
:- pred main(io::di, io::uo) is det.
|
|
|
|
%---------------------------------------------------------------------------%
|
|
%---------------------------------------------------------------------------%
|
|
|
|
:- implementation.
|
|
|
|
:- import_module list.
|
|
:- import_module string.
|
|
:- import_module uint.
|
|
|
|
main(!IO) :-
|
|
io.format("%-22s %-24s %-22s%-22s\n", [s("Decimal"), s("Octal"), s("Hex"),
|
|
s("HEX")], !IO),
|
|
list.foldl(do_test, test_values, !IO),
|
|
io.nl(!IO).
|
|
|
|
:- pred do_test( uint, io, io).
|
|
:- mode do_test(in, di, uo) is det.
|
|
|
|
do_test(U, !IO) :-
|
|
Decimal = uint_to_string(U),
|
|
Octal = uint_to_octal_string(U),
|
|
Hex = uint_to_hex_string(U),
|
|
HexUC = uint_to_uc_hex_string(U),
|
|
io.format("%-22s %-24s %-22s%-22s\n",
|
|
[s(Decimal), s(Octal), s(Hex), s(HexUC)], !IO).
|
|
|
|
:- func test_values = list(uint).
|
|
|
|
test_values = [
|
|
0u,
|
|
1u,
|
|
2u,
|
|
3u,
|
|
4u,
|
|
7u,
|
|
8u,
|
|
9u,
|
|
10u,
|
|
11u,
|
|
12u,
|
|
13u,
|
|
14u,
|
|
15u,
|
|
16u,
|
|
32u,
|
|
64u,
|
|
127u,
|
|
128u,
|
|
255u,
|
|
256u,
|
|
32767u,
|
|
65535u,
|
|
2147483647u,
|
|
4294967295u,
|
|
uint.max_uint
|
|
].
|