comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Converting Integer / Float to String
Date: Thu, 17 Jul 2008 11:24:51 +0200
Date: 2008-07-17T11:24:51+02:00	[thread overview]
Message-ID: <1iawxy1h6dfdt$.1n0nm38ucgzpg.dlg@40tude.net> (raw)
In-Reply-To: newscache$md654k$4y6$1@pleione.3soft.de

On Thu, 17 Jul 2008 10:32:59 +0200, Alexander Camek wrote:

> First of all, this was only a base to start discussion on it and at that 
> point I need some value to initial my float or integer value to get a proper 
> value if the string is malformed.

I don't think it is a good idea. Malformed document should be reported,
instead of being silently parsed.

> As far as I can tell, the background of the code snipplet is that the string 
> is given from an xml file which is parsed by glib xml and there I get all as 
> a string.

I see. Doesn't Glib's XML parser have a support for numeric parsing? I
mean, if you already use a parser then it is better to use it at full,
instead of adding other code.

> To your second question. The whitespaces are trimmed and I can only rely on 
> the point that there is a correct value in it,

But it can be like this:

<rubbish> 1.0 2 4 </rubbish>

Then what about encoded Unicode characters? Like "&#x1234". Does the parser
translate them? If it does, how does it this for non-Latin-1 characters? I
don't know it, but the parser could produce UTF-8 octets stored in Ada
String, which then would be OK to parse as if it were Latin-1... (:-))

> if not then the default value 
> will be set or at the beginning for initilisation 0.0 is used.

Well, the cases "there is no any number" and "the file contains rubbish"
are different to me. I would not use the default in the second case.

I have a library for parsing:

   http://www.dmitry-kazakov.de/ada/strings_edit.htm

Your sample code could look like:

begin
   Value := Strings_Edit.Floats.Value (Float_String);
exception
   when End_Error => -- No number
      Value := Default;
   when Data_Error => -- Wrong number, XML file is corrupt
       ...
   when Constraint_Error => -- Out of range
      ...
end;

If saturation of the input is OK, then

   Value (Float_String, ToFirst => True, ToLast => True);

that would saturate the input to the nearest bound instead of
Constraint_Error propagation.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



  reply	other threads:[~2008-07-17  9:24 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-07-17  7:49 Converting Integer / Float to String Alexander Camek
2008-07-17  8:05 ` anon
2008-07-17  8:15 ` Dmitry A. Kazakov
2008-07-17  8:32   ` Alexander Camek
2008-07-17  9:24     ` Dmitry A. Kazakov [this message]
2008-07-20  0:28 ` JPWoodruff
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox