On Fri, Sep 6, 2013 at 9:38 PM, Jonathan M Davis <jmdavisProg@gmx.com> wrote:
On Friday, September 06, 2013 21:15:44 Timothee Cour wrote:You could try std.string.isNumeric.
> 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):
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{// isStringLitteral: tests whether a string represents a string litteralassert(`"abc"`. isStringLitteral);assert(`q{abc}`. isStringLitteral);assert(!"abc". isStringLitteral);}
//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 unEscapeStringstring escapeString(string a);I have clear use cases for those, which I can explain if it's not obvious.
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 unEscapeStringstring 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=6843Agreed with this and std.conv.to calling a nothrow function that returns a boolean.
- Jonathan M Davis