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

Conversions (tango.text.convert.Integer.toInt etc and std.conv.to)

Posted: 10/17/07 17:24:07 Modified: 02/11/08 18:10:43

Well, I've been writing a file-parsing library, and wanted to provide an easy way to convert text into most data-types. I found the tango.text.convert.Integer.toInt and Float.toFloat routines to be slightly inconsistant (see http://www.dsource.org/projects/tango/ticket/672) and not really optimal for conversions to any (integer or floating point) type and wanted support for some other conversions, so I decided to write my own.

What I now have is this:

Code removed since it's obsolete and makes the post overly long.

(This still needs a bit more work such as making the documentation more consistent. Most notably it lacks support for wchar, dchar, cent, ucent, imaginary and complex types, and only takes input from chars.)

I noticed that phobos now has templated to!(type)(...) and parse!(type)(...) functions in std.conv, and wondered if something similar, either based on the phobos functions or my own, would be a useful addition to tango. I have no idea what the phobos functions support, how accurate they are etc (tbh I only just noticed they exist and can't be bothered to try them out right now).

Basically I'm offering the above code and to do a bit of work making it more complete and use more tango-like conventions if you'd like to use it. Otherwise I'll probably just keep my own code more-or-less as is and use that, and make it publically available along with more of my project later.

btw I'm also wanting conversions from various types to strings; I've yet to investigate what's available for this but I could end up doing something in similar style (although I expect tango already provides well enough for this).

Diggory Hardy / Cyborg16

Author Message

Posted: 10/19/07 10:19:08

I forgot to mention the above code would be better called a (nearly) universal parser, rather than a converter. So really it serves a completely different purpose to std.conv.to .

I just had a look at std.conv and it contains some rather horrible looking code to differentiate between different types of conversion... but that's probably necessary when you want to convert to/from basically ANY type. The way tango does things with Layout to convert types to a string and mathematical & UTF functions for converting between types where cast(type) is insufficient makes sense, since it separates conversions into the separate types of things they're likely to be used for. That leaves conversion from strings to other types --- a generic parser is useful for this, especially when using generic programming.

Whether or not you include a generic parser in tango really isn't my choice though.

I have a few ideas for improvements to my (or any other) code to be used for this purpose:

+ Rename to parse + Include a specification of some sort, to specify octal, hex & binary support, escape sequences, how whitespace is handled, how arrays are read, etc. + Decide what types and extras to support (e.g. my binary support as above is probably out of place). + Show how people can extend this by including their own templates with some specialisations, with an unspecialised template calling these functions.

That's my thoughts on the idea, and I'm quite happy to have a go if one of the tango designers decides to include this functionality. I'd like some feedback since I expect others have good ideas and more relevant experience than me!

Posted: 11/03/07 03:19:17

Hrm, DanielKeep? has been writing something similar to this, but as a to!() style module

Posted: 11/05/07 11:21:35

Hmm, I've made a few changes in my code. But it's still basically a char[] -> various types converter. I've also made a module with templates for doing the inverse (various types -> char[]) since I thought Layout was an overly heavy implementation for my needs and still didn't handle writing arrays... and it was extremely easy to do.

Anyway, my code's available on request, but I won't be doing any more development than I require unless requested.

Posted: 11/05/07 11:28:05 -- Modified: 11/05/07 11:28:26 by
larsivi

Daniel's code is now available in tango/util/Convert.

Posted: 02/11/08 18:25:15

Just to let you know, my conversion code is in a bit better form now, and available here. It supports converting arrays and associative arrays of any supported type too now, and tries to keep its syntax close to that of D (well, without rewriting the tango converters).

Tango's util.Convert module looks more structured than my conversion code, and probably supports most of what my code does with the exception of escape sequences (which can't be done with a generic string->string converter of course), arrays and associative arrays (and a binary type I added). Both still lack support for converting complex types to/from strings (maybe we should add this to text.convert.Float?).

Most of what my code does could of course be added to util.Convert, but I still think there's a purpose for a dedicated text parser/formatter like I've written. E.g. the two modules handle conversions from strings to strings completely differently; my code uses escape sequences and appends quotes. Of course it may well not be of generic enough purpose to be worth including in tango.

Posted: 02/11/08 18:51:36

I'm a bit slow today, could you please list up the functionality of your module and state where it differs from / expand upon current functionality in Tango?

If what you have written doesn't end up in Tango proper, there is always Tango.Scrapple to make it a bit more visible.

Posted: 02/12/08 11:56:42

Yeah Scrapple's probably a good place for it, thanks. I'm a bit doubtful that it would be useful in tango proper since it's got limited uses.

Basically I wrote the code for a file-IO library to read and write variables using a D-like syntax. Thus things like an int would be converted to a string like to!() does, but a char is put in single-quotes with escapable-characters converted to escape-sequences, arrays are enclosed in square brackets and separated by commas, etc. The reading part does pretty-much the same thing as the D compiler when reading variables.

Aside from converting variables in a text format for saving, I've also found it useful for debug output of array/associative array types (since converting the whole thing to a string just requires one function call), and for comparing associative arrays (compiler bug causes the built-in comparison to fail).

Posted: 02/12/08 12:17:59

Some form of isolated escaping functionality may be of interest, the rest does indeed sound a bit application specific. If you have a routine that can be generally used for escaping, please post it in a ticket - either it will get accepted or not :) Thanks for your effort.

Posted: 02/12/08 12:27:29

Sure, I can rip the escape-sequence code out. But it'll be a bit small on its own so I'll leave you to fit it in somewhere...

Do you have much to do with tango.scrapple or is it largely a separate effort?

Posted: 02/12/08 12:45:35

tango.scrapple is mostly a separate effort, create a ticket in that project and attach your proposed module. Turnaround is usually fast. If you're in our channel from time to time, you may find Tim (nick is flithm), the mantainer.

Posted: 02/13/08 10:58:53

OK thanks.

For anyone interested: http://www.dsource.org/projects/tango.scrapple/ticket/16