[Issue 3610] isNumeric("3.14w") is false.

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Jun 5 17:54:50 PDT 2011


http://d.puremagic.com/issues/show_bug.cgi?id=3610



--- Comment #5 from Jonathan M Davis <jmdavisProg at gmx.com> 2011-06-05 17:50:20 PDT ---
Well, in order to properly determine whether a string is fully numeric and
convertible to an int or float or whatever, I believe that you _do_ have to
parse the whole thing. I don't see how you can really get around that except
when shortcutting if you find a character which makes the string fail for
isNumeric.

to throws if you can't convert the whole string, and parse throws if you can't
convert any of it. So, checking for convertibility by doing the conversion
would be a _bad_ idea, because handling exceptions is extremely expensive. And
so, there should definitely be a way to check for convertibility prior to
converting. Phobos provides the tools to do that, but given the extra
complication of - and ., it seems like it would just force people to keep
reinventing the wheel if we didn't have a function like isNumeric which said
whether a string was numeric. And technically, with that in mind, there would
be benefit in having isInt, isFloat, etc. in addition to isNumeric.

So, I don't think that the fact that the string needs to be fully parsed is an
issue. It's a given and can't be gotten around. And in comparison to the cost
of an exception being thrown, it's generally going to be a negligible cost.
However, what I think that this highlights is that it would be generally useful
to know prior to calling parse or to whether parse or to will succeed. And that
being the case, maybe we need a more general solution in std.conv where you can
call a function to test whether _any_ particular call to parse or to will
succeed. And _that_ being the case, std.string.isNumeric should probably go
away, but we'd need the new functions in std.conv first.

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


More information about the Digitalmars-d-bugs mailing list