access foreach index counter for Iterate n times

mw mingwu at gmail.com
Sat May 21 15:38:40 UTC 2022


On Saturday, 21 May 2022 at 15:21:09 UTC, user1234 wrote:
> On Saturday, 21 May 2022 at 15:06:31 UTC, mw wrote:
>> On Saturday, 21 May 2022 at 11:59:42 UTC, Adam D Ruppe wrote:
>>> On Saturday, 21 May 2022 at 03:01:39 UTC, mw wrote:
>>>> Yes! let's call it "loop counter"
>>>
>>> It isn't a loop counter. It is an index back into the 
>>> original source. Consider:
>>>
>>> foreach(INDEX, dchar character; "“”")
>>>    writeln(INDEX); // 0 then 3
>>
>> Wow, thus surprised me again!
>>
>> 1) First, why not 0 then 4? Since dchar is 32 bits.
>>
>> 2) Second, compare:
>>
>> import std;
>> void main()
>> {
>>     dstring ds = "“”";
>>     writeln(ds.length);
>>     foreach(INDEX, dchar character; ds)
>>       writeln(INDEX, character); // 0 then
>> }
>>
>> Output:
>> 2
>> 0“
>> 1”
>>
>> Explanations?
>
> Adam D Ruppe expamples implies auto (hidden) decoding, which is 
> a special case, so it reads 3 bytes to decode the the 1st glyph.

Why 3 bytes? Not 4 bytes?

As dchar is specified as 32 bits here?

https://dlang.org/spec/type.html




More information about the Digitalmars-d mailing list