Issue 4656 - stdio.readf does not ignore white space
Summary: stdio.readf does not ignore white space
Status: RESOLVED WORKSFORME
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: Other Linux
: P2 normal
Assignee: Andrei Alexandrescu
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-08-16 14:11 UTC by Ali Cehreli
Modified: 2015-11-07 01:29 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Ali Cehreli 2010-08-16 14:11:35 UTC
This makes it very difficult to read values. We can't retire din.readf before fixing this.

import std.stdio;

void main()
{
    int i, j;
    readf("%s", &i);
    readf("%s", &j);
}

Run the program and provide the following as input:

42 43

The program terminates with

std.conv.ConvError: std.conv(1070): Can't convert value `LockingTextReader(File(807637C),  )' of type LockingTextReader to type int

The reason is the white space.
Comment 1 Andrei Alexandrescu 2011-05-09 09:18:02 UTC
This is by design. The example works when modified as follows:

import std.stdio;

void main()
{
    int i, j;
    readf("%s", &i);
    readf(" %s", &j);
}

The space before the second parameter tells readf to read and skip all whitespace before attempting conversion.

I've implemented readf to be a fair amount more Nazi about whitespace than scanf in an attempt to improve its precision. Scanf has been famously difficult to use for complex input parsing and validation, and I attribute some of that to its laissez-faire attitude toward whitespace. I'd be glad to relax some of readf's insistence on precise whitespace handling if there's enough evidence that that serves most of our users. I personally believe that the current behavior (strict by default, easy to relax) is best.
Comment 2 Ali Cehreli 2011-05-09 10:42:43 UTC
My apologies for forgetting about this bug report. I have since then replaced din and dout with stdin and stdout in D.ershane. The nazi whitespace behaviour worked correctly and consistently in all of the examples.

Thank you,
Ali
Comment 3 Andrei Alexandrescu 2011-05-09 11:27:57 UTC
Let's keep it opened until the error message is fixed.
Comment 4 SomeDude 2012-04-21 15:22:11 UTC
The original test case now produces this message:

PS E:\DigitalMars\dmd2\samples> rdmd bug.d
456 45646
std.conv.ConvException@E:\DigitalMars\dmd2\windows\bin\..\..\src\phobos\std\conv.d(1779): Unexpected ' ' when converting from type LockingTextReader to type int
Comment 5 Vladimir Panteleev 2015-11-07 00:54:06 UTC
(In reply to Andrei Alexandrescu from comment #3)
> Let's keep it opened until the error message is fixed.

What is there to be fixed?(In reply to SomeDude from comment #4)
> The original test case now produces this message:

"Unexpected ' '" seems like an improvement, so I think this can now be closed.
Comment 6 Andrei Alexandrescu 2015-11-07 01:29:35 UTC
alrighty