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

Timothee Cour thelastmammoth at gmail.com
Sun Sep 8 20:16:48 PDT 2013


On Fri, Sep 6, 2013 at 9:38 PM, Jonathan M Davis <jmdavisProg at gmx.com>wrote:

> 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.
>

beautiful, thanks. not clear if it's better in std.string rather than
std.conv.

While I'm at it, I was also curious whether there's functionality for the
following (I wrote my own versions but they might not consider all cases) :

unittest{
  // isQuotedString: tests whether a string represents a quoted string.
  assert(`"abc"`. isQuotedString);
  assert(`q{abc}`. isQuotedString);
  assert(!"abc". isQuotedString);
}

//escapeString:
/// escapes a to be pasted as is in D code (analog to
std.process.escapeShellFileName which btw has little business to do in
std.process)
string escapeD(string a){
import std.array:replace;
return `r"`~a.replace(`"`,`" "\"" r"`)~`"`;
}

//inverse operation to unEscapeString
string escapeString(string a);


I have clear use cases for those, which I can explain if it's not obvious.



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


Agreed with this and std.conv.to calling a nothrow function that returns a
boolean.


>
>
> - Jonathan M Davis
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20130908/fd1648ad/attachment.html>


More information about the Digitalmars-d-learn mailing list