www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.announce - isNumeric.d - I'd like to contribute to Digital Mars D

reply David L. Davis <SpottedTiger yahoo.com> writes:
I'd like to share what I've written which may help others in their own D code,
and wish to contribute this code to Digital Mars D if Walter will accept it. I'm
not sure exactly which module this code would best fit in if it's accepted,
whether that would be in the std.string, std.math, or maybe in some a new
std.misc module.

But anyway, I found that I needed a isNumeric() function for my variant
(user-defined) datatype project, and already know I'll find it very useful in
checking for numeric strings. After creating this code, I decided to scan the
web for code others might have written for a isNumeric() function and to see if
I might have missed anything important in my own function. But, to my surprise I
found that most of them did something that was more of a isDigits() function
than a isNumeric() function...which I thought was pretty darn odd. Well, I hope
someone will find this code a tat-bit useful...enjoy! :)


































































































































































































































































































































































David L.

-------------------------------------------------------------------
"Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
-------------------------------------------------------------------

MKoD: http://spottedtiger.tripod.com/D_Language/D_Main_XP.html
Jun 01 2005
parent reply "Andrew Fedoniouk" <news terrainformatica.com> writes:
Cool!

David, would it be possible to write also toNumeric ?

Like this:

std.boxer.Box  toNumeric(const char[] s, bool bAllowSep = false)

?

Andrew.






"David L. Davis" <SpottedTiger yahoo.com> wrote in message 
news:d7m0nf$3s0$1 digitaldaemon.com...
 I'd like to share what I've written which may help others in their own D 
 code,
 and wish to contribute this code to Digital Mars D if Walter will accept 
 it. I'm
 not sure exactly which module this code would best fit in if it's 
 accepted,
 whether that would be in the std.string, std.math, or maybe in some a new
 std.misc module.

 But anyway, I found that I needed a isNumeric() function for my variant
 (user-defined) datatype project, and already know I'll find it very useful 
 in
 checking for numeric strings. After creating this code, I decided to scan 
 the
 web for code others might have written for a isNumeric() function and to 
 see if
 I might have missed anything important in my own function. But, to my 
 surprise I
 found that most of them did something that was more of a isDigits() 
 function
 than a isNumeric() function...which I thought was pretty darn odd. Well, I 
 hope
 someone will find this code a tat-bit useful...enjoy! :)


































































































































































































































































































































































 David L.

 -------------------------------------------------------------------
 "Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
 -------------------------------------------------------------------

 MKoD: http://spottedtiger.tripod.com/D_Language/D_Main_XP.html 
Jun 02 2005
next sibling parent reply Derek Parnell <derek psych.ward> writes:
On Thu, 2 Jun 2005 15:26:27 -0700, Andrew Fedoniouk wrote:

 Cool!
 
 David, would it be possible to write also toNumeric ?
 
 Like this:
 
 std.boxer.Box  toNumeric(const char[] s, bool bAllowSep = false)
I just wrote one of those a couple of days ago. Its a part of a new application I'm writing but I guess I can split it out ... give me a few moments ... -- Derek Melbourne, Australia 3/06/2005 8:57:45 AM
Jun 02 2005
parent reply Derek Parnell <derek psych.ward> writes:
Content-type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit

On Fri, 3 Jun 2005 08:59:41 +1000, Derek Parnell wrote:

 On Thu, 2 Jun 2005 15:26:27 -0700, Andrew Fedoniouk wrote:
 
 Cool!
 
 David, would it be possible to write also toNumeric ?
 
 Like this:
 
 std.boxer.Box  toNumeric(const char[] s, bool bAllowSep = false)
I just wrote one of those a couple of days ago. Its a part of a new application I'm writing but I guess I can split it out ... give me a few moments ...
Here is my version attached ;-) real TextToNumber( dchar[] pText, out int pStopPosn, bool pNotify = true); Hope somebody finds it useful. -- Derek Melbourne, Australia 3/06/2005 10:32:49 AM
Jun 02 2005
next sibling parent reply "Kris" <fu bar.com> writes:
Cool!

Mango.format has similar stuff for formatting and parsing numbers: e.g.
http://svn.dsource.org/projects/mango/trunk/mango/format/Double.d

There's also a more powerful version using David Gay's dtoa.c code. You
really should take a look at what that C file does ~ it's kinda'
frightening!

http://svn.dsource.org/projects/mango/trunk/mango/format/dtoa.c
http://svn.dsource.org/projects/mango/trunk/mango/format/DGDouble.d

- Kris


"Derek Parnell" <derek psych.ward> wrote in message
news:bo1091wvq352$.1noqjnreoaasw$.dlg 40tude.net...
 On Fri, 3 Jun 2005 08:59:41 +1000, Derek Parnell wrote:

 On Thu, 2 Jun 2005 15:26:27 -0700, Andrew Fedoniouk wrote:

 Cool!

 David, would it be possible to write also toNumeric ?

 Like this:

 std.boxer.Box  toNumeric(const char[] s, bool bAllowSep = false)
I just wrote one of those a couple of days ago. Its a part of a new application I'm writing but I guess I can split it out ... give me a few moments ...
Here is my version attached ;-) real TextToNumber( dchar[] pText, out int pStopPosn, bool pNotify =
true);
 Hope somebody finds it useful.

 -- 
 Derek
 Melbourne, Australia
 3/06/2005 10:32:49 AM
Jun 02 2005
parent reply pragma <pragma_member pathlink.com> writes:
In article <d7odh0$2mv2$1 digitaldaemon.com>, Kris says...
Cool!

Mango.format has similar stuff for formatting and parsing numbers: e.g.
http://svn.dsource.org/projects/mango/trunk/mango/format/Double.d

There's also a more powerful version using David Gay's dtoa.c code. You
really should take a look at what that C file does ~ it's kinda'
frightening!

http://svn.dsource.org/projects/mango/trunk/mango/format/dtoa.c
http://svn.dsource.org/projects/mango/trunk/mango/format/DGDouble.d

- Kris
[reading dtoa.c] "kinda' frigtening" doesn't even begin to describe it. Not only am I reminded of why I switched to D, but I actually feel a touch queasy now. Here's to cleaner solutions and better looking code. - EricAnderton at yahoo
Jun 03 2005
parent reply kris <fu bar.org> writes:
pragma wrote:
 In article <d7odh0$2mv2$1 digitaldaemon.com>, Kris says...
 
Cool!

Mango.format has similar stuff for formatting and parsing numbers: e.g.
http://svn.dsource.org/projects/mango/trunk/mango/format/Double.d

There's also a more powerful version using David Gay's dtoa.c code. You
really should take a look at what that C file does ~ it's kinda'
frightening!

http://svn.dsource.org/projects/mango/trunk/mango/format/dtoa.c
http://svn.dsource.org/projects/mango/trunk/mango/format/DGDouble.d

- Kris
[reading dtoa.c] "kinda' frigtening" doesn't even begin to describe it. Not only am I reminded of why I switched to D, but I actually feel a touch queasy now. Here's to cleaner solutions and better looking code. - EricAnderton at yahoo
First time I read through dtoa.c, I was about half way through and thinking "egad! what the feck?" when up pops a comment in the code saying "Now, here's the difficult stuff" ... I knew, at that point, that I was completely out of my depth.
Jun 03 2005
parent Sean Kelly <sean f4.ca> writes:
In article <d7pvni$rsr$1 digitaldaemon.com>, kris says...
First time I read through dtoa.c, I was about half way through and 
thinking "egad! what the feck?" when up pops a comment in the code 
saying "Now, here's the difficult stuff" ... I knew, at that point, that 
I was completely out of my depth.
The best part about this profession is being hired to maintain code like that. Perhaps back when that stuff was actually written the few CPU cycles it saved over a readable design were actually important ;) Sean
Jun 03 2005
prev sibling parent reply "Andrew Fedoniouk" <news terrainformatica.com> writes:
"Derek Parnell" <derek psych.ward> wrote in message 
news:bo1091wvq352$.1noqjnreoaasw$.dlg 40tude.net...
 On Fri, 3 Jun 2005 08:59:41 +1000, Derek Parnell wrote:

 On Thu, 2 Jun 2005 15:26:27 -0700, Andrew Fedoniouk wrote:

 Cool!

 David, would it be possible to write also toNumeric ?

 Like this:

 std.boxer.Box  toNumeric(const char[] s, bool bAllowSep = false)
I just wrote one of those a couple of days ago. Its a part of a new application I'm writing but I guess I can split it out ... give me a few moments ...
Here is my version attached ;-) real TextToNumber( dchar[] pText, out int pStopPosn, bool pNotify = true); Hope somebody finds it useful.
Nice, Derek, One thought: to have returning value as box allows: 1) to detect was it value at all; 2) to select the most compact storage form. 3) to be more precise - casting real to int is always something from fuzzy logic field. Just my guess. Andrew.
 -- 
 Derek
 Melbourne, Australia
 3/06/2005 10:32:49 AM 
Jun 02 2005
parent Derek Parnell <derek psych.ward> writes:
On Thu, 2 Jun 2005 21:33:45 -0700, Andrew Fedoniouk wrote:


[snip]
 One thought:
 
 to have returning value as box allows:
 
 1) to detect was it value at all;
 2) to select the most compact storage form.
 3) to be more precise - casting real to int is always something from
     fuzzy logic field.
This was ported from another language and before the boxer module was available. I'll have go at implementing your suggestion though ... wish me luck ;-) -- Derek Melbourne, Australia 3/06/2005 3:01:16 PM
Jun 02 2005
prev sibling next sibling parent David L. Davis <SpottedTiger yahoo.com> writes:
In article <d7o12k$2dv4$1 digitaldaemon.com>, Andrew Fedoniouk says...
Cool!

David, would it be possible to write also toNumeric ?

Like this:

std.boxer.Box  toNumeric(const char[] s, bool bAllowSep = false)

?

Andrew.
Andrew, here's what I came up with so far...what do you think? (Note: that this uses the isNumeric.d code as an import) Output: ----------------- C:\dmd>dmd tonumeric.d isNumeric.d -release C:\dmd\bin\..\..\dm\bin\link.exe tonumeric+isNumeric,,,user32+kernel32/noi; C:\dmd>tonumeric unbox!(uint)(wx)=123 unbox!(long)(wx)=-12 unbox!(double)(wx)= 1.234 unbox!(ifloat)(wx= 1.234 unbox!(float)(wx)=1.234e+12 C:\dmd> David L. ------------------------------------------------------------------- "Dare to reach for the Stars...Dare to Dream, Build, and Achieve!" ------------------------------------------------------------------- MKoD: http://spottedtiger.tripod.com/D_Language/D_Main_XP.html
Jun 02 2005
prev sibling parent David L. Davis <SpottedTiger yahoo.com> writes:
In article <d7o12k$2dv4$1 digitaldaemon.com>, Andrew Fedoniouk says...
Cool!

David, would it be possible to write also toNumeric ?

Like this:

std.boxer.Box  toNumeric(const char[] s, bool bAllowSep = false)

?

Andrew.
Here's a working copy! My first one I stayed up too late working on it and I made a few mistakes...which they're all fixed in this version. The nice thing about this code, is that it uses Phobos' builded in std.boxer and std.conv functions to do its work, which are already within the runtime library. Except of course for the isNumeric() function which can be found at the begnning of this thread. Well, I hope this does the trick...but it will be kool to see what others will come up with! ;) strings. string."); Output: ---------- C:\dmd>dmd tonumeric.d isnumeric.d -release C:\dmd\bin\..\..\dm\bin\link.exe tonumeric+isnumeric,,,user32+kernel32/noi; C:\dmd>tonumeric unbox!(uint)(wx)=123456 unbox!(long)(wx)= -124599 unbox!(double)(wx)= 1.234 unbox!(ifloat)(wx)=1.234 unbox!(float)(wx)=1.234e+12 unbox!(creal)(wx)=1.234e+12+3.454e+36i unbox!(creal)(wx)=nan+0i C:\dmd> David L. ------------------------------------------------------------------- "Dare to reach for the Stars...Dare to Dream, Build, and Achieve!" ------------------------------------------------------------------- MKoD: http://spottedtiger.tripod.com/D_Language/D_Main_XP.html
Jun 03 2005