Break/Continue Structure

Oskar Linde oskar.lindeREM at OVEgmail.com
Fri Mar 24 02:36:51 PST 2006


Rory Starkweather wrote:
> Oskar Linde wrote:
>> Derek Parnell wrote:
>>
>>
>>> BTW, using the foreach this way can be misleading. The pointer value
>>> returned represents the number of dchars examined and *not* an index 
>>> into
>>> theString. This is significant if theString is not a dchar[].
>>
>>
>> That is not correct. The index returned is an index into the char[] 
>> array,
>> not the number of dchars processed:
>>
>> void main() {
>>         foreach(uint ix, dchar c; "едц"c)
>>                 writefln("c = %s, ix = %s",c,ix);
>> }
>>
>> Prints:
>>
>> c = е, ix = 0
>> c = д, ix = 2
>> c = ц, ix = 4
>>
>> /Oskar
> 
> 
>  I'm having some trouble understanding this implementation of the 
> 'foreach' construct. From the definiton of the'foreach' expression, the 
> purpose of the 'c' in . . .; "едц"c) is not clear to me. Does this 
> implicitly declare an array of items with the same data type as 'c'? In 
> other words, three dchars in this case? From Oskar's comment that seems 
> unlikely.

No, the c is a suffix to the string literal, defining it as a  char[]. 
Other suffixes are w and d for wchar[] and dchar[] respectively. Here 
follows a clearer version (with longer variable names ;) ):

void main() {
	char[] string = "едц"; // contains 6 UTF-8 code units
         foreach(uint index, dchar character; string)
                 writefln("character = %s, index = %s",character,index);
}

In this case, the c suffix is superfluous since the data type of string 
(char[])  makes the conversion implicit.

/Oskar



More information about the Digitalmars-d-learn mailing list