pu$�le

Jonathan M Davis jmdavisprog at gmail.com
Sat Jul 17 20:14:54 PDT 2010


On Saturday 17 July 2010 18:59:18 strtr wrote:
> That is [dollar sign, euro sign]
> 
> The reason I post it is because I expected the stash to be 3 lower.

As to why it's not working right, change th foreach loop to this:

foreach(dchar coin; coins)
{
...
}

Otherwise, instead of looping over each code point, you're looping over each 
code unit. char[] and string are encoded in utf-8, so each char is a code unit, 
and 1 - 4 code units are put together to form a code point, which is what you'd 
normally think of as a character.

The dollar sign takes one code unit in utf-8, but the euro sign takes 3. So, 
you're looping 4 times instead of 2. By specifying dchar, the compiler 
automatically processes the code units correctly to make it so that you loop 
over each code point (i.e. character) rather than each code unit (i.e. char).

You should pretty much never deal with each individual char or wchar in a string 
or wstring. Do the conversion to dchar or dstring if you want to access 
individual characters. You can also use std.utf.stride() to iterate over to the 
next code unit which starts a code point, but you're still going to have to make 
sure that you convert it to a dchar to process it properly. Otherwise, only 
ASCII characters will work right (since they fit in a single code unit). 
Fortunately, foreach takes care of all this for is if we specify the element 
type as dchar.

As for why it's 4 rather than 2 in the corrected version (or 8 instead of 4 in 
the incorrect version), that's because you have both scope(exit) and 
scop(success) there. Both will be run, so both will increment stash, and you get 
double the increments that you seem to be expecting.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list