De-Referencing A Pointer

Derek Parnell derek at psych.ward
Thu Mar 23 05:48:29 PST 2006


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.

> 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.

-- 
Derek Parnell
Melbourne, Australia



More information about the Digitalmars-d-learn mailing list