Skip to content

Commit

Permalink
Merge pull request #5591 from RazvanN7/Issue_12260
Browse files Browse the repository at this point in the history
Fix Issue 12260 - Improve error of std.stdio.readf when involving whitespace
merged-on-behalf-of: Andrei Alexandrescu <andralex@users.noreply.github.com>
  • Loading branch information
dlang-bot committed Jul 31, 2017
2 parents a6ef870 + afc7326 commit 63de27b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
9 changes: 8 additions & 1 deletion std/conv.d
Expand Up @@ -81,8 +81,15 @@ private auto convError(S, T)(S source, string fn = __FILE__, size_t ln = __LINE_
if (source.empty)
msg = "Unexpected end of input when converting from type " ~ S.stringof ~ " to type " ~ T.stringof;
else
msg = text("Unexpected '", source.front,
{
ElementType!S el = source.front;

if (el == '\n')
msg = text("Unexpected '\\n' when converting from type " ~ S.stringof ~ " to type " ~ T.stringof);
else
msg = text("Unexpected '", el,
"' when converting from type " ~ S.stringof ~ " to type " ~ T.stringof);
}

return new ConvException(msg, fn, ln);
}
Expand Down
22 changes: 22 additions & 0 deletions std/stdio.d
Expand Up @@ -1897,6 +1897,28 @@ $(CONSOLE
assert(b1 == true && b2 == false);
}

@system unittest
{
static import std.file;

auto deleteme = testFilename();
std.file.write(deleteme, "1\n2");
scope(exit) std.file.remove(deleteme);
int input;
auto f = File(deleteme);
f.readf("%s", &input);

import std.conv : ConvException;
try
{
f.readf("%s", &input);
}
catch (ConvException e)
{
assert(e.msg == "Unexpected '\\n' when converting from type LockingTextReader to type int");
}
}

/**
Returns a temporary file by calling
$(HTTP cplusplus.com/reference/clibrary/cstdio/_tmpfile.html, _tmpfile).
Expand Down

0 comments on commit 63de27b

Please sign in to comment.