Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Ticket #1486 (closed defect: fixed)

Opened 3 years ago

Last modified 3 years ago

Issues with tango.util.Convert

Reported by: d0c Assigned to: DRK
Priority: normal Milestone: 0.99.9
Component: Tango Version: trunk
Keywords: convert float patch Cc:

Description

Hi, with the latest SVN version, I get the following errors in tango.util.Convert:

to!(float)("Foo") returns 0 (should throw ConversionException?).

to!(float)("") throws an AV in the runtime:

Error: Access Violation (object.Exception)
backtrace:
 004042d1 tango.text.convert.Float.parse!(char) (+49) import\tango\text\convert\Float.d:308
 0040479b tango.util.Convert.toReal!(float, char[0]) (+13) import\tango\util\Convert.d:837
 00404786 tango.util.Convert.toImpl!(float, char[0]) (+e) import\tango\util\Convert.d:1047
 00404772 tango.util.Convert.to!(float).to!(char[0], tango.util.Convert.Missing) (+e) import\tango\util\Convert.d:148

It seems, tango.util.Convert.toReal is missing some error checking (like toIntegerFromString).

Another one: to!(float)("0x1.2cp+9") returns -nan (wtf?), whereas toFloat("0x1.2cp+9") throws ConversionException? (not nice, but better). Is there no support for hexadecimal exponential notation of floats in tango?

Last (I'm not sure if this is intended behavior),
to!(int)("") returns 0.

Attachments

convert.patch (4.2 kB) - added by d0c on 03/22/09 08:35:20.
Patch for to!(float) and some other errors (no hex-float support)

Change History

02/18/09 12:32:10 changed by kris

  • owner changed from kris to DRK.

03/22/09 08:35:20 changed by d0c

  • attachment convert.patch added.

Patch for to!(float) and some other errors (no hex-float support)

03/22/09 08:51:41 changed by d0c

  • keywords changed from convert float to convert float patch.

I added a patch that should fix the abovementioned problems. I assumed that for empty strings, the to function should return the type's default value (maybe add some documentation for that case). The patch does not add hex-float support.

Changes:

  • to!(float)("") returns nan
  • Hex support for to, e.g. to!(float)("0x2d"), to!(int)("0x2d")
  • to!(float) throws ConversionError? for invalid strings
  • to!(int)("0x") and to!(int)("-") are now invalid, same for float
  • Fixed a bug in toIntegerFromInteger (range checks)
  • More unittests in tango.util.Convert

03/22/09 09:00:10 changed by larsivi

  • milestone changed from 0.99.8 to 0.99.9.

04/14/09 08:44:25 changed by DRK

  • status changed from new to closed.
  • resolution set to fixed.

(In [4562]) Improved handling of invalid values in tango.util.Convert. Some extra checks were added to convert.Float and convert.Integer in support of this. Closes #1486.

04/14/09 08:47:16 changed by DRK

Improved the error handling. The following assertions now hold:

assert(ex( to!(int)("") ));

assert(ex( to!(real)("Foo") ));
assert(ex( to!(real)("") ));
assert(ex( to!(real)("0x1.2cp+9") ));

// From d0c's patch
assert(ex( to!(int)("0x20") ));
assert(ex( to!(int)("0x") ));
assert(ex( to!(int)("-") ));
assert(ex( to!(int)("-0x") ));

assert( to!(real)("0x20") == cast(real) 0x20 );
assert(ex( to!(real)("0x") ));
assert(ex( to!(real)("-") ));

Support for hex floats should be an enhancement for convert.Float, not for util.Convert.