string types: const(char)[] and cstring

Regan Heath regan at netmail.co.nz
Mon May 28 05:20:20 PDT 2007


renoX Wrote:
> Regan Heath a écrit :
> > renoX Wrote:
> >> I agree with you, I don't think that the string should be a char[]
> >>  alias, wether it's const or not but a class with
> >> char[],dchar[],wchar[] under the hood representation and safe
> >> slicing by default.
> >> 
> >> The difficulty is providing enough flexibility for managing
> >> correctly the internal representation: there should be a
> >> possibility to say use UTF8 even though there are multibyte
> >> characters for example (a size optimization with some CPU cost).
> > 
> > I think the class you describe would be useful, but only for certain
> > types of application.  Many applications (those that deal with ASCII
> 
> Hopefully a rare thing now.

No, sadly they aren't.  Most existing applications these days deal with ASCII or one of the strange code pages (which youd handle in D with ubyte and appropriate conversion to one of UTF8, 16 or 32 internally).

Granted in the case of the code page apps you might want a String class which can be produced by a <codepage>toString() free function which leverages iconv (which is just what I suggested)

However you may only want to deal with them as UTF-8 internally therefore not need the functionality provided by the class, opting instead to use 'string' directly.

Sure, in the future I expect/hope people will move to UTF8, 16, and 32 but I suspect code pages will be hauting us for many years to come.

> > wont need the sorts of
> > things this class provides and can get away with just using
> > 'const(char[])' AKA 'string'.  Basically I think there is a ample
> > room for both 'string' as an alias and 'String' as a class to exist
> > at the same time.
> 
> Room of course, but IMHO one should almost always use the class (except 
> in wrappers of native calls) instead of the alias.

I think that's an invalid assertion, specifically your use of the word 'always'.  There are 'almost certainly' (see, my term leaves room for me to be wrong) many cases where the alias would be preferred, most likely for performance reasons, espeically if the added functionality isn't required.

In other words, all I'm saying is; sometimes you want it, sometimes you don't.  Both can exist, both can be used and both should be interchangable (without too much trouble).

Regan



More information about the Digitalmars-d-announce mailing list