<div dir="ltr">actually that doesn't work:<div><br></div><div><div>assert(!isNumeric(`j`)); //ok<br></div></div><div><div>assert(!isNumeric(`i`)); //fails ; i is treated as a complex number but that's not good behavior as we can't write auto a=i;<br>
</div></div><div>assert(isNumeric(`1e2`)); // fails even though we can write auto a=1e2;<br></div><div><br></div><div>In contrast, what I had worked (passes those asserts) but only throws on certain occasions (doesn't always throw on false).</div>
<div><br></div><div>bool isNumberLitteral(T=double)(string a){<br></div><div><div> import std.conv;</div><div> //ugly hack to avoid throwing most of the time; </div><div> if(!a.length||a==".")</div><div> return false;</div>
<div> string s="0"~a;</div><div> auto x=parse!T(s);</div><div> return s.length==0;</div><div>}</div></div><div><br></div><div><br></div><div><div>I'd like to get a version that never throws.</div><div><br>
</div></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, Sep 6, 2013 at 9:38 PM, Jonathan M Davis <span dir="ltr"><<a href="mailto:jmdavisProg@gmx.com" target="_blank">jmdavisProg@gmx.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">On Friday, September 06, 2013 21:15:44 Timothee Cour wrote:<br>
> I'd like to have a function:<br>
><br>
> @nothrow bool isNumberLitteral(string a);<br>
> unittest{<br>
> assert(isNumberLitteral("1.2"));<br>
> assert(!isNumberLitteral("a1.2"));<br>
> assert(!isNumberLitteral("a.b"));<br>
> }<br>
><br>
> I want it nothrow for efficiency (I'm using it intensively), and try/catch<br>
> as below has significant runtime overhead (esp when the exception is<br>
> caught):<br>
<br>
</div>You could try std.string.isNumeric.<br>
<br>
But it's true that it would be nice to have some sort of counterpart to<br>
<a href="http://std.conv.to" target="_blank">std.conv.to</a> which checked whether a conversion was possible or which returned<br>
its argument via an out parameter and returned whether it succeeded or not (or<br>
something similar) for cases where you need to avoid throwing.<br>
<br>
<a href="http://d.puremagic.com/issues/show_bug.cgi?id=6840" target="_blank">http://d.puremagic.com/issues/show_bug.cgi?id=6840</a><br>
<a href="http://d.puremagic.com/issues/show_bug.cgi?id=6843" target="_blank">http://d.puremagic.com/issues/show_bug.cgi?id=6843</a><br>
<span class="HOEnZb"><font color="#888888"><br>
- Jonathan M Davis<br>
</font></span></blockquote></div><br></div>