[m-rev.] for review: format ints with thousand separators
Ian MacLarty
maclarty at cs.mu.OZ.AU
Thu Feb 3 17:54:37 AEDT 2005
On Thu, Feb 03, 2005 at 05:21:16PM +1100, Zoltan Somogyi wrote:
> On 03-Feb-2005, Julien Fischer <juliensf at cs.mu.OZ.AU> wrote:
> > > +string__int_to_base_string_group_2(NegN, Base, Curr, Period, Sep, Str) :-
> > > + (
> > > + Curr = Period, Period > 0
> > > + ->
> > > + string__int_to_base_string_group_2(NegN, Base, 0, Period, Sep,
> > > + Str1),
> > > + string__append(Str1, Sep, Str)
>
> I think you need to document the meaning of the Curr and Period arguments.
>
Okay.
> > > + (
> > > + NegN > -Base
> > > + ->
> > > + N = -NegN,
> > > + char__det_int_to_digit(N, DigitChar),
> > > + string__char_to_string(DigitChar, Str)
> > > + ;
> > > + NegN1 = NegN // Base,
> > > + N10 = (NegN1 * Base) - NegN,
> > > + char__det_int_to_digit(N10, DigitChar),
> > > + string__char_to_string(DigitChar, DigitString),
> > > + string__int_to_base_string_group_2(NegN1, Base,
> > > + Curr + 1, Period, Sep, Str1),
> > > + string__append(Str1, DigitString, Str)
> > > + )
> > > + ).
>
> Noting both here and in int_to_base_string that this code is basically
> duplicated would be a good idea.
>
Okay.
Here's an interdiff:
diff -u library/string.m library/string.m
--- library/string.m 3 Feb 2005 05:50:13 -0000
+++ library/string.m 3 Feb 2005 06:47:53 -0000
@@ -123,11 +123,10 @@
%
:- func string__int_to_string_thousands(int) = string.
:- mode string__int_to_string_thousands(in) = uo is det.
-:- pred string__int_to_string_thousands(int, string).
-:- mode string__int_to_string_thousands(in, uo) is det.
% string__int_to_base_string(Int, Base, String):
- % Convert an integer to a string in a given Base (between 2 and 36).
+ % Convert an integer to a string in a given Base.
+ % An exception is thrown if Base is not between 2 and 36.
%
:- func string__int_to_base_string(int, int) = string.
:- mode string__int_to_base_string(in, in) = uo is det.
@@ -137,13 +136,13 @@
% string__int_to_base_string_group(Int, Base, GroupLength, Seperator,
% String):
% Convert an integer to a string in a given Base (between 2 and 36)
- % and insert Seperator between every GroupLength digits. Useful
- % for formatting numbers like "1,300,000".
+ % and insert Seperator between every GroupLength digits.
+ % If GroupLength is less than one then no seperators will appear in the
+ % output. An exception is thrown if Base is not between 2 and 36.
+ % Useful for formatting numbers like "1,300,000".
%
:- func string__int_to_base_string_group(int, int, int, string) = string.
:- mode string__int_to_base_string_group(in, in, in, in) = uo is det.
-:- pred string__int_to_base_string_group(int, int, int, string, string).
-:- mode string__int_to_base_string_group(in, in, in, in, uo) is det.
% Convert a float to a string.
% In the current implementation the resulting float will be in the
@@ -941,6 +940,10 @@
:- pred string__int_to_base_string_2(int, int, string).
:- mode string__int_to_base_string_2(in, in, uo) is det.
+ % string__int_to_base_string_2/3 is almost identical to
+ % string__int_to_base_string_group_2/6 below so any changes here might
+ % also need to be applied to string__int_to_base_string_group_2/3.
+ %
string__int_to_base_string_2(NegN, Base, Str) :-
(
NegN > -Base
@@ -960,16 +963,12 @@
string__from_char_list(CharList, Str) :-
string__to_char_list(Str, CharList).
-string__int_to_string_thousands(N) = Str :-
- string__int_to_base_string_group(N, 10, 3, ",", Str).
-
-string__int_to_string_thousands(N, Str) :-
- string__int_to_base_string_group(N, 10, 3, ",", Str).
+string__int_to_string_thousands(N) =
+ string__int_to_base_string_group(N, 10, 3, ",").
+ % Period is how many digits should be between each seperator.
+ %
string__int_to_base_string_group(N, Base, Period, Sep) = Str :-
- string__int_to_base_string_group(N, Base, Period, Sep, Str).
-
-string__int_to_base_string_group(N, Base, Period, Sep, Str) :-
(
Base >= 2, Base =< 36
->
@@ -982,6 +981,8 @@
:- pred string__int_to_base_string_group_1(int::in, int::in, int::in,
string::in, string::uo) is det.
+ % Period is how many digits should be between each seperator.
+ %
string__int_to_base_string_group_1(N, Base, Period, Sep, Str) :-
% Note that in order to handle MININT correctly,
% we need to do the conversion of the absolute
@@ -1002,6 +1003,13 @@
:- pred string__int_to_base_string_group_2(int::in, int::in, int::in, int::in,
string::in, string::uo) is det.
+ % Period is how many digits should be between each seperator.
+ % Curr is how many digits have been processed since the last seperator
+ % was inserted.
+ % string__int_to_base_string_group_2/6 is almost identical to
+ % string__int_to_base_string_2/3 above so any changes here might also
+ % need to be applied to string__int_to_base_string_2/3.
+ %
string__int_to_base_string_group_2(NegN, Base, Curr, Period, Sep, Str) :-
(
Curr = Period, Period > 0
Ian.
--------------------------------------------------------------------------
mercury-reviews mailing list
post: mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------
More information about the reviews
mailing list