Thread overview | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
September 07, 2013 Re: nothrow function to tell if a string can be converted to a number? | ||||
---|---|---|---|---|
| ||||
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 |
September 07, 2013 Re: nothrow function to tell if a string can be converted to a number? | ||||
---|---|---|---|---|
| ||||
On Sat, Sep 07, 2013 at 12:38:58AM -0400, Jonathan M Davis 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. > > 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 [...] I like the idea of maybeTo!(). But I'm not sure if it's possible to guarantee performance -- conversion to user-defined types, for example, may involving a ctor that might throw. If so, it's impossible to implement maybeTo!() without using try/catch, so the performance hit will still be there. But at least, it will buy us performance when basic types are used. T -- It is of the new things that men tire --- of fashions and proposals and improvements and change. It is the old things that startle and intoxicate. It is the old things that are young. -- G.K. Chesterton |
September 07, 2013 Re: nothrow function to tell if a string can be converted to a number? | ||||
---|---|---|---|---|
| ||||
On Friday, September 06, 2013 22:38:20 H. S. Teoh wrote:
> On Sat, Sep 07, 2013 at 12:38:58AM -0400, Jonathan M Davis 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.
> >
> > 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
>
> [...]
>
> I like the idea of maybeTo!(). But I'm not sure if it's possible to guarantee performance -- conversion to user-defined types, for example, may involving a ctor that might throw. If so, it's impossible to implement maybeTo!() without using try/catch, so the performance hit will still be there.
>
> But at least, it will buy us performance when basic types are used.
If need be, we could go the route of creating a new function which std.conv.to looks for in addition to the cast operator where that function does a conversion without throwing and returns false when it fails or returns a Nullable or something along those lines so that std.conv.to can take advantage of it. And if the type doesn't implement that function, then either it won't work with the version of to which doesn't throw, or that version of to will use a try-catch (though I'd be inclined to make it require that function in order to avoid invisible performance hits).
But I really don't like the idea of maybeTo as Bearophile describes it (as a wrapper around to which catches the exception). I think that we really need a version of to which takes care of this (and it could be called maybeTo if we wanted, but it needs to not doing any wrapping).
- Jonathan M Davis
|
September 07, 2013 Re: nothrow function to tell if a string can be converted to a number? | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | 07-Sep-2013 09:38, H. S. Teoh пишет: > On Sat, Sep 07, 2013 at 12:38:58AM -0400, Jonathan M Davis 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. >> >> 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 > [...] > > I like the idea of maybeTo!(). But I'm not sure if it's possible to > guarantee performance -- conversion to user-defined types, for example, > may involving a ctor that might throw. If so, it's impossible to > implement maybeTo!() without using try/catch, so the performance hit > will still be there. As it's a template it could be inferred as nothrow for basic types. I've been constantly looking for something like: bool tryTo(T)(ref T dest, ...<same args as in to>) That either parses successfully or returns false and leaves dest unaffected. > > But at least, it will buy us performance when basic types are used. > > > T > -- Dmitry Olshansky |
September 09, 2013 Re: nothrow function to tell if a string can be converted to a number? | ||||
---|---|---|---|---|
| ||||
Attachments:
| 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: > > 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 > |
September 09, 2013 Re: nothrow function to tell if a string can be converted to a number? | ||||
---|---|---|---|---|
| ||||
Attachments:
| sorry I was a bit sloppy in my previous post. Here it is corrected: I was also curious whether there's functionality for the following (I wrote my own versions but they might not consider all cases) bool isStringLitteral(string); //tests whether a string represents a string litteral unittest{ assert(`"abc"`. isStringLitteral); assert(`q{abc}`. isStringLitteral); assert(!"abc". isStringLitteral); } string escapeString(string); // escapes a string; analog to std.process.escapeShellFileName which btw could be placed in a different module than std.process (std.string?) string escapeString(string a){ import std.array:replace; return `r"`~a.replace(`"`,`" "\"" r"`)~`"`; // not sure if it'll always work } unittest{ assert(`abc`. escapeString==r"`abc`"); } string unEscapeString(string a); //inverse operation to escapeString unittest{ assert(`"abc"`. unEscapeString ==`abc`); } I several use cases for those, which I can explain if it's unclear. On Sun, Sep 8, 2013 at 8:16 PM, Timothee Cour <thelastmammoth@gmail.com>wrote: > > > > 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: >> > 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) : > sorry I was a bit sloppy in my previous post. Here it is corrected: unittest{ > // isStringLitteral: tests whether a string represents a string litteral > assert(`"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 unEscapeString > string escapeString(string a); > > > I have clear use cases for those, which I can explain if it's not obvious. > I meant s/isQuotedString/isStringLitteral > > 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 >> > > |
September 11, 2013 Re: nothrow function to tell if a string can be converted to a number? | ||||
---|---|---|---|---|
| ||||
Attachments:
| actually that doesn't work: assert(!isNumeric(`j`)); //ok 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; assert(isNumeric(`1e2`)); // fails even though we can write auto a=1e2; In contrast, what I had worked (passes those asserts) but only throws on certain occasions (doesn't always throw on false). bool isNumberLitteral(T=double)(string a){ import std.conv; //ugly hack to avoid throwing most of the time; if(!a.length||a==".") return false; string s="0"~a; auto x=parse!T(s); return s.length==0; } I'd like to get a version that never throws. 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: > > 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 > |
September 11, 2013 Re: nothrow function to tell if a string can be converted to a number? | ||||
---|---|---|---|---|
| ||||
On Wed, Sep 11, 2013 at 02:10:32PM -0700, Timothee Cour wrote: > actually that doesn't work: > > assert(!isNumeric(`j`)); //ok > 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; Yikes! This makes me *extremely* glad built-in complex numbers are deprecated... can you imagine the mess that would result if you had a loop counter named 'i' and the loop body contains expressions involving 'i' and complex literals involving (the other) 'i'? *shudder* T -- GEEK = Gatherer of Extremely Enlightening Knowledge |
September 12, 2013 Re: nothrow function to tell if a string can be converted to a number? | ||||
---|---|---|---|---|
| ||||
Attachments:
| anyways, isNumeric sounds buggy, isn't it?
On Wed, Sep 11, 2013 at 4:03 PM, H. S. Teoh <hsteoh@quickfur.ath.cx> wrote:
> On Wed, Sep 11, 2013 at 02:10:32PM -0700, Timothee Cour wrote:
> > actually that doesn't work:
> >
> > assert(!isNumeric(`j`)); //ok
> > 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;
>
> Yikes!
>
> This makes me *extremely* glad built-in complex numbers are deprecated... can you imagine the mess that would result if you had a loop counter named 'i' and the loop body contains expressions involving 'i' and complex literals involving (the other) 'i'?
>
> *shudder*
>
>
> T
>
> --
> GEEK = Gatherer of Extremely Enlightening Knowledge
>
|
September 12, 2013 Re: nothrow function to tell if a string can be converted to a number? | ||||
---|---|---|---|---|
| ||||
On Wed, Sep 11, 2013 at 08:15:26PM -0700, Timothee Cour wrote: > anyways, isNumeric sounds buggy, isn't it? Yeah, I'd file a bug. I can't see how "i" can possibly be numeric. I thought the compiler rejected that anyway? > On Wed, Sep 11, 2013 at 4:03 PM, H. S. Teoh <hsteoh@quickfur.ath.cx> wrote: > > > On Wed, Sep 11, 2013 at 02:10:32PM -0700, Timothee Cour wrote: > > > actually that doesn't work: > > > > > > assert(!isNumeric(`j`)); //ok > > > 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; > > > > Yikes! > > > > This makes me *extremely* glad built-in complex numbers are deprecated... can you imagine the mess that would result if you had a loop counter named 'i' and the loop body contains expressions involving 'i' and complex literals involving (the other) 'i'? > > > > *shudder* > > > > > > T > > > > -- > > GEEK = Gatherer of Extremely Enlightening Knowledge > > -- Help a man when he is in trouble and he will remember you when he is in trouble again. |
Copyright © 1999-2021 by the D Language Foundation