Thread overview
[Issue 4165] New: std.conv.to!int doesn't ignore whitespace
Aug 15, 2010
David Simcha
May 09, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4165

           Summary: std.conv.to!int doesn't ignore whitespace
           Product: D
           Version: future
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody@puremagic.com
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2010-05-09 05:20:45 PDT ---
Too much flexibility in a language and its libs causes several problems, but the opposite too makes the language and its use fussy. So there is a right intermediate point of balance to be found.

The conversion from string to numbers performed by std.conv.to is excessively rigid.

In Python this conversion works:

>>> int(" 0123\n")
123


In D v.2.043 the same produces an error:

import std.conv: to;
void main() {
    int x = to!int(" 0123\n");
}

std.conv.ConvError: Can't convert value ` 0123
' of type const(char)[] to type int


Can std.conv.to be modified to ignore whitespace?
In particular the ending newline is very common, for example every time I want
to convert a line from a text file to a number I have to use something like
this:

to!int(readln().chomp());

Instead of a simpler:

to!int(readln());

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 22, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4165



--- Comment #1 from bearophile_hugs@eml.cc 2010-05-22 14:24:20 PDT ---
A comment by Adam Ruppe:
> I don't think that's a bug. It should only worry about converting, not filtering out bad stuff. That's an orthogonal problem that the other function does well, and easily too.

It's not a bug. But saying it's an orthogonal problem is not enough.

You must keep a balance between having a so flexible language/stdlib that's sloppy and can lead to bugs, and to have as much orthogonal functions as possible that are fussy and can lead to opposite kinds of bugs.

Often if I have to convert strings to numbers that have a leading newline. Converting such string with leading newline to a number is not sloppiness because my experience shows me it doesn't cause bugs in Python.

The way to!() is currently designed forces me to remove the spaces often. This has caused a bug in one script-like D program.

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


Lars T. Kyllingstad <bugzilla@kyllingen.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla@kyllingen.net
           Severity|normal                      |enhancement


--- Comment #2 from Lars T. Kyllingstad <bugzilla@kyllingen.net> 2010-08-13 03:27:02 PDT ---
Since we all agree this is not a bug, I'm marking it as an enhancement request.

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


David Simcha <dsimcha@yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |dsimcha@yahoo.com
         Resolution|                            |WONTFIX


--- Comment #3 from David Simcha <dsimcha@yahoo.com> 2010-08-15 08:10:52 PDT ---
I'm marking this as wontfix because it's by design (it's mentioned in the docs of std.conv), and there's a trivial workaround:

int x = to!int(" 0123\n".strip());

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