D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4122 - More handy BigInt.toString()
Summary: More handy BigInt.toString()
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-04-24 16:09 UTC by bearophile_hugs
Modified: 2015-06-09 05:13 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2010-04-24 16:09:02 UTC
The toString() of BigInt is not handy to print bigintegers during debugging. I have had to create a function like:

const(char)[] bigIntRepr(BigInt i) {
    const(char)[] result;
    i.toString((const(char)[] s){ result = s; }, "d");
    return result;
}


Note that this doesn't work (Access violation, I don't know why):

const(char)[] bigIntRepr(BigInt i) {
    const(char)[] result;
    i.toString((const(char)[] s){ result = s; }, null);
    return result;
}


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 important.
Comment 1 bearophile_hugs 2010-11-19 10:06:24 UTC
See:
http://www.dsource.org/projects/phobos/changeset/2183
Comment 2 Don 2011-03-18 23:33:50 UTC
Marking this as fixed, since you can just use writefln() in 2.051 and later.
(as added in the changeset).