nothrow function to tell if a string can be converted to a number?

Jonathan M Davis jmdavisProg at gmx.com
Fri Sep 6 21:38:58 PDT 2013


On Friday, September 06, 2013 21:15:44 Timothee Cour wrote:
> I'd like to have a function:
> 
> @nothrow bool isNumberLitteral(string a);
> unittest{
> assert(isNumberLitteral("1.2"));
> assert(!isNumberLitteral("a1.2"));
> assert(!isNumberLitteral("a.b"));
> }
> 
> I want it nothrow for efficiency (I'm using it intensively), and try/catch
> as below has significant runtime overhead (esp when the exception is
> caught):

You could try std.string.isNumeric.

But it's true that it would be nice to have some sort of counterpart to 
std.conv.to which checked whether a conversion was possible or which returned 
its argument via an out parameter and returned whether it succeeded or not (or 
something similar) for cases where you need to avoid throwing.

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

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list