Casting between char[]/wchar[]/dchar[]

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Sat Aug 5 14:35:25 PDT 2006


Jarrett Billingsley wrote:
> "Hasan Aljudy" <hasan.aljudy at gmail.com> wrote in message 
> news:eb2u9n$psv$1 at digitaldaemon.com...
> 
>> Can I ask you atleast to simplify the conversion by adding properties utf* 
>> to char/wchar/dchar arrays?
>>
>> so, if I have:
>> ----
>> char[] process( char[] str ) { ... }
>>
>> ...
>>
>> dchar[] my32str = .....;
>>
>> //I can write
>> my32str = process( my32str.utf8 ).utf32;
>>
>> //instead of
>> //my32str = toUTF32( process( toUTF8( my32str ) ) );
> 
> import utf = std.utf;
> 
> wchar[] utf16(char[] s)
> {
>     return utf.toUTF16(s);
> }
> 
> ...
> 
> char[] s = "hello";
> wchar[] t = s.utf16;
> 
>  ;)
> 
> Aren't first-array-param-as-a-property functions cool? 

In fact, "raw" toUTF* functions work without the wrapper functions 
(though they're obviously named differently):

import std.utf;

void main()
{
     char[] s = "hello";
     wchar[] t = s.toUTF16();

     // Or, if you prefer:
     alias toUTF16 utf16;
     wchar[] u = s.utf16();
}



More information about the Digitalmars-d mailing list