Bug 8231 – conv.to fails to convert string with trailing "u"
Status
RESOLVED
Resolution
INVALID
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-06-11T18:36:00Z
Last change time
2016-08-27T22:45:41Z
Assigned to
nobody
Creator
andrej.mitrovich
Comments
Comment #0 by andrej.mitrovich — 2012-06-11T18:36:57Z
import std.conv;
void main()
{
uint x = 3u; // ok
uint y = to!uint("3u"); // fail
}
std.conv.ConvException@D:\DMD\dmd2\windows\bin\..\..\src\phobos\std\conv.d(1597): Unexpected 'u' when converting from type string to type uint
If it can be used in D syntax it should be convertible by conv.to.
Comment #1 by github-bugzilla — 2012-07-01T22:13:53Z
Comment #2 by john.loughran.colvin — 2013-06-17T15:21:56Z
This is quite easily fixed with a little utility function that checks if the remainder after parsing is a floating point or integer suffix.
However, it is potentially a nasty subtle breaking change as it's quite a common pattern to rely on functions like "to" to check for unusual input, which this would change the definition of. It also introduces a difference in attitude between "to" and "parse", which currently work together very neatly.
On those grounds, I'm against it.
To be acceptable it would have to be enabled with a template argument (an enum with names RAW and D_LITERAL_AWARE ?), or be in a function with a different name.
Comment #3 by bearophile_hugs — 2013-06-17T15:39:28Z
Both converting regular numbers and converting D literals to their values are important needs and use cases, but mixing them transparently is probably a not so good idea.
So better to give it a different function name or to add some compile-time argument to tell apart the two cases, as Comment #2 says.
D number literals also allow underscores: 1_000_000u
Comment #4 by hsteoh — 2014-07-17T18:36:41Z
I agree that this is a bad idea. std.conv.to is intended for converting user data between different types, not for parsing D language constructs like uint literals. We should have a different function for that (maybe something from std.d.lexer, once we have that).
Comment #5 by andrej.mitrovich — 2016-08-27T22:45:41Z
Yeah the suggestion was bad, if anything it might subtly allow buggy input.