Issue 17708 - Escape control characters in std.conv.to error messages
Summary: Escape control characters in std.conv.to error messages
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P3 minor
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-07-31 19:31 UTC by Vladimir Panteleev
Modified: 2024-12-01 16:30 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Vladimir Panteleev 2017-07-31 19:31:51 UTC
Currently this program:

///// test.d ////
import std.conv;

void main()
{
    to!int("\r");
}
/////////////////

will produce output which looks like this in a terminal:

' when converting from type string to type intbin/../phobos/std/conv.d(2168): Unexpected '
----------------
??:? pure @safe int std.conv.parse!(int, immutable(char)[]).parse(ref immutable(char)[]) [0x4331de]
??:? pure @safe int std.conv.toImpl!(int, immutable(char)[]).toImpl(immutable(char)[]) [0x436a70]
??:? pure @safe int std.conv.to!(int).to!(immutable(char)[]).to(immutable(char)[]) [0x432a83]
??:? _Dmain [0x432a5d]

The error message appears malformed.

std.conv.to should escape whitespace / control / non-printable characters in its error messages. Perhaps the existing functionality in std.conv / std.format can be reused: format!"%(%s%)"(c.only) will already correctly escape such characters.

See discussion in https://github.com/dlang/phobos/pull/5591 (which fixed this issue specifically for the newline character only) for more details.
Comment 1 Jon Degenhardt 2017-12-17 18:50:54 UTC
Encountered this case in my tools (https://github.com/eBay/tsv-utils-dlang/issues/96). A user was processing a DOS file (with `\r\n` line endings) on a Unix box, byLine left the extraneous `\r` at the end of the line. My tool tried to convert to double, and output the error text when this failed due to the `\r`. The formatting of the output is quite confusing, as `\r` actually repositions to the start of the line, effectively deleting the initial part of the output line. The result:

  ' when converting from type const(char)[] to type doubleUnexpected '

For my tools I'll add explicit detection of DOS line endings on Unix boxes, it's just too common when getting data files from a variety of sources. However, it'd be helpful to update error message generation to handle `\r` and other non-printing characters.
Comment 2 berni44 2019-12-06 13:10:56 UTC
Which controll characters need escaping? 0x00 - 0x1F only? Or are there more of them I'm unaware of?
Comment 3 dlangBugzillaToGithub 2024-12-01 16:30:39 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/phobos/issues/10259

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB