Thread overview
[Issue 5970] New: format("%d", BigInt) problem
Aug 25, 2011
timon.gehr@gmx.ch
[Issue 5970] fix BigInt.toString
Aug 25, 2011
Don
Aug 25, 2011
Kenji Hara
May 09, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5970

           Summary: format("%d", BigInt) problem
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Keywords: rejects-valid
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody@puremagic.com
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2011-05-09 15:27:23 PDT ---
This is in reply to comment #3 of bug 5765

> > How do I perform the equivalent of str(ackermann(4, 2)) with BigInt?
> 
> format("%d", ackermann(4,2))

I think it doesn't work with DMD 2.053beta:


import std.bigint, std.string;
void main() {
    format("%d", BigInt(1));
}


It prints:
std.format.FormatError: std.format Can't convert std.bigint.BigInt to string:
"string toString()" not defined

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 25, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5970



--- Comment #1 from bearophile_hugs@eml.cc 2011-08-24 18:59:13 PDT ---
import std.bigint, std.conv;
void main() {
    string s = text(BigInt(1));
}


In DMD 2.055beta it gives:

...\src\phobos\std\conv.d(829): Error: function std.bigint.BigInt.toString
(void delegate(const(char)[]) sink, string formatString) const is not callable
using argument types ()
...\src\phobos\std\conv.d(829): Error: expected 2 function arguments, not 0


See also notes in bug 4122 :

My suggestion is to change the signature of BigInt.toString() from this:

void toString(void delegate(const (char)[]) sink, string formatString) const {


To something like this:

string toString(void delegate(string) sink=null, string formatString="d") const
{

And make it return a string filled with the decimal representation when sink is null; and to return an empty string when sink!=null.

--------

Eventually the signature can even become:

string toString(void delegate(string) sink=null, string formatString="d",
string thousands="") const {

So if thousands="_" the number gets represented as:

"100_000_000_000"

But this is less essential.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 25, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5970


timon.gehr@gmx.ch changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |timon.gehr@gmx.ch


--- Comment #2 from timon.gehr@gmx.ch 2011-08-24 19:38:02 PDT ---
alternatively, just provide another overload.

bi.toString(), where bi is a BigInt should just work and return a newly allocated string that represents bi.

(The current toString would actually better be called writeTo. Format string should default to null in each case.)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 25, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5970


Don <clugdbug@yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |clugdbug@yahoo.com.au


--- Comment #3 from Don <clugdbug@yahoo.com.au> 2011-08-25 01:27:40 PDT ---
(In reply to comment #0)
> This is in reply to comment #3 of bug 5765
> 
> > > How do I perform the equivalent of str(ackermann(4, 2)) with BigInt?
> > 
> > format("%d", ackermann(4,2))
> 
> I think it doesn't work with DMD 2.053beta:
> 
> 
> import std.bigint, std.string;
> void main() {
>     format("%d", BigInt(1));
> }
> 
> 
> It prints:
> std.format.FormatError: std.format Can't convert std.bigint.BigInt to string:
> "string toString()" not defined

You're right. writefln() works, but format() doesn't:

import std.bigint, std.stdio;
void main() {
    writefln("%d %x", BigInt(114), BigInt(114)); // works
}

A bit strange, since writefln() should really be using format().

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 25, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5970



--- Comment #4 from Kenji Hara <k.hara.pg@gmail.com> 2011-08-25 01:40:12 PDT ---
(In reply to comment #3)
> A bit strange, since writefln() should really be using format().

It is not true. std.string.format() still uses std.format.doFormat(), not
formattedWrite().
I think that is old feature.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 25, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5970



--- Comment #5 from bearophile_hugs@eml.cc 2011-08-25 01:53:06 PDT ---
See also bug 6448

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 25, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=5970


bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED


--- Comment #6 from bearophile_hugs@eml.cc 2012-04-24 19:11:22 PDT ---
This works correctly:

import std.bigint, std.string, std.stdio;
void main() {
    writeln(xformat("%d", BigInt(1)));
}


Change discussed here: https://github.com/D-Programming-Language/phobos/pull/231

So format() is to be considered obsolete (and eventually deprecated and
removed, I presume). So I close this issue.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------