First Impressions
Chad J
"gamerChad\" at spamIsBad gmail.com
Fri Sep 29 15:03:43 PDT 2006
Anders F Björklund wrote:
> 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
ah. And yep the i++ was a typo (oops).
So maybe something like:
dchar opIndex( int index )
{
int i;
foreach( dchar c; data )
{
if ( i == index )
return c;
i++;
}
}
The i is no longer the foreach's index, so the i++ isn't a typo anymore.
Thanks for the info. I'll check out that faq a little later, gotta go.
More information about the Digitalmars-d
mailing list