De-Referencing A Pointer

James Dunne james.jdunne at gmail.com
Thu Mar 23 07:37:11 PST 2006


Derek Parnell wrote:
> On Thu, 23 Mar 2006 02:59:09 +1100, James Dunne 
> <james.jdunne at gmail.com>  wrote:
> 
> 
> 
>> I had to look at the std.string.d module's find() method to see if it is
>> accepting ASCII or UTF-8, and I couldn't figure out which!  The code is:
>>
>> int find(char[] s, dchar c)
>> {
>>      char* p;
>>
>>      if (c <= 0x7F)
>>      {    // Plain old ASCII
>>     p = cast(char*)memchr(s, c, s.length);
>>     if (p)
>>         return p - cast(char *)s;
>>     else
>>         return -1;
>>      }
>>
>>      // c is a universal character
>>      foreach (int i, dchar c2; s)
>>      {
>>     if (c == c2)
>>         return i;
>>      }
>>      return -1;
>> }
>>
>> This doesn't make a lick of sense to me why one can iterate over a
>> char[] with foreach, expecting dchars to come out of it outside the
>> range of ASCII...  is there something going on under the hood that I'm
>> not aware of?
> 
> 
> The foreach() has a mode of operation that automatically converts UTF  
> encodings one character at a time.  Thus  foreach(dchar c; "abcdef"c) 
> is  valid code.
> 

Okay, that's where I was confused.  Thanks!

>> Is this code trying to imply that the char[] is being
>> treated as UTF-8 magically?
> 
> 
> char[] *is* utf-8 it is not ASCII.  No magic here.
> 

I understand that, I just didn't know the hidden foreach magic.

-- 
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS/MU/S d-pu s:+ a-->? C++++$ UL+++ P--- L+++ !E W-- N++ o? K? w--- O 
M--@ V? PS PE Y+ PGP- t+ 5 X+ !R tv-->!tv b- DI++(+) D++ G e++>e 
h>--->++ r+++ y+++
------END GEEK CODE BLOCK------

James Dunne



More information about the Digitalmars-d-learn mailing list