ADDED: format specifier ~NL to limit the number of digits per line

Example:

    %?- format("~65L", [2^1000]).
    %@ 10715086071862673209484250490600018105614048117055336074437503883_
    %@ 70351051124936122493198378815695858127594672917553146825187145285_
    %@ 69231404359845775746985748039345677748242309854210746050623711418_
    %@ 77954182153046474983581941267398767559165543946077062914571196477_
    %@ 686542167660429831652624386837205668069376   true.
This commit is contained in:
Markus Triska
2022-01-10 17:29:45 +01:00
parent f5952088e3
commit 2889a4438b

View File

@@ -29,6 +29,8 @@
~ND like ~Nd, separating digits to the left of the decimal point ~ND like ~Nd, separating digits to the left of the decimal point
in groups of three, using the character "," (comma) in groups of three, using the character "," (comma)
~NU like ~ND, using "_" (underscore) to separate groups of digits ~NU like ~ND, using "_" (underscore) to separate groups of digits
~NL format an integer so that at most N digits appear on a line.
If N is 0 or omitted, it defaults to 72.
~Nr where N is an integer between 2 and 36: format the ~Nr where N is an integer between 2 and 36: format the
next argument, which must be an integer, in radix N. next argument, which must be an integer, in radix N.
The characters "a" to "z" are used for radices 10 to 36. The characters "a" to "z" are used for radices 10 to 36.
@@ -208,6 +210,16 @@ cells([~|Fs0], Args0, Tab, Es, VNs) -->
!, !,
{ separate_digits_fractional(Arg, '_', Num, Cs) }, { separate_digits_fractional(Arg, '_', Num, Cs) },
cells(Fs, Args, Tab, [chars(Cs)|Es], VNs). cells(Fs, Args, Tab, [chars(Cs)|Es], VNs).
cells([~|Fs0], Args0, Tab, Es, VNs) -->
{ numeric_argument(Fs0, Num0, ['L'|Fs], Args0, [Arg|Args]) },
!,
{ ( Num0 =:= 0 ->
Num = 72
; Num = Num0
),
phrase(format_("~d", [Arg]), Cs0),
phrase(split_lines_width(Cs0, Num), Cs) },
cells(Fs, Args, Tab, [chars(Cs)|Es], VNs).
cells([~,i|Fs], [_|Args], Tab, Es, VNs) --> !, cells([~,i|Fs], [_|Args], Tab, Es, VNs) --> !,
cells(Fs, Args, Tab, Es, VNs). cells(Fs, Args, Tab, Es, VNs).
cells([~,n|Fs], Args, Tab, Es, VNs) --> !, cells([~,n|Fs], Args, Tab, Es, VNs) --> !,
@@ -328,6 +340,14 @@ upto_what([], _) --> [].
groups_of_three([A,B,C,D|Rs], Sep) --> !, [A,B,C,Sep], groups_of_three([D|Rs], Sep). groups_of_three([A,B,C,D|Rs], Sep) --> !, [A,B,C,Sep], groups_of_three([D|Rs], Sep).
groups_of_three(Ls, _) --> seq(Ls). groups_of_three(Ls, _) --> seq(Ls).
split_lines_width(Cs, Num) -->
( { length(Prefix, Num),
append(Prefix, [R|Rs], Cs) } ->
seq(Prefix), "_\n",
split_lines_width([R|Rs], Num)
; seq(Cs)
).
cell(From, To, Es0) --> cell(From, To, Es0) -->
( { Es0 == [] } -> [] ( { Es0 == [] } -> []
; { reverse(Es0, Es) }, ; { reverse(Es0, Es) },