First Impressions

Anders F Björklund afb at algonet.se
Fri Sep 29 14:01:17 PDT 2006


Chad J > wrote:

> char[] data; 

>   dchar opIndex( int index )
>   {
>     foreach( int i, dchar c; data )
>     {
>       if ( i == index )
>         return c;
> 
>       i++;
>     }
>   }

This code probably does not work as you think it does...

If you loop through a char[] using dchars (with a foreach),
then the int will get the codeunit index - *not* codepoint.
(the ++ in your code above looks more like a typo though,
since it needs to *either* foreach i, or do it "manually")

import std.stdio;
void main()
{
    char[] str = "Björklund";
    foreach(int i, dchar c; str)
    {
      writefln("%4d \\U%08X '%s'", i, c, ""d ~ c);
    }
}

Will print the following sequence:

    0 \U00000042 'B'
    1 \U0000006A 'j'
    2 \U000000F6 'ö'
    4 \U00000072 'r'
    5 \U0000006B 'k'
    6 \U0000006C 'l'
    7 \U00000075 'u'
    8 \U0000006E 'n'
    9 \U00000064 'd'

Notice how the non-ASCII character takes *two* code units ?
(if you expect indexing to use characters, that'd be wrong)

More at http://prowiki.org/wiki4d/wiki.cgi?CharsAndStrs

--anders



More information about the Digitalmars-d mailing list