<div dir="ltr"><div>I'd like to have a function:</div><div><br></div><div>@nothrow bool isNumberLitteral(string a);<br></div><div>unittest{</div><div> assert(isNumberLitteral("1.2"));</div><div> assert(!isNumberLitteral("a1.2"));</div>
<div> assert(!isNumberLitteral("a.b"));</div><div>}<br></div><div><br></div><div>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):</div>
<div><br></div><div><br></div><div>bool isNumberLitteral(string a){//slow because throws</div><div><span class="" style="white-space:pre"> </span>try{</div><div><span class="" style="white-space:pre"> </span>import std.conv:to;<span class="" style="white-space:pre"> </span><br>
</div><div><span class="" style="white-space:pre"> </span>auto ret=<a href="http://a.to">a.to</a>!double;</div><div><span class="" style="white-space:pre"> </span>}</div><div><span class="" style="white-space:pre"> </span>catch</div>
<div><span class="" style="white-space:pre"> </span>return false;</div><div><span class="" style="white-space:pre"> </span>return true;</div><div>}</div><div><br></div><div>even this can throw, eg on "a.b" (and uses gc)</div>
<div><div>bool isNumberLitteral(T=double)(string a){</div><div> import std.conv;</div><div> //ugly hack to avoid throwing; <br></div><div> if(!a.length||a==".")</div><div> <span class="" style="white-space:pre"> </span>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>